-
Notifications
You must be signed in to change notification settings - Fork 0
/
configopts.sh
624 lines (554 loc) · 15.5 KB
/
configopts.sh
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/usr/bin/env sh
if [ "$(basename "$0")" = "configopts.sh" ]; then
cat << HELPEOF
Usage: . $0
Parse ARGS according to a specification.
Redirects to util-linux's 'enhanced' getopt for most actual option parsing.
Source this file to access the 'parse_args' function and call it as such:
parse_args "\$@" < SPECFILE
or
parse_args "\$@" << EOF
spec...
...
EOF
any function defined in this script that doesn't begin with an underscore
is suitable/meant to be used by a calling script
Note: a pipe cannot be used to pass the spec to this function as it
sets up variables that must be able to be accessed afterwards
HELPEOF
exit 1
fi
ensure_getopt() {
if ! command -v getopt 1>/dev/null 2>&1; then
1>&2 echo "getopt could not be found"
exit 1
fi
error_code=0
(unset GETOPT_COMPATIBLE; getopt --test) || error_code=$?
if [ "$error_code" -ne 4 ]; then
1>&2 echo "installed getopt is not the enhanced version, which is required"
exit 1
fi
}
is_exe_name() {
[ "$(basename "$(ps -p $$ -o exe=)")" = "$1" ]
}
is_bash() {
is_exe_name bash
}
########### CONSTANTS FOR POSIX ###########
NEWLINE=$(printf '\nx')
NEWLINE=${NEWLINE%x}
TAB=$(printf '\t')
########### UTILITIES ###########
repeatchar() { printf "%${2-}s" | tr ' ' "${1:- }"; }
check_varname() {
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_235
# > In the shell command language, a word consisting solely of underscores,
# > digits, and alphabetics from the portable character set.
# > The first character of a name is not a digit.
# not technically specified but empty string is also invalid
while [ "$#" -gt 0 ]; do
case "$1" in "" | [!_a-zA-Z]* | *[!_0-9a-zA-Z]*)
1>&2 echo "'$1': not a valid identifier"
return 1
esac
shift
done
}
if is_bash; then
_read_single_char() {
# previously confirmed that the shell is bash
# shellcheck disable=SC3045
read -rN1 "$1"
# -N rather than -n is in order to read newline as well
}
else
# see https://unix.stackexchange.com/a/464963
_read_single_char() {
gradual_char=
while true; do
# read one byte, using a work around for the fact that command
# substitution strips trailing newline characters.
c=$(dd bs=1 count=1 2> /dev/null; echo x)
c=${c%x}
# fail on EOF
[ -z "$c" ] && return 1
# succeed if a full character has been accumulated in the output
# variable (using "wc -m" to count the number of characters).
gradual_char=${gradual_char}$c
[ "$(printf %s "$gradual_char" | wc -m)" -gt 0 ] && break
done
if [ "$gradual_char" = "'" ]; then
eval "$1=\'"
else
eval "$1='$gradual_char'"
fi
}
fi
readc() {
check_varname "$@" || return 1
while [ "$#" -gt 0 ]; do
_read_single_char "$1" || return 1
shift
done
}
escape_string() {
while [ "$#" -gt 0 ]; do
(
escaped_string=$(printf "%sx" "$1" | sed -e "s/'/'\\\\''/g")
# delimit strings with spaces, but don't append a trailing one
printf "'%s'%s" "${escaped_string%x}" "${2+ }"
)
shift
done
}
# TODO: other options (customise help/version options, etc.)
########### DISPLAYING HELP ###########
_displayoptionshelp() (
MAX_WIDTH=80
MAX_OPTION_SPACE=30
OPTION_DESCRIPTION_GAP=2
MIN_DESCRIPTION_FIRST_LINE_SPACE=20
OPTION_INDENT=2
DESCRIPTION_INDENT=2
formattedoptions=
requiredargexists=
while IFS=';' read -r short long requiredarg description; do
case "$description" in "#"*)
# hidden
continue
esac
if [ -n "$short$long" ] && [ -n "$requiredarg" ]; then
requiredargexists=1
fi
comma=${short:+${long:+, }}
comma=${comma:-${long:+ }}
optionalarg=${requiredarg%"${requiredarg#"?"}"}
requiredarg=${requiredarg#"$optionalarg"}
if [ -n "$short$long" ]; then
argseparator=
test -z "$optionalarg" && argseparator=" "
test -n "$long" && argseparator="="
short=${short:+-$short}
short=${short:-${long:+ }}
long=${long:+--$long}
requiredarg=${requiredarg:+${optionalarg:+[}${argseparator}${requiredarg}${optionalarg:+]}}
else
requiredarg=${requiredarg%...}
fi
formattedoptions=${formattedoptions:+$formattedoptions$NEWLINE}$(repeatchar ' ' "$OPTION_INDENT")${short}${comma}${long}${requiredarg}$TAB${description}
done
test -n "$requiredargexists" && echo 'Mandatory arguments to long options are mandatory for short options too.'
MAX_MAX_OPTION_LENGTH=$((MAX_WIDTH - OPTION_DESCRIPTION_GAP - MIN_DESCRIPTION_FIRST_LINE_SPACE))
max_option_length="$(echo "$formattedoptions" |
awk -F "$TAB" -v max_maxlen="$MAX_MAX_OPTION_LENGTH" '
max_maxlen >= length($1) && length($1) > maxlen {
maxlen = length($1)
}
END {
print maxlen
}
'
)"
if [ "$MAX_OPTION_SPACE" -gt "$((max_option_length + OPTION_DESCRIPTION_GAP))" ]; then
MAX_OPTION_SPACE=$((max_option_length + OPTION_DESCRIPTION_GAP))
fi
MAX_OPTION_LENGTH=$((MAX_OPTION_SPACE - OPTION_DESCRIPTION_GAP))
echo "$formattedoptions" |
while IFS=$TAB read -r option description; do
if [ "${#option}" -gt "$MAX_MAX_OPTION_LENGTH" ]; then
# start description on next line
optionhelp=$option$NEWLINE$(repeatchar ' ' "$MAX_OPTION_LENGTH")
option_length=$MAX_OPTION_SPACE
else
optionhelp=$(printf "%-${MAX_OPTION_LENGTH}s" "$option")
option_length=${#optionhelp}
fi
placeholder=$(repeatchar '@' "$((OPTION_DESCRIPTION_GAP + 1))")
firstline=$(repeatchar ' ' "$((option_length - 2))")${placeholder}
secondline=$(printf "%-$((MAX_OPTION_SPACE + DESCRIPTION_INDENT))s" '')$description
fmteddescription=$(echo "$firstline$NEWLINE$secondline" | fmt -w "$MAX_WIDTH" -t)
fulloptionhelp=${optionhelp}$(repeatchar ' ' "$((OPTION_DESCRIPTION_GAP - 1))")${fmteddescription#*"$placeholder"}
echo "$fulloptionhelp"
done
)
_displayusagehelp() (
allusages=${customusages:+${customusages#"$NEWLINE"}}
# an extra newline shows that a usage exists even if it is empty
usage_exists=${customusages:+${customusages%"$alluages"}}
if [ -z "${disabledefaultusage-}" ]; then
positional_args=
while IFS=';' read -r short long requiredarg description; do
if [ -z "${short}${long}" ]; then
beforemulti=${requiredarg%...}
multi=${requiredarg#"$beforemulti"}
case "$beforemulti" in "?"*)
beforemulti=[${beforemulti#"?"}]
esac
positional_args=${positional_args:+$positional_args }$beforemulti$multi
fi
done
allusages=$positional_args${allusages:+$NEWLINE$allusages}
usage_exists=1
fi
if [ -n "$usage_exists$allusages" ]; then
first=
printf '%s\n' "$allusages" |
while read -r usage; do
if [ -z "$first" ]; then
prefix="Usage: $programname [OPTION]..."
first=1
else
prefix=" or: $programname [OPTION]..."
fi
echo "$prefix $usage"
done
fi
)
_displaydescriptionhelp() (
test -n "${helpdescription-}" && echo "$helpdescription"
)
_displayextrainfohelp() (
test -n "${helpextrainfo-}" && echo "$NEWLINE$helpextrainfo"
)
_displayposthelp() (
test -n "${helpfooter-}" && echo "$NEWLINE$helpfooter"
)
_displayhelp() (
printed=
usages=$(echo "$alloptions" | _displayusagehelp)
if [ -n "$usages" ]; then
echo "$usages"
printed=1
fi
( _displaydescriptionhelp || test -n "$printed" ) &&
# add gap if necessary
echo
echo "$alloptions" | _displayoptionshelp
_displayextrainfohelp
_displayposthelp
)
_displayversion() {
printf %s "$versioninformation"
}
########### PROCESSING SPEC ###########
_processoption() {
short=
case "$1" in ?,*)
short=${1%,*}
esac
rest=${1#"$short,"}
long=${rest%%=*}
requiredarg=${rest#"$long"}
case "$requiredarg" in
=) requiredarg="ARG" ;;
="?") requiredarg="?ARG" ;;
=...) requiredarg="ARG..." ;;
="?"...) requiredarg="?ARG..." ;;
=*) requiredarg="${requiredarg#=}" ;;
esac
shift
description=$*
optionalarg=${requiredarg%"${requiredarg#"?"}"}
argrequirement=${requiredarg:+:}${optionalarg:+:}
test -n "$short" && shortoptions="${shortoptions}${short}${argrequirement}"
test -n "$long" && longoptions="${longoptions},${long}${argrequirement}"
alloptions="${alloptions:+$alloptions$NEWLINE}${short};${long};${requiredarg};${description}"
}
_processusage() {
customusages=${customusages-}$NEWLINE$1
}
_processline() {
case "$1" in
"programname")
programname=$2
;;
"option")
# shellcheck disable=SC2086
_processoption $2
;;
"disable-default-help")
disabledefaulthelp=true
;;
"usage")
_processusage "$2"
;;
"disable-default-usage")
disabledefaultusage=true
;;
"description")
helpdescription=$2
;;
"extrainfo")
helpextrainfo=$2
;;
"footer")
helpfooter=$2
;;
"version")
versioninformation=$2
;;
esac
}
_processspec() {
currentcommand=
currentargs=
while readc firstchar; do
# readc sets $firstchar
# shellcheck disable=SC2154
case "$firstchar" in
(@)
_processline "$currentcommand" "$currentargs"
read -r currentcommand currentargs
;;
("#") ;;
("$NEWLINE")
currentargs=${currentargs:+$currentargs$NEWLINE}
;;
(*)
IFS= read -r extraargs
[ "$firstchar" = "\\" ] && firstchar=
currentargs=${currentargs:+$currentargs$NEWLINE}$firstchar$extraargs
;;
esac
done
_processline "$currentcommand" "$currentargs"
if [ -z "${programname+x}" ]; then
calc_better0
programname=$better0
fi
}
########## PARSING ARGS ##########
# possibly shouldn't be 'public' but I can see it getting some use
getoptioninfo() {
echo "$alloptions" |
while IFS=';' read -r short long requiredarg description; do
if [ "$1" = "-$short" ] || [ "$1" = "--$long" ]; then
echo "${short};${long};${requiredarg}"
return 0
fi
done
return 1
}
optionhasrequiredarg() (
# won't work correctly if requiredarg is just newlines
# as they'll be stripped but ????
optioninfo=$(getoptioninfo "$1")
test -n "${optioninfo#*;*;}"
return
)
parse_args() {
ensure_getopt
shortoptions=h
longoptions=help
alloptions=
_processspec </dev/stdin
if [ -z "${disabledefaulthelp-}" ]; then
_processoption "h,help" "display this help and exit"
fi
if [ -n "${versioninformation-}" ]; then
_processoption "v,version" "output version information and exit"
fi
ARGS="$(
unset GETOPT_COMPATIBLE
getopt \
--shell sh \
--name "$programname" \
--options "$shortoptions" \
--longoptions "$longoptions" \
-- "$@"
)" || {
1>&2 echo "Try '$programname --help' for more information"
exit 1
}
eval "set -- ${ARGS}"
named_args=
positional_args=
named_args_finished=false
while true; do
option=${1-}
shift || break
if $named_args_finished; then
positional_args="$positional_args $(escape_string "$option")"
continue
fi
case "$option" in
(--) named_args_finished=true ;;
(-? | --*)
if [ -z "${disabledefaulthelp-}" ]; then
case "$option" in -h | --help)
_displayhelp
exit 0
esac
fi
if [ -n "${versioninformation-}" ]; then
case "$option" in -v | --version)
_displayversion
exit 0
esac
fi
if optionhasrequiredarg "$option"; then
optionarg=$1
shift
else
optionarg=
fi
named_args="$named_args $(escape_string "$option" "$optionarg")"
;;
(*)
1>&2 echo 'should not be possible :/ (provided getopt may be invalid)'
exit 1
;;
esac
done
}
########## READING OPTIONS/ARGS ##########
_removequotedarg() {
sed -e "
# combine all lines into a single one:
:loop;
\$! { # if not last line
N; # append next line to current
b loop; # jump to :loop
};
# match intitial whitespace and the first quoted string,
# removing them
s/^[[:blank:]]*'[^']*\('\\\''[^']*\)*'//;
"
# s/
# ^[[:blank:]]*
# '
# [^']*
# \(
# '\\\''
# [^']*
# \)*
# '
# /\1/
}
_extractquotedarg() {
sed -e "
# combine all lines into a single one:
:loop;
\$! { # if not last line
N; # append next line to current
b loop; # jump to :loop
};
# match intitial whitespace then capture the first quoted string
# and match the rest of the text, replacing it with the capture
s/^[[:blank:]]*\('[^']*\('\\\''[^']*\)*'\).*/\1/;
"
# s/
# ^[[:blank:]]*
# \(
# '
# [^']*
# \(
# '\\\''
# [^']*
# \)*
# '
# \)
# .*
# /\1/
}
readoption() {
if [ "$#" -ne 2 ]; then
1>&2 echo "readoption: exactly 2 args required but $# were supplied"
return 1
fi
# no trailing whitespace so if it's empty it's empty
test -z "$named_args" && return 1
check_varname "$@"
eval "$1=$(echo "$named_args" | _extractquotedarg)"
named_args=$(echo "$named_args" | _removequotedarg)
eval "$2=$(echo "$named_args" | _extractquotedarg)"
named_args=$(echo "$named_args" | _removequotedarg)
}
readpositionalarg() {
if [ "$#" -ne 1 ]; then
1>&2 echo "readpositionalarg: exactly 1 arg required but $# were supplied"
return 1
fi
# no trailing whitespace so if it's empty it's empty
test -z "$positional_args" && return 1
check_varname "$@"
eval "$1=$(echo "$positional_args" | _extractquotedarg)"
positional_args=$(echo "$positional_args" | _removequotedarg)
}
readrequiredpositionalarg() {
if [ "$#" -ne 1 ]; then
1>&2 echo "readrequiredpositionalarg: exactly 1 arg required but $# were supplied"
return 1
fi
if ! readpositionalarg "$1"; then
echoerr "missing $1 operand"
tryhelpexit
fi
}
readexactpositionalargs() {
tryhelpexit=
while [ "$#" -gt 0 ]; do
if ! readpositionalarg "$1"; then
echoerr "missing $1 operand"
tryhelpexit=true
fi
shift
done
if readpositionalarg extra_arg; then
# readpositionalarg sets $extra_arg
# (idk why shellcheck doesn't pick
# this up automatically tbh)
# shellcheck disable=SC2154
echoerr "extra argument recieved: '$extra_arg'"
tryhelpexit=true
fi
test -z "$tryhelpexit" || tryhelpexit
}
readremainingpositionalargs() {
if ! is_bash; then
1>&2 echo "readremainingpositionalargs: function only available in bash"
return 1
fi
if [ "$#" -ne 1 ]; then
1>&2 echo "readremainingpositionalargs: exactly 1 arg required but $# were supplied"
return 1
fi
check_varname "$@"
eval "$1=($positional_args)"
positional_args=
}
########## EXTRA PUBLIC UTILITIES ##########
# preferably I'd simulate the behaviour of coreutils:
# `cp --help` -> `Usage: cp ...`
# `$(which cp) --help` -> `Usage: /.../cp ...`
# but I just can't hack it :/
# use a shorter $0 if possible
calc_better0() {
test -n "${better0-}" && return 0
scriptname=$(basename "$0")
if scriptinPATH=$(command -v "$scriptname") && [ "$(realpath "$scriptinPATH")" = "$(realpath "$0")" ]; then
# scriptname is in PATH and it points to the same place as the
# one that is currently running so it's fine to use it instead
better0=$scriptname
else
better0=$0
fi
}
get_programname() {
if [ -z "${programname+x}" ]; then
calc_better0
printf %s "$better0"
else
printf %s "$programname"
fi
}
echoerr() {
>&2 get_programname
>&2 echo ": ${1-an error occured}"
}
tryhelpexit() {
>&2 echo "Try '$(get_programname) --help' for more information"
exit 1
}