3 declare -i testcount
=0 pass
=0 fail
=0
5 # source the library function
6 if [[ -z $1 ||
! -f $1 ]]; then
7 printf "error: path to parseopts library not provided or does not exist\n"
12 if ! type -t parseopts
>/dev
/null
; then
13 printf 'parseopts function not found\n'
17 # borrow opts from makepkg
18 OPT_SHORT
="AcdefFghiLmop:rRsV"
19 OPT_LONG
=('allsource' 'asroot' 'ignorearch' 'check' 'clean:' 'cleanall' 'nodeps'
20 'noextract' 'force' 'forcever:' 'geninteg' 'help' 'holdver'
21 'install' 'key:' 'log' 'nocolor' 'nobuild' 'nocheck' 'nosign' 'pkg:' 'rmdeps'
22 'repackage' 'skipinteg' 'sign' 'source' 'syncdeps' 'version' 'config:'
23 'noconfirm' 'noprogressbar')
26 local result
=$1 tokencount
=$2; shift 2
29 parseopts
"$OPT_SHORT" "${OPT_LONG[@]}" -- "$@" 2>/dev
/null
30 test_result
"$result" "$tokencount" "$*" "${OPTRET[@]}"
35 local result
=$1 tokencount
=$2 input
=$3; shift 3
37 if [[ $result = "$*" ]] && (( tokencount
== $# )); then
40 printf '[TEST %3s]: FAIL\n' "$testcount"
41 printf ' input: %s\n' "$input"
42 printf ' output: %s (%s tokens)\n' "$*" "$#"
43 printf ' expected: %s (%s tokens)\n' "$result" "$tokencount"
51 printf 'All %s tests successful\n\n' "$testcount"
54 printf '%s of %s tests failed\n\n' "$fail" "$testcount"
60 printf 'Beginning parseopts tests\n'
62 # usage: parse <expected result> <token count> test-params...
63 # a failed parse will match only the end of options marker '--'
69 parse
'-s -r --' 3 -s -r
71 # short options, no spaces
72 parse
'-s -r --' 3 -sr
74 # short opt missing an opt arg
77 # short opt with an opt arg
78 parse
'-p PKGBUILD -L --' 4 -p PKGBUILD
-L
80 # short opt with an opt arg, no space
81 parse
'-p PKGBUILD --' 3 -pPKGBUILD
83 # valid shortopts as a long opt
86 # long opt wiht no optarg
87 parse
'--log --' 2 --log
89 # long opt with missing optarg
90 parse
'--' 1 -sr --pkg
92 # long opt with optarg
93 parse
'--pkg foo --' 3 --pkg foo
95 # long opt with optarg with whitespace
96 parse
'--pkg foo bar -- baz' 4 --pkg "foo bar" baz
98 # long opt with optarg with =
99 parse
'--pkg foo=bar -- baz' 4 --pkg foo
=bar baz
101 # long opt with explicit optarg
102 parse
'--pkg bar -- foo baz' 5 foo
--pkg=bar baz
104 # long opt with explicit optarg, with whitespace
105 parse
'--pkg foo bar -- baz' 4 baz
--pkg="foo bar"
107 # long opt with explicit optarg that doesn't take optarg
108 parse
'--' 1 --force=always
-s
110 # long opt with explicit optarg with =
111 parse
'--pkg foo=bar --' 3 --pkg=foo
=bar
113 # explicit end of options with options after
114 parse
'-s -r -- foo bar baz' 6 -s -r -- foo bar baz
116 # non-option parameters mixed in with options
117 parse
'-s -r -- foo baz' 5 -s foo baz
-r
119 # optarg with whitespace
120 parse
'-p foo bar -s --' 4 -p'foo bar' -s
122 # non-option parameter with whitespace
123 parse
'-i -- foo bar' 3 -i 'foo bar'
125 # successful stem match (opt has no arg)
126 parse
'--nocolor --' 2 --nocol
128 # successful stem match (opt has arg)
129 parse
'--config foo --' 3 --conf foo
134 # exact match on a possible stem (--force & --forcever)
135 parse
'--force --' 2 --force
137 # exact match on possible stem (opt has optarg)
138 parse
'--clean foo --' 3 --clean=foo