Fix error when passing regex keywords to getopt #386

This commit is contained in:
Simon Hartcher
2015-07-24 13:38:27 +10:00
parent c71243a7df
commit 8a0e8b8bc0
2 changed files with 30 additions and 3 deletions
+6 -2
View File
@@ -15,6 +15,10 @@ function getopt($argv, $shortopts, $longopts) {
$opts, $rem, $msg
}
function regex_escape($str) {
return [regex]::escape($str)
}
# ensure these are arrays
$argv = @($argv)
$longopts = @($longopts)
@@ -46,7 +50,7 @@ function getopt($argv, $shortopts, $longopts) {
for($j = 1; $j -lt $arg.length; $j++) {
$letter = $arg[$j].tostring()
if($shortopts -match "$letter`:?") {
if($shortopts -match "$(regex_escape $letter)`:?") {
$shortopt = $matches[0]
if($shortopt[1] -eq ':') {
if($j -ne $arg.length -1 -or $i -eq $argv.length - 1) {
@@ -66,4 +70,4 @@ function getopt($argv, $shortopts, $longopts) {
}
$opts, $rem
}
}
+24 -1
View File
@@ -45,4 +45,27 @@ describe "getopt" {
$opt.global | should be $true
$opt.a | should be '32bit'
}
}
it 'handles regex characters' {
$a = "-?"
{ $opt, $rem, $err = getopt $a 'ga:' 'global' 'arch=' } | should not throw
{ $null, $null, $null = getopt $a '?:' 'help' | should not throw }
}
it 'handles short option without required argument' {
$null, $null, $err = getopt '-x' 'x' ''
$err | should be $null
}
it 'handles long option without required argument' {
$opt, $null, $err = getopt '--long-arg' '' 'long-arg'
$err | should be $null
$opt."long-arg" | should be $true
}
it 'handles long option with required argument' {
$opt, $null, $err = getopt '--long-arg', 'test' '' 'long-arg='
$err | should be $null
$opt."long-arg" | should be "test"
}
}