-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshell-getopt
463 lines (408 loc) · 10.2 KB
/
shell-getopt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
if [ -z "${__included_shell_getopt-}" ]; then
__included_shell_getopt=1
. shell-error
. shell-quote
. shell-string
### Ignore unknown options.
GETOPT_ALLOW_UNKNOWN=
### Long options may be abbreviated, as long as the abbreviation is not ambiguous.
GETOPT_ALLOW_ABBREV=1
### Allow long options to start with a single `-'. See (getopt -a).
GETOPT_ALLOW_ALTERNATIVE=
OPTERR=1
OPTTYP=
OPTUKN=
OPTOPT=
OPTARG=
OPTIND=1
OPTOFS=
###*** Example ***
### Usage: getopt2 -ab -d 10 -e20 --opt1 --opt4=100 text1 text2
###
### . shell-getopt
### echo Using getoptex to parse arguments:
### while getoptex "a; b; c; d: e. opt1 opt2 opt3 opt4: opt5." "$@"; do
### echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}"
### done
### shift $(($OPTIND-1))
### set -- $@ ${OPTUKN-}
### for arg in "$@"; do
### echo "Non option argument <$arg>"
### done
###
getoptex()
{
[ "$#" -gt 0 ] ||
return 1
[ "${OPTIND-}" -gt 0 ] 2>/dev/null ||
OPTIND=1
[ "$OPTIND" -lt "$#" ] ||
return 1
getoptex_badarg()
{
[ -z "${OPTERR-}" ] ||
message "option requires an argument -- '$OPTOPT'"
OPTARG="$OPTOPT"
OPTOPT='?'
}
getoptex_argument()
{
[ "$OPTTYP" = ':' ] ||
return 0
OPTARG="$1"
### Added test on following argument being an option identified by '-' this way #
### the routine no longer takes options as an argument thus breaking error #
### detection. 2004-04-04 by raphael at oninet dot pt #
[ -n "$OPTARG" ] && [ -n "${OPTARG%%-*}" ] ||
{ getoptex_badarg; return 1; }
OPTARG="${1#[#]}"
OPTIND=$(($OPTIND+1)) ### skip option's argument
}
getoptex_option_long()
{
local p
for p in -- ${GETOPT_ALLOW_ALTERNATIVE:+-}; do
p="$p$OPTOPT"
case "$1" in
$p=*)
[ -n "$OPTTYP" ] ||
{ getoptex_badarg; return 3; }
OPTARG="${1#$p=}"
return 1
;;
"$p")
getoptex_argument "$2" ||
return 3
return 1
;;
esac
done
}
getoptex_option_short()
{
local o="-${1#-${OPTOFS-}}"
case "$o" in
"-$OPTOPT")
OPTOFS=
getoptex_argument "$2" ||
return 3
return 1
;;
-$OPTOPT*)
if [ -z "$OPTTYP" ]; then ### an option with no argument is in a chain of options
OPTOFS="${OPTOFS-}?" ### move to the next option in the chain
OPTIND=$(($OPTIND-1)) ### the chain still has other options
else
OPTOFS=
OPTARG="${o#-$OPTOPT}"
fi
return 1
;;
esac
}
getoptex_option_abbrev()
{
[ "$1" != '--' ] ||
return 0
local opt o i=0 variants='' l='' v='' n="${1%%=*}"
[ -n "${1##*=*}" ] || v="${1#*=}"
shift
for opt; do
opt="${opt%[;.:]}"
[ "${#opt}" -gt 1 ] ||
continue
for o in -- ${GETOPT_ALLOW_ALTERNATIVE:+-}; do
o="$o$opt"
[ -z "${o##$n*}" ] ||
continue
l="--$opt"
if [ "$o" = "$n" ]; then
# Full match was found, so all other variants should be ignored.
variants=
i=1
break 2
else
#local d='--'
#[ -z "${GETOPT_ALLOW_ALTERNATIVE-}" ] || d='-'
#variants="$variants '$d$opt'"
variants="$variants '--$opt'"
i=$(($i+1))
fi
done
done
if [ "$i" -eq 1 ]; then
param="$l${v:+=$v}"
elif [ "$i" -gt 1 ]; then
message "option '$n${v:+=$v}' is ambiguous; possibilities:$variants"
return 1
fi
}
local optlist="${1#;}" param='' value=''
shift $OPTIND
param="$1"
if [ -n "${GETOPT_ALLOW_ABBREV-}" ]; then
if ! getoptex_option_abbrev "$param" $optlist && [ -z "${GETOPT_ALLOW_UNKNOWN-}" ]; then
OPTIND=$(($OPTIND+1))
OPTOPT='?'
OPTARG=
return 2
fi
fi
[ "$#" -lt 2 ] ||
value="#$2"
OPTTYP=
### test whether $param is an option.
if [ "$param" = '--' ]; then
OPTTYP='--'
OPTIND=$(($OPTIND+1))
elif [ "$param" != '-' ] && [ "$param" != "${param#-}" ]; then
OPTIND=$(($OPTIND+1))
local cmd argtype
for opt in $optlist; do
OPTOPT="${opt%[;.:]}"
OPTARG=
OPTTYP=
[ "$OPTTYP" = ';' ] ||
OPTTYP="${opt#$OPTOPT}"
cmd=long
[ "${#OPTOPT}" -gt 1 ] ||
cmd=short
getoptex_option_$cmd "$param" "$value" ||
return $(($?-1))
done
OPTOPT='?'
OPTARG=
if [ -n "${GETOPT_ALLOW_UNKNOWN-}" ]; then
local q_arg
quote_shell_variable q_arg "$param"
OPTUKN="${OPTUKN-} \"$q_arg\""
return 0
fi
message "unrecognized option '$param'"
return 2
fi
OPTOPT='?'
OPTARG=
return 1
}
###*** Example ***
### Usage: getopt1 -ab -d 10 -e20 text1 text2
###
### . shell-getopt
### echo "Using getopt to parse arguments:"
### while getopts "abcd:e." "$@"; do
### echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}"
### done
### shift $(($OPTIND-1))
### set -- $@ ${OPTUKN-}
### for arg in "$@"; do
### echo "Non option argument <$arg>"
### done
###
getopts()
{
local l="$1"
local m= ### mask
local r= ### to store result
fill_mask m "$l" ### create a "???..." mask
while [ -n "$l" ]; do
r="${r:+"$r "}${l%$m}" ### append the first character of $l to $r
l="${l#?}" ### cut the first charecter from $l
m="${m#?}" ### cut one "?" sign from m
### a special character (";", ".", or ":") was found
if [ "${l#[:.;]}" != "$l" ]; then
r="$r${l%$m}" ### append it to $r
l="${l#?}" ### cut the special character from l
m="${m#?}" ### cut one more "?" sign
fi
done
shift
getoptex "$r" ${1:+"$@"} ||
return $?
}
###*** Example ***
### . shell-getopt
### while getsubopt 'rw mode: path: dev:' 'rw,mode=755,path="/zzz xxx",dev=/dev/zzz'; do
### echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}"
### done
###
__getsubopt_arguments=
getsubopt()
{
local l="$2"
if [ -z "$__getsubopt_arguments" ]; then
local ch m=
fill_mask m "$l"
__getsubopt_arguments='--'
while [ -n "$l" ]; do
ch="${l%$m}"
l="${l#?}"
m="${m#?}"
# shellcheck disable=SC2102
# Disable this `Ranges can only match single chars (mentioned due to duplicates)`
# because some shells treat escaped characters differently.
case "$ch" in
,) ch=' --' ;;
[\\\\\`\$\"\ ]) ch="\\$ch" ;;
esac
__getsubopt_arguments="$__getsubopt_arguments$ch"
done
fi
local GETOPT_ALLOW_ABBREV=
eval getoptex "\"$1\"" "$__getsubopt_arguments" ||
return $?
}
. shell-version
GETOPT_POSIXLY_CORRECT=
getopt()
{
__getopt_version()
{
cat <<-EOF
$PROG version $libshell_version
Written by Alexey Gladkov <[email protected]>
Copyright (C) 2008 Alexey Gladkov <[email protected]>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
}
__getopt_usage()
{
cat <<-EOF
Try \`$PROG --help' for more information.
EOF
}
__getopt_help()
{
cat <<-EOF
Usage: $PROG optstring parameters
or: $PROG [options] [--] optstring parameters
or: $PROG [options] -o|--options optstring [options] [--] parameters
Options ignored:
-s, --shell=shell Set shell quoting conventions;
-u, --unqote Do not quote the output;
Options:
-a, --alternative Allow long options starting with single -;
-l, --longoptions=longopts Long options to be recognized;
-n, --name=progname The name under which errors are reported;
-o, --options=optstring Short options to be recognized;
-q, --quiet Disable error reporting;
-Q, --quiet-output No normal output;
-T, --test Test for getopt version;
-V, --version Output version information;
-h, --help This small usage guide.
Report bugs to http://bugzilla.altlinux.org/
EOF
}
local PROG='getopt' OPTIND=1 OPTERR=1 prg='' opts='' lopts='' quiet_output='' alt='' order='' rc
local GETOPT_POSIXLY_CORRECT
while getoptex 'a alternative h help l: longoptions: n: name: o: options: q quiet Q quiet-output s shell T test u unquoted V version' ${1:+"$@"} && rc=0 || rc=$?; do
case "$rc" in
2) __getopt_usage; return 2 ;;
1) break ;;
esac
case "$OPTOPT" in
a|alternative) alt=1 ;;
n|name) prg="$OPTARG" ;;
o|options) opts="$OPTARG " ;;
l|longoptions) lopts="$OPTARG" ;;
q|quiet) OPTERR= ;;
Q|quiet-output) quiet_output=1 ;;
T|test) return 4 ;;
V|version)
__getopt_version
return 0
;;
h|help)
__getopt_help
return 2
;;
esac
done
shift $(($OPTIND-1))
set -- ${1:+"$@"}
if [ -z "${opts##+*}" ]; then
opts="${opts#+}"
GETOPT_POSIXLY_CORRECT=1
elif [ -z "${opts##-*}" ]; then
opts="${opts#-}"
order=1
fi
if [ -z "$opts" ]; then
if [ "$#" -gt 1 ] && [ "${1#-}" = "$1" ] && [ "$1" != '--' ]; then
opts="$1"
shift
else
message "$PROG: missing optstring argument"
__getopt_usage
return 2
fi
fi
opts="${lopts:+$lopts,}$opts"
while :; do
case "$opts" in
*,*) opts="${opts%%,*} ${opts#*,}" ;;
*::*) opts="${opts%%::*}.${opts#*::}" ;;
*) break ;;
esac
done
local OPTIND=1 OPTOFS='' GETOPT_ALLOW_ALTERNATIVE="${alt:+1}"
local ret=0 out=' -- '
! printenv POSIXLY_CORRECT >/dev/null 2>&1 ||
GETOPT_POSIXLY_CORRECT=1
__getopt_out_arg()
{
local q_arg
shift $(($OPTIND-1))
quote_shell_variable q_arg "$1"
[ -z "$order" ] &&
out="${out%% -- *} -- ${out#* -- } \"$q_arg\"" ||
out="${out%% -- *} \"$q_arg\" -- ${out#* -- }"
}
PROG="$prg"
while getoptex "$opts" ${1:+"$@"} && rc=0 || rc=$?; do
case "$rc" in
1)
[ $# -ge $OPTIND ] && [ -z "${GETOPT_POSIXLY_CORRECT-}" ] ||
break
__getopt_out_arg ${1:+"$@"}
OPTIND=$(($OPTIND+1))
[ "$OPTTYP" != '--' ] ||
break
continue
;;
2) ret=1
;;
esac
if [ "$OPTOPT" = '?' ]; then
if [ -n "$GETOPT_ALLOW_UNKNOWN" ]; then
out="${out%% -- *} -- ${out#* -- }$OPTUKN"
OPTUKN=
fi
continue
fi
pfx='-'
[ "${#OPTOPT}" -eq 1 ] ||
pfx='--'
# shellcheck disable=SC2027
# Disable `The surrounding quotes actually unquote this`
# because $OPTARG actually quoted.
out="${out%% -- *} $pfx$OPTOPT${OPTTYP:+ "'"$OPTARG"'"} -- ${out#* -- }"
done
local q_arg
shift $(($OPTIND-1))
set -- ${1:+"$@"}
while [ "$#" -gt 0 ]; do
quote_shell_variable q_arg "$1"
out="${out%% -- *} -- ${out#* -- } \"$q_arg\""
shift
done
[ -n "$quiet_output" ] ||
printf '%s\n' "$out${OPTUKN:+$OPTUKN}"
return $ret
}
fi #__included_shell_getopt