Skip to content

Commit

Permalink
getopt: return remaining args, use getopt for scoop install
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesampson committed Sep 3, 2013
1 parent fd7514f commit b7cfd6f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
28 changes: 2 additions & 26 deletions lib/opts.ps1
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
function parse_args($a) {
$apps = @(); $arch = $null; $global = $false

for($i = 0; $i -lt $a.length; $i++) {
$arg = $a[$i]
if($arg.startswith('-')) {
switch($arg) {
'-arch' {
if($a.length -gt $i + 1) { $arch = $a[$i++] }
else { write-host '-arch parameter requires a value'; exit 1 }
}
'-global' {
$global = $true
}
default {
write-host "unrecognised parameter: $arg"; exit 1
}
}
} else {
$apps += $arg
}
}

$apps, $arch, $global
}

# adapted from http://hg.python.org/cpython/file/2.7/Lib/getopt.py
# returns @(opts hash, rem_args array, error string)
function getopt($argv, $shortopts, $longopts) {
Expand Down Expand Up @@ -75,6 +49,8 @@ function getopt($argv, $shortopts, $longopts) {
return err "option -$letter not recognized"
}
}
} else {
$rem += $arg
}
}

Expand Down
10 changes: 7 additions & 3 deletions libexec/scoop-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# When installing from your computer, you can leave the .json extension off if you like.
#
# Options:
# -arch 32bit|64bit use the specified architecture, if the app supports it
# -global install the app globally
# -a, --arch <32bit|64bit> use the specified architecture, if the app supports it
# -g, --global install the app globally

. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\manifest.ps1"
Expand Down Expand Up @@ -76,7 +76,11 @@ function install($app, $architecture, $global) {
show_notes $manifest
}

$apps, $architecture, $global = parse_args $args
$opt, $apps, $err = getopt $args 'ga:' 'global', 'arch='
if($err) { "scoop install: $err"; exit 1 }

$global = $opt.g -or $opt.global
$architecture = $opt.a + $opt.arch

switch($architecture) {
'' { $architecture = architecture }
Expand Down
13 changes: 11 additions & 2 deletions test/opts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ test 'handle unrecognized long option' {
assert $err -ne $null
assert $err -eq 'option --non-exist not recognized'

$null, $null, $err = getopt '--global', '--another' 'abc:de:' 'global', 'one'
$null, $null, $err = getopt '--global','--another' 'abc:de:' 'global','one'
assert $err -eq 'option --another not recognized'
}

test 'remaining args returned' {
$opt, $rem, $err = getopt '-g','rem' 'g' ''
assert $err -eq $null
assert $opt.g -eq $true
assert $rem -ne $null
assert $rem.length -eq 1
assert $rem[0] -eq 'rem'
}

test 'get a long flag and a short option with argument' {
$a = "--global -a 32bit test" -split ' '
$opt, $rem, $err = getopt $a 'ga:' 'global', 'arch='
$opt, $rem, $err = getopt $a 'ga:' 'global','arch='
assert $err -eq $null
assert $opt.global -eq $true
assert $opt.a -eq '32bit'
Expand Down

0 comments on commit b7cfd6f

Please sign in to comment.