Last active
June 3, 2025 02:31
-
-
Save scriptingstudio/3d637d12016da9836451ee9721757d17 to your computer and use it in GitHub Desktop.
Get ValueFromRemainingArguments/Args as HashTable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test-remaining { | |
[cmdletbinding()] | |
param ( | |
[parameter(ValueFromRemainingArguments)] | |
$remaining | |
) | |
$htRemaining = [ordered]@{} | |
$unnamed = [System.Collections.Generic.list[object]]::new() | |
$remaining | ForEach-Object { | |
if ($_ -match '^-(?<param>[^:]+):?$') { | |
# New parameter | |
$lastpar = $matches['param'] | |
$htRemaining[$lastpar] = $true # Default value for switch | |
} elseif ($lastpar) { | |
# Update value to next argument | |
$htRemaining[$lastpar] = $_ | |
$lastpar = $null | |
} else { | |
$unnamed.add($_) | |
} | |
} | |
if ($unnamed.count) {$htRemaining['unnamed'] = $unnamed} | |
$htRemaining | |
} | |
function convert-args { | |
$htRemaining = [ordered]@{} | |
$unnamed = [System.Collections.Generic.list[object]]::new() | |
$args | ForEach-Object { | |
if ($_ -match '^-(?<param>[^:]+):?$') { | |
# New parameter | |
$lastpar = $matches['param'] | |
$htRemaining[$lastpar] = $true # Default value for switch | |
} elseif ($lastpar) { | |
# Update value to next argument | |
$htRemaining[$lastpar] = $_ | |
$lastpar = $null | |
} else { | |
$unnamed.add($_) | |
} | |
} | |
if ($unnamed.count) {$htRemaining['unnamed'] = $unnamed} | |
$htRemaining | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment