Jump to content

Update a configuration file: Difference between revisions

no edit summary
No edit summary
Line 1,530:
(for L Data
(prinl (glue " " (if (car L) L (cdr L)))) ) ) )</lang>
 
=={{header|PowerShell}}==
<lang PowerShell>
function Update-ConfigurationFile
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false,
Position=0)]
[ValidateScript({Test-Path $_})]
[string]
$Path = ".\config.txt",
 
[Parameter(Mandatory=$false)]
[string]
$FavouriteFruit,
 
[Parameter(Mandatory=$false)]
[int]
$NumberOfBananas,
 
[Parameter(Mandatory=$false)]
[int]
$NumberOfStrawberries,
 
[Parameter(Mandatory=$false)]
[ValidateSet("On", "Off")]
[string]
$NeedsPeeling,
 
[Parameter(Mandatory=$false)]
[ValidateSet("On", "Off")]
[string]
$SeedsRemoved
)
 
[string[]]$lines = Get-Content $Path
 
Clear-Content $Path
 
if (-not ($lines | Select-String -Pattern "^\s*NumberOfStrawberries" -Quiet))
{
"", "# How many strawberries we have", "NumberOfStrawberries 0" | ForEach-Object {$lines += $_}
}
 
foreach ($line in $lines)
{
$line = $line -replace "^\s*","" ## Strip leading whitespace
 
switch -Regex ($line)
{
"(^$)|(^#.*)" ## Blank line or comment
{
$line = $line
}
"^FavouriteFruit\s*.*" ## Parameter FavouriteFruit
{
if ($FavouriteFruit)
{
$line = "FavouriteFruit $FavouriteFruit"
}
else
{
$line = $line
}
}
"^NumberOfBananas\s*.*" ## Parameter NumberOfBananas
{
if ($NumberOfBananas)
{
$line = "NumberOfBananas $NumberOfBananas"
}
else
{
$line = $line
}
}
"^NumberOfStrawberries\s*.*" ## Parameter NumberOfStrawberries
{
if ($NumberOfStrawberries)
{
$line = "NumberOfStrawberries $NumberOfStrawberries"
}
else
{
$line = $line
}
}
".*NeedsPeeling\s*.*" ## Parameter NeedsPeeling
{
if ($NeedsPeeling -eq "On")
{
$line = "NeedsPeeling"
}
elseif ($NeedsPeeling -eq "Off")
{
$line = "; NeedsPeeling"
}
else
{
$line = $line
}
}
".*SeedsRemoved\s*.*" ## Parameter SeedsRemoved
{
if ($SeedsRemoved -eq "On")
{
$line = "SeedsRemoved"
}
elseif ($SeedsRemoved -eq "Off")
{
$line = "; SeedsRemoved"
}
else
{
$line = = $line
}
}
Default ## Whatever...
{
$line = $line
}
}
 
Add-Content $Path -Value $line -Force
}
}
</lang>
<lang PowerShell>
Update-ConfigurationFile -NumberOfStrawberries 62000 -NumberOfBananas 1024 -SeedsRemoved On -NeedsPeeling Off
 
Invoke-Item -Path ".\config.txt"
</lang>
{{Out}}
<pre>
# The first word on each non comment line is the configuration option.
# Remaining words or numbers on the line are configuration parameter
# data fields.
 
# Note that configuration option names are not case sensitive. However,
# configuration parameter data is case sensitive and the lettercase must
# be preserved.
 
# This is a favourite fruit
FAVOURITEFRUIT banana
 
# This is a boolean that should be set
; NeedsPeeling
 
# This boolean is commented out
SeedsRemoved
 
# How many bananas we have
NumberOfBananas 1024
 
# How many strawberries we have
NumberOfStrawberries 62000
</pre>
 
=={{header|Python}}==
308

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.