-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubcommander
executable file
·192 lines (166 loc) · 4.38 KB
/
subcommander
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
#!/bin/sh
set -eu
export APPLICATION="${APPLICATION:-$0}"
export SUBCOMMANDS="${SUBCOMMANDS:-./subcommands}"
export DESCRIPTION="${DESCRIPTION:-$APPLICATION}"
export HELP_COLUMN_WIDTH="${HELP_COLUMN_WIDTH:-15}"
export HOOK="${HOOK:-}"
export VERSION="${VERSION:-unknown}"
subcommander() {
scope="$1"
subcmd="${2:-}"
newscope="$scope/$subcmd"
code=0
shift
if [ -n "$subcmd" ] && [ -d "$newscope" ]; then # if the subcmd is a directory
shift && subcommander "$newscope" "$@" # recurse into it
fi
if [ -n "$subcmd" ] && [ -f "$newscope" ]; then # if given a subcmd is a file
if is_help "${2:-}"; then # if the subcmd is an indication for help info
script_to_help "$newscope" # display help info
elif is_version "${2:-}"; then # if the subcmd is an indication for version info
display_version # display version info
else # otherwise execute the file
shift && "$newscope" "$@" # pass along the rest of a params
fi
exit $? # exit with the status of one of the above blocks
fi
is_version "$subcmd" && display_version
if ! is_help "$subcmd"; then
[ -z "$subcmd" ] || warn "Unknown subcommand: $(scope_to_subcommand "$newscope")"
code=1
fi
usage "$scope/"
exit "$code"
}
warn() {
>&2 printf '\e[1;33m%s\e[0m\n' "$*"
}
subcmd_row() {
echo "$1\`\`$2"
}
usage() {
draw_row() {
printf "%-${HELP_COLUMN_WIDTH}s │ %-${HELP_COLUMN_WIDTH}s │ %s\n" "$@"
}
suffix=''
[ -n "$1" ] || suffix=' '
echo 'Usage:'
echo " $APPLICATION $(scope_to_subcommand "$1")$suffix<SUBCOMMAND> [<ARGUMENT>...]"
echo
echo "$DESCRIPTION"
echo
draw_row SUBCOMMAND ALIASES DESCRIPTION
IFS=$'\n'
subcommands_help "$1" | sort -s -k 1,1 | while read -r line; do
[ -n "$line" ] || continue
draw_row \
"$(echo "$line" | cut -d'`' -f1)" \
"$(echo "$line" | cut -d'`' -f2)" \
"$(echo "$line" | cut -d'`' -f3)"
done
}
# Takes a usage string (eg. '<SERVICE_NAME>') and a description string
print_usage() {
printf 'Usage:\n\t%s\n\n%s\n' "$1" "$2"
}
script_to_usage() {
suffix=''
[ -z "$1" ] || suffix=' '
echo "$APPLICATION $(scope_to_subcommand "$1")$suffix$(extract_from_script 'Usage' "$1")"
}
script_to_help() {
print_usage "$(script_to_usage "$1")" "$(extract_from_script 'Description' "$1")"
}
subcommands_help() {
scope="$1"
out=""
addToOutput() {
name="$1"
aliases="$2"
description="$3"
out="$out
$name\`$aliases\`$description"
}
addAliasToOutput() {
name="$1"
newAlias="$2"
description="$3"
if echo "$out" | grep -E "^$name\`" >/dev/null 2>&1; then
out="$(echo "$out" | sed "s/^$name\`/$name\`$newAlias /")"
else
addToOutput "$@"
fi
}
wrapped_readlink() {
if [ "$(uname)" == 'Darwin' ]; then
readlink "$@"
else
readlink -f "$@"
fi
}
set +f
linkednsd="$(mktemp -d)"
for subcmd in "$scope"/*; do
[ "$(basename "$subcmd")" = 'version' ] && custom_version=true
[ "$(basename "$subcmd")" = 'help' ] && custom_help=true
if [ -L "$subcmd" ]; then
real="$(wrapped_readlink "$subcmd")"
realsubcmd="$(basename "$real")"
desc="$(dir_to_usage "$realsubcmd" | cut -d'`' -f 3)"
[ -d "$real" ] || desc="$(extract_from_script 'Description' "$subcmd")"
touch "$linkednsd/$realsubcmd"
addAliasToOutput "$realsubcmd" \
"$(basename "$subcmd")" \
"$desc"
elif [ -f "$subcmd" ] && ! echo "$out" | grep -E "^$(basename "$subcmd")\`" >/dev/null 2>&1; then
addToOutput "$(basename "$subcmd")" "" "$(extract_from_script 'Description' "$subcmd")"
elif [ -d "$subcmd" ] && [ ! -f "$linkednsd/$(basename "$subcmd")" ] ; then
touch "$linkednsd/$(basename "$subcmd")"
dir_to_usage "$subcmd"
fi
done
set -f
echo "$out"
"${custom_help:-false}" || subcmd_row help 'You are looking at it'
"${custom_version:-false}" || subcmd_row version 'Display version information'
}
extract_from_script() {
grep -E "^(#|//) $1" "$2" | cut -d":" -f2 | sed 's/^[[:space:]]*//' || true
}
scope_to_subcommand() {
echo "$1" | sed -e "s@$SUBCOMMANDS/@@" -e 's@/@ @g' -e 's@/$@@'
}
dir_to_usage() {
subcmd_row "$(basename "$1")" 'Nested subcommand, try giving it --help'
}
is_help() {
case "${1:-}" in
'-h' | '--help' | 'help')
return 0
;;
*)
return 1
;;
esac
}
display_version() {
echo "$VERSION"
exit 0
}
is_version() {
case "${1:-}" in
'-v' | '--version' | 'version')
return 0
;;
*)
return 1
;;
esac
}
maybe_call_hook() {
# shellcheck disable=SC1090
[ -z "$HOOK" ] || . "$HOOK" "$@"
}
maybe_call_hook "$@"
subcommander "$SUBCOMMANDS" "$@"