Skip to content

Commit b5a78fc

Browse files
committed
Update to getoptions v3.3.1
1 parent aa05c6e commit b5a78fc

11 files changed

Lines changed: 97 additions & 108 deletions

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PREFIX ?= /usr/local
22
BINDIR := $(PREFIX)/bin
33
LIBDIR := $(PREFIX)/lib
44

5-
GETOPTIONSCLI := getoptions-cli --indent=2 --shellcheck
5+
GENGETOPTIONS := gengetoptions
66
OPTPARSERDIR := lib/libexec/optparser
77

88
.PHONY: coverage test dist build release
@@ -25,11 +25,17 @@ uninstall:
2525
package:
2626
contrib/make_package_json.sh > package.json
2727

28+
# Retest getoptions in various shells with shellspec
29+
embed_getoptions:
30+
cp ../getoptions/lib/getoptions_*.sh lib
31+
cp ../getoptions/spec/getoptions_*_spec.sh spec
32+
2833
optparser:
2934
@printf "getoptions: "
30-
@$(GETOPTIONSCLI) --version
31-
$(GETOPTIONSCLI) $(OPTPARSERDIR)/parser_definition.sh \
32-
optparser_parse SHELLSPEC optparser_error \
35+
@$(GENGETOPTIONS) --version
36+
$(GENGETOPTIONS) parser --indent=2 --shellcheck \
37+
-f $(OPTPARSERDIR)/parser_definition.sh \
38+
parser_definition optparser_parse SHELLSPEC optparser_error \
3339
> $(OPTPARSERDIR)/parser_definition_generated.sh
3440

3541
demo:

lib/getoptions_abbr.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ getoptions_abbr() {
1010
args() {
1111
abbr=1
1212
shift
13-
for i; do
13+
[ $# -gt 0 ] || return 0
14+
for i in "$@"; do
1415
case $i in
1516
--) break ;;
1617
[!-+]*) eval "${i%%:*}=\${i#*:}"
1718
esac
1819
done
1920
[ "$abbr" ] || return 0
2021

21-
for i; do
22+
for i in "$@"; do
2223
case $i in
2324
--) break ;;
2425
--\{no-\}*) abbr "--${i#--\{no-\}}"; abbr "--no-${i#--\{no-\}}" ;;
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# shellcheck shell=sh disable=SC2016
22
# [getoptions] License: Creative Commons Zero v1.0 Universal
33
getoptions() {
4-
_error='' _on=1 _off='' _export='' _plus='' _mode='' _alt='' _rest=''
4+
_error='' _on=1 _no='' _export='' _plus='' _mode='' _alt='' _rest='' _def=''
55
_flags='' _nflags='' _opts='' _help='' _abbr='' _cmds='' _init=@empty IFS=' '
6+
[ $# -lt 2 ] && set -- "${1:?No parser definition}" -
7+
[ "$2" = - ] && _def=getoptions_parse
68

79
_0() { echo "$@"; }
810
for i in 1 2 3 4 5; do eval "_$i() { _$((${i-}-1)) \" \$@\"; }"; done
@@ -17,6 +19,7 @@ getoptions() {
1719
[ "${1#:}" = "$1" ] && c=3 || c=4
1820
eval "[ ! \${$c:+x} ] || $2 \"\$$c\""
1921
}
22+
sw() { sw="$sw${sw:+|}$1"; }
2023
kv() { eval "${2-}${1%%:*}=\${1#*:}"; }
2124
loop() { [ $# -gt 1 ] && [ "$2" != -- ]; }
2225

@@ -27,7 +30,7 @@ getoptions() {
2730
done
2831

2932
args() {
30-
on=$_on off=$_off export=$_export init=$_init _hasarg=$1 && shift
33+
on=$_on no=$_no export=$_export init=$_init _hasarg=$1 && shift
3134
while loop "$@" && shift; do
3235
case $1 in
3336
-?) [ "$_hasarg" ] && _opts="$_opts${1#-}" || _flags="$_flags${1#-}" ;;
@@ -64,30 +67,31 @@ getoptions() {
6467
cmd() { :; }
6568
_0 "${_rest:?}=''"
6669

67-
_0 "$2() {"
70+
_0 "${_def:-$2}() {"
6871
_1 'OPTIND=$(($#+1))'
6972
_1 'while OPTARG= && [ $# -gt 0 ]; do'
7073
[ "$_abbr" ] && getoptions_abbr "$@"
7174

7275
args() {
73-
sw='' validate='' pattern='' counter='' on=$_on off=$_off export=$_export
76+
sw='' validate='' pattern='' counter='' on=$_on no=$_no export=$_export
7477
while loop "$@" && shift; do
7578
case $1 in
76-
--\{no-\}*) i=${1#--?no-?}; sw="$sw${sw:+|}'--$i'|'--no-$i'" ;;
77-
[-+]? | --*) sw="$sw${sw:+|}'$1'" ;;
79+
--\{no-\}*) i=${1#--?no-?}; sw "'--$i'|'--no-$i'" ;;
80+
--with\{out\}-*) i=${1#--*-}; sw "'--with-$i'|'--without-$i'" ;;
81+
[-+]? | --*) sw "'$1'" ;;
7882
*) kv "$1"
7983
esac
8084
done
8185
quote on "$on"
82-
quote off "$off"
86+
quote no "$no"
8387
}
8488
setup() { :; }
8589
_flag() {
8690
args "$@"
87-
[ "$counter" ] && on=1 off=-1 v="\$((\${$1:-0}+\$OPTARG))" || v=''
91+
[ "$counter" ] && on=1 no=-1 v="\$((\${$1:-0}+\$OPTARG))" || v=''
8892
_3 "$sw)"
8993
_4 '[ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break'
90-
_4 "eval '[ \${OPTARG+x} ] &&:' && OPTARG=$on || OPTARG=$off"
94+
_4 "eval '[ \${OPTARG+x} ] &&:' && OPTARG=$on || OPTARG=$no"
9195
valid "$1" "${v:-\$OPTARG}"
9296
_4 ';;'
9397
}
@@ -104,9 +108,9 @@ getoptions() {
104108
_3 "$sw)"
105109
_4 'set -- "$1" "$@"'
106110
_4 '[ ${OPTARG+x} ] && {'
107-
_5 'case $1 in --no-*) set "noarg" "${1%%\=*}"; break; esac'
111+
_5 'case $1 in --no-*|--without-*) set "noarg" "${1%%\=*}"; break; esac'
108112
_5 '[ "${OPTARG:-}" ] && { shift; OPTARG=$2; } ||' "OPTARG=$on"
109-
_4 "} || OPTARG=$off"
113+
_4 "} || OPTARG=$no"
110114
valid "$1" '$OPTARG'
111115
_4 'shift ;;'
112116
}
@@ -139,7 +143,7 @@ getoptions() {
139143
_3 '--?*=*) OPTARG=$1; shift'
140144
_wa '"${OPTARG%%\=*}" "${OPTARG#*\=}"'
141145
_4 ';;'
142-
_3 '--no-*) unset OPTARG ;;'
146+
_3 '--no-*|--without-*) unset OPTARG ;;'
143147
[ "$_alt" ] || {
144148
[ "$_opts" ] && _op "-[$_opts]?*" '' ';;'
145149
[ ! "$_flags" ] || _op "-[$_flags]?*" - 'OPTARG= ;;'
@@ -161,16 +165,15 @@ getoptions() {
161165
_3 '--)'
162166
[ "$_mode" = @ ] || _4 'shift'
163167
rest
164-
_3 "[-${_plus:++}]?*)"
165-
case $_mode in [=#]) rest ;; *) _4 'set "unknown" "$1"; break ;;'; esac
168+
_3 "[-${_plus:++}]?*)" 'set "unknown" "$1"; break ;;'
166169
_3 '*)'
167170
case $_mode in
168171
@)
169172
_4 "case \$1 in ${_cmds:-*}) ;;"
170173
_5 '*) set "notcmd" "$1"; break'
171174
_4 'esac'
172175
rest ;;
173-
[+#]) rest ;;
176+
+) rest ;;
174177
*) _4 "$_rest=\"\${$_rest}" '\"\${$(($OPTIND-$#))}\""'
175178
esac
176179
_2 'esac'
@@ -190,5 +193,7 @@ getoptions() {
190193
_1 'exit 1'
191194
_0 '}'
192195

193-
[ ! "$_help" ] || eval "shift 2; getoptions_help $1 $_help" ${3+'"$@"'}
196+
[ "$_help" ] && eval "shift 2; getoptions_help $1 $_help" ${3+'"$@"'}
197+
[ "$_def" ] && _0 "eval $_def \${1+'\"\$@\"'}; eval set -- \"\${$_rest}\""
198+
_0 '# Do not execute' # exit 1
194199
}

lib/getoptions_help.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ getoptions_help() {
3030
[ ${#label} -le "$len" ] && [ $# -gt 0 ] && label="$label$1" && shift
3131
echo "$label"
3232
pad label '' "$len"
33-
for i; do echo "$label$i"; done
33+
[ $# -gt 0 ] || return 0
34+
for i in "$@"; do echo "$label$i"; done
3435
}
3536

3637
for i in setup flag param option disp 'msg -' cmd; do

lib/libexec/optparser/parser_definition.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ parser_definition() {
9797
param LOGFILE --log-file init:='/dev/tty' -- \
9898
'Log file for %logger directive and trace [default: "/dev/tty"]'
9999

100-
param TMPDIR --tmpdir init:"export $2_TMPDIR="'${TMPDIR:-${TMP:-/tmp}}' -- \
100+
param TMPDIR --tmpdir init:"export $2_TMPDIR="'"${TMPDIR:-${TMP:-/tmp}}"' -- \
101101
'Specify temporary directory [default: $TMPDIR, $TMP or "/tmp"]'
102102

103103
flag KEEP_TMPDIR --keep-tmpdir -- \

lib/libexec/optparser/parser_definition_generated.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# shellcheck shell=sh
22
# Generated by getoptions (BEGIN)
3-
# URL: https://github.com/ko1nksm/getoptions (v2.5.1)
3+
# URL: https://github.com/ko1nksm/getoptions (v3.3.1)
44
export SHELLSPEC_SHELL=''
55
export SHELLSPEC_REQUIRES=''
66
export SHELLSPEC_OPTIONS=''
@@ -374,7 +374,7 @@ optparser_parse() {
374374
--?*=*) OPTARG=$1; shift
375375
eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
376376
;;
377-
--no-*) unset OPTARG ;;
377+
--no-*|--without-*) unset OPTARG ;;
378378
-[sOIeCjfoPETD]?*) OPTARG=$1; shift
379379
eval 'set -- "${OPTARG%"${OPTARG#??}"}" "${OPTARG#??}"' ${1+'"$@"'}
380380
;;
@@ -457,7 +457,7 @@ optparser_parse() {
457457
'--fail-fast'|'--no-fail-fast')
458458
set -- "$1" "$@"
459459
[ ${OPTARG+x} ] && {
460-
case $1 in --no-*) set "noarg" "${1%%\=*}"; break; esac
460+
case $1 in --no-*|--without-*) set "noarg" "${1%%\=*}"; break; esac
461461
[ "${OPTARG:-}" ] && { shift; OPTARG=$2; } || OPTARG='1'
462462
} || OPTARG=''
463463
check_number || { set -- check_number:$? "$1" check_number; break; }
@@ -756,8 +756,7 @@ optparser_parse() {
756756
shift
757757
done
758758
break ;;
759-
[-+]?*)
760-
set "unknown" "$1"; break ;;
759+
[-+]?*) set "unknown" "$1"; break ;;
761760
*)
762761
params="${params} \"\${$(($OPTIND-$#))}\""
763762
esac

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
"lib/cov/kcov/.zshenv",
6262
"lib/ext/ext.sh",
6363
"lib/general.sh",
64-
"lib/getoptions.sh",
6564
"lib/getoptions_abbr.sh",
65+
"lib/getoptions_base.sh",
6666
"lib/getoptions_help.sh",
6767
"lib/libexec.sh",
6868
"lib/libexec/binary.sh",

spec/getoptions_abbr_spec.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# shellcheck shell=sh disable=SC1083,SC2016,SC2286,SC2287,SC2288
1+
# shellcheck shell=sh disable=SC1083,SC2004,SC2016
22

33
Describe "getoptions_abbr()"
4-
Include ./lib/getoptions.sh
4+
Include ./lib/getoptions_base.sh
55
Include ./lib/getoptions_abbr.sh
66

77
parse() {

0 commit comments

Comments
 (0)