-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
subcmd.sh
executable file
·78 lines (68 loc) · 1.68 KB
/
subcmd.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
#!/bin/sh
set -eu
# shellcheck disable=SC2034
VERSION=0.1
PROG=${0##*/}
parser_definition() {
setup REST help:usage abbr:true -- \
"Usage: $PROG [global options...] [command] [options...] [arguments...]"
msg -- '' 'getoptions subcommand example' ''
msg -- 'Options:'
flag GLOBAL -g --global -- "global flag"
disp :usage -h --help
disp VERSION --version
msg -- '' 'Commands:'
cmd cmd1 -- "subcommand 1"
cmd cmd2 -- "subcommand 2"
cmd cmd3 -- "subcommand 3"
}
parser_definition_cmd1() {
setup REST help:usage abbr:true -- \
"Usage: $PROG cmd1 [options...] [arguments...]"
msg -- '' 'getoptions subcommand example' ''
msg -- 'Options:'
flag FLAG_A -a --flag-a
disp :usage -h --help
}
parser_definition_cmd2() {
setup REST help:usage abbr:true -- \
"Usage: $PROG cmd2 [options...] [arguments...]"
msg -- '' 'getoptions subcommand example' ''
msg -- 'Options:'
flag FLAG_B -b --flag-b
disp :usage -h --help
}
parser_definition_cmd3() {
setup REST help:usage abbr:true -- \
"Usage: $PROG cmd3 [options...] [arguments...]"
msg -- '' 'getoptions subcommand example' ''
msg -- 'Options:'
flag FLAG_C -c --flag-c
disp :usage -h --help
}
eval "$(getoptions parser_definition) exit 1"
if [ $# -gt 0 ]; then
cmd=$1
shift
case $cmd in
cmd1)
eval "$(getoptions parser_definition_cmd1)"
echo "FLAG_A: $FLAG_A"
;;
cmd2)
eval "$(getoptions parser_definition_cmd2)"
echo "FLAG_B: $FLAG_B"
;;
cmd3)
eval "$(getoptions parser_definition_cmd3)"
echo "FLAG_C: $FLAG_C"
;;
--) # no subcommand, arguments only
esac
fi
echo "GLOBAL: $GLOBAL"
i=0
while [ $# -gt 0 ] && i=$((i + 1)); do
echo "$i: $1"
shift
done