Read a configuration file: Difference between revisions
Content added Content deleted
(→{{header|PowerShell}}: Using Switch -Regex) |
(→Using Switch -Regex: Using regex \s) |
||
Line 3,430: | Line 3,430: | ||
=== Using Switch -Regex === |
=== Using Switch -Regex === |
||
<lang PowerShell> |
<lang PowerShell> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
Function Read-ConfigurationFile { |
Function Read-ConfigurationFile { |
||
[CmdletBinding()] |
[CmdletBinding()] |
||
Line 3,463: | Line 3,448: | ||
) |
) |
||
Begin { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
Process { |
Process { |
||
$__Configuration = [Ordered]@{} |
$__Configuration = [Ordered]@{} |
||
# Config equivalent pattern |
# Config equivalent pattern |
||
# Select-String -Pattern '^ |
# Select-String -Pattern '^\s*([^\s;#=]+).*\s*$' -LiteralPath '.\filename.cfg' |
||
Switch -Regex -File $_LiteralPath { |
Switch -Regex -File $_LiteralPath { |
||
'^ |
'^(\s*)[;#=]|^(\s*)$' { |
||
Write-Verbose -Message ' |
Write-Verbose -Message "v$(' '*20)ignored" |
||
Write-Verbose -Message $Matches[0] |
Write-Verbose -Message $Matches[0] |
||
Continue |
Continue |
||
Line 3,479: | Line 3,481: | ||
Write-Verbose -Message $Matches[0] |
Write-Verbose -Message $Matches[0] |
||
$__Name,$__Value = $Matches[1..2] |
$__Name,$__Value = $Matches[1..2] |
||
$__Configuration[$__Name.Trim( |
$__Configuration[$__Name.Trim()] = Houdini-Value($__Value) |
||
Continue |
Continue |
||
} |
} |
||
'^ |
'^\s*([^\s;#=]+)(.*)(\s*)$' { |
||
Write-Verbose -Message '↓← ← ← ← ← ← ← ← ← ← space or tab pattern or only name' |
Write-Verbose -Message '↓← ← ← ← ← ← ← ← ← ← space or tab pattern or only name' |
||
Write-Verbose -Message $Matches[0] |
Write-Verbose -Message $Matches[0] |
||
$__Name,$__Value = $Matches[ |
$__Name,$__Value = $Matches[1..2] |
||
$__Configuration[$__Name.Trim( |
$__Configuration[$__Name.Trim()] = Houdini-Value($__Value) |
||
Continue |
Continue |
||
} |
} |