Skip to content

Commit b7cfd6f

Browse files
committed
getopt: return remaining args, use getopt for scoop install
1 parent fd7514f commit b7cfd6f

3 files changed

Lines changed: 20 additions & 31 deletions

File tree

lib/opts.ps1

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
function parse_args($a) {
2-
$apps = @(); $arch = $null; $global = $false
3-
4-
for($i = 0; $i -lt $a.length; $i++) {
5-
$arg = $a[$i]
6-
if($arg.startswith('-')) {
7-
switch($arg) {
8-
'-arch' {
9-
if($a.length -gt $i + 1) { $arch = $a[$i++] }
10-
else { write-host '-arch parameter requires a value'; exit 1 }
11-
}
12-
'-global' {
13-
$global = $true
14-
}
15-
default {
16-
write-host "unrecognised parameter: $arg"; exit 1
17-
}
18-
}
19-
} else {
20-
$apps += $arg
21-
}
22-
}
23-
24-
$apps, $arch, $global
25-
}
26-
271
# adapted from http://hg.python.org/cpython/file/2.7/Lib/getopt.py
282
# returns @(opts hash, rem_args array, error string)
293
function getopt($argv, $shortopts, $longopts) {
@@ -75,6 +49,8 @@ function getopt($argv, $shortopts, $longopts) {
7549
return err "option -$letter not recognized"
7650
}
7751
}
52+
} else {
53+
$rem += $arg
7854
}
7955
}
8056

libexec/scoop-install.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# When installing from your computer, you can leave the .json extension off if you like.
1313
#
1414
# Options:
15-
# -arch 32bit|64bit use the specified architecture, if the app supports it
16-
# -global install the app globally
15+
# -a, --arch <32bit|64bit> use the specified architecture, if the app supports it
16+
# -g, --global install the app globally
1717

1818
. "$psscriptroot\..\lib\core.ps1"
1919
. "$psscriptroot\..\lib\manifest.ps1"
@@ -76,7 +76,11 @@ function install($app, $architecture, $global) {
7676
show_notes $manifest
7777
}
7878

79-
$apps, $architecture, $global = parse_args $args
79+
$opt, $apps, $err = getopt $args 'ga:' 'global', 'arch='
80+
if($err) { "scoop install: $err"; exit 1 }
81+
82+
$global = $opt.g -or $opt.global
83+
$architecture = $opt.a + $opt.arch
8084

8185
switch($architecture) {
8286
'' { $architecture = architecture }

test/opts.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ test 'handle unrecognized long option' {
2626
assert $err -ne $null
2727
assert $err -eq 'option --non-exist not recognized'
2828

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

33+
test 'remaining args returned' {
34+
$opt, $rem, $err = getopt '-g','rem' 'g' ''
35+
assert $err -eq $null
36+
assert $opt.g -eq $true
37+
assert $rem -ne $null
38+
assert $rem.length -eq 1
39+
assert $rem[0] -eq 'rem'
40+
}
41+
3342
test 'get a long flag and a short option with argument' {
3443
$a = "--global -a 32bit test" -split ' '
35-
$opt, $rem, $err = getopt $a 'ga:' 'global', 'arch='
44+
$opt, $rem, $err = getopt $a 'ga:' 'global','arch='
3645
assert $err -eq $null
3746
assert $opt.global -eq $true
3847
assert $opt.a -eq '32bit'

0 commit comments

Comments
 (0)