Read a configuration file: Difference between revisions
Content added Content deleted
(→{{header|PowerShell}}: Using Switch -Regex) |
|||
Line 3,426: | Line 3,426: | ||
seedsRemoved False |
seedsRemoved False |
||
otherFamily {Rhu Barber, Harry Barber} |
otherFamily {Rhu Barber, Harry Barber} |
||
</pre> |
|||
=== Using Switch -Regex === |
|||
<lang PowerShell> |
|||
Function Houdini-Value ([String]$_Text) { |
|||
$__Aux = $_Text.Trim(" `t") |
|||
If ($__Aux.Length -eq 0) { |
|||
$__Aux = $true |
|||
} ElseIf ($__Aux.Contains(',')) { |
|||
$__Aux = $__Aux.Split(',') | |
|||
ForEach-Object { |
|||
If ($PSItem.Trim(" `t").Length -ne 0) { |
|||
$PSItem.Trim(" `t") |
|||
} |
|||
} |
|||
} |
|||
Return $__Aux |
|||
} |
|||
Function Read-ConfigurationFile { |
|||
[CmdletBinding()] |
|||
[OutputType([Collections.Specialized.OrderedDictionary])] |
|||
Param ( |
|||
[Parameter( |
|||
Mandatory=$true, |
|||
Position=0 |
|||
) |
|||
] |
|||
[Alias('LiteralPath')] |
|||
[ValidateScript({ |
|||
Test-Path -LiteralPath $PSItem -PathType 'Leaf' |
|||
}) |
|||
] |
|||
[String] |
|||
$_LiteralPath |
|||
) |
|||
Process { |
|||
$__Configuration = [Ordered]@{} |
|||
# Config equivalent pattern |
|||
# Select-String -Pattern '^((\t| )*)([^\t;#= ]+)(.*)((\t| )*)$' -LiteralPath '.\filename.cfg' |
|||
Switch -Regex -File $_LiteralPath { |
|||
'^((\t| )*)[;#=]|^((\t| )*)$' { |
|||
Write-Verbose -Message '↓← ← ← ← ← ← ← ← ← ← ignored' |
|||
Write-Verbose -Message $Matches[0] |
|||
Continue |
|||
} |
|||
'^([^=]+)=(.*)$' { |
|||
Write-Verbose -Message '↓← ← ← ← ← ← ← ← ← ← equal pattern' |
|||
Write-Verbose -Message $Matches[0] |
|||
$__Name,$__Value = $Matches[1..2] |
|||
$__Configuration[$__Name.Trim(" `t")] = Houdini-Value($__Value) |
|||
Continue |
|||
} |
|||
'^((\t| )*)([^\t;#= ]+)(.*)((\t| )*)$' { |
|||
Write-Verbose -Message '↓← ← ← ← ← ← ← ← ← ← space or tab pattern or only name' |
|||
Write-Verbose -Message $Matches[0] |
|||
$__Name,$__Value = $Matches[3..4] |
|||
$__Configuration[$__Name.Trim(" `t")] = Houdini-Value($__Value) |
|||
Continue |
|||
} |
|||
} |
|||
Return $__Configuration |
|||
} |
|||
} |
|||
Function Show-Value ([Collections.Specialized.OrderedDictionary]$_Dictionary, $_Index, $_SubIndex) { |
|||
$__Aux = $_Index + ' = ' |
|||
If ($_Dictionary[$_Index] -eq $null) { |
|||
$__Aux += $false |
|||
} ElseIf ($_Dictionary[$_Index].Count -gt 1) { |
|||
If ($_SubIndex -eq $null) { |
|||
$__Aux += $_Dictionary[$_Index] -join ',' |
|||
} Else { |
|||
$__Aux = $_Index + '(' + $_SubIndex + ') = ' |
|||
If ($_Dictionary[$_Index][$_SubIndex] -eq $null) { |
|||
$__Aux += $false |
|||
} Else { |
|||
$__Aux += $_Dictionary[$_Index][$_SubIndex] |
|||
} |
|||
} |
|||
} Else { |
|||
$__Aux += $_Dictionary[$_Index] |
|||
} |
|||
Return $__Aux |
|||
} |
|||
</lang> |
|||
Setting variable |
|||
<lang PowerShell> |
|||
$Configuration = Read-ConfigurationFile -LiteralPath '.\config.cfg' |
|||
</lang> |
|||
Show variable |
|||
<lang PowerShell> |
|||
$Configuration |
|||
</lang> |
|||
{{Out}} |
|||
<pre> |
|||
Name Value |
|||
---- ----- |
|||
FULLNAME Foo Barber |
|||
FAVOURITEFRUIT banana |
|||
NEEDSPEELING True |
|||
OTHERFAMILY {Rhu Barber, Harry Barber} |
|||
</pre> |
|||
Using customize function |
|||
<lang PowerShell> |
|||
Show-Value $Configuration 'fullname' |
|||
Show-Value $Configuration 'favouritefruit' |
|||
Show-Value $Configuration 'needspeeling' |
|||
Show-Value $Configuration 'seedsremoved' |
|||
Show-Value $Configuration 'otherfamily' |
|||
Show-Value $Configuration 'otherfamily' 0 |
|||
Show-Value $Configuration 'otherfamily' 1 |
|||
Show-Value $Configuration 'otherfamily' 2 |
|||
</lang> |
|||
{{Out}} |
|||
<pre> |
|||
fullname = Foo Barber |
|||
favouritefruit = banana |
|||
needspeeling = True |
|||
seedsremoved = False |
|||
otherfamily = Rhu Barber,Harry Barber |
|||
otherfamily(0) = Rhu Barber |
|||
otherfamily(1) = Harry Barber |
|||
otherfamily(2) = False |
|||
</pre> |
|||
Using index variable |
|||
<lang PowerShell> |
|||
'$Configuration[''fullname'']' |
|||
$Configuration['fullname'] |
|||
'$Configuration.''fullname''' |
|||
$Configuration.'fullname' |
|||
'$Configuration.Item(''fullname'')' |
|||
$Configuration.Item('fullname') |
|||
'$Configuration[0]' |
|||
$Configuration[0] |
|||
'$Configuration.Item(0)' |
|||
$Configuration.Item(0) |
|||
' ' |
|||
'=== $Configuration[''otherfamily''] ===' |
|||
$Configuration['otherfamily'] |
|||
'=== $Configuration[''otherfamily''][0] ===' |
|||
$Configuration['otherfamily'][0] |
|||
'=== $Configuration[''otherfamily''][1] ===' |
|||
$Configuration['otherfamily'][1] |
|||
' ' |
|||
'=== $Configuration.''otherfamily'' ===' |
|||
$Configuration.'otherfamily' |
|||
'=== $Configuration.''otherfamily''[0] ===' |
|||
$Configuration.'otherfamily'[0] |
|||
'=== $Configuration.''otherfamily''[1] ===' |
|||
$Configuration.'otherfamily'[1] |
|||
' ' |
|||
'=== $Configuration.Item(''otherfamily'') ===' |
|||
$Configuration.Item('otherfamily') |
|||
'=== $Configuration.Item(''otherfamily'')[0] ===' |
|||
$Configuration.Item('otherfamily')[0] |
|||
'=== $Configuration.Item(''otherfamily'')[1] ===' |
|||
$Configuration.Item('otherfamily')[1] |
|||
' ' |
|||
'=== $Configuration[3] ===' |
|||
$Configuration[3] |
|||
'=== $Configuration[3][0] ===' |
|||
$Configuration[3][0] |
|||
'=== $Configuration[3][1] ===' |
|||
$Configuration[3][1] |
|||
' ' |
|||
'=== $Configuration.Item(3) ===' |
|||
$Configuration.Item(3) |
|||
'=== $Configuration.Item(3).Item(0) ===' |
|||
$Configuration.Item(3).Item(0) |
|||
'=== $Configuration.Item(3).Item(1) ===' |
|||
$Configuration.Item(3).Item(1) |
|||
</lang> |
|||
{{Out}} |
|||
<pre> |
|||
$Configuration['fullname'] |
|||
Foo Barber |
|||
$Configuration.'fullname' |
|||
Foo Barber |
|||
$Configuration.Item('fullname') |
|||
Foo Barber |
|||
$Configuration[0] |
|||
Foo Barber |
|||
$Configuration.Item(0) |
|||
Foo Barber |
|||
=== $Configuration['otherfamily'] === |
|||
Rhu Barber |
|||
Harry Barber |
|||
=== $Configuration['otherfamily'][0] === |
|||
Rhu Barber |
|||
=== $Configuration['otherfamily'][1] === |
|||
Harry Barber |
|||
=== $Configuration.'otherfamily' === |
|||
Rhu Barber |
|||
Harry Barber |
|||
=== $Configuration.'otherfamily'[0] === |
|||
Rhu Barber |
|||
=== $Configuration.'otherfamily'[1] === |
|||
Harry Barber |
|||
=== $Configuration.Item('otherfamily') === |
|||
Rhu Barber |
|||
Harry Barber |
|||
=== $Configuration.Item('otherfamily')[0] === |
|||
Rhu Barber |
|||
=== $Configuration.Item('otherfamily')[1] === |
|||
Harry Barber |
|||
=== $Configuration[3] === |
|||
Rhu Barber |
|||
Harry Barber |
|||
=== $Configuration[3][0] === |
|||
Rhu Barber |
|||
=== $Configuration[3][1] === |
|||
Harry Barber |
|||
=== $Configuration.Item(3) === |
|||
Rhu Barber |
|||
Harry Barber |
|||
=== $Configuration.Item(3).Item(0) === |
|||
Rhu Barber |
|||
=== $Configuration.Item(3).Item(1) === |
|||
Harry Barber |
|||
</pre> |
</pre> |
||