darwin-shell 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- darwin_shell/__init__.py +34 -0
- darwin_shell/cli.py +140 -0
- darwin_shell/share/bootstrap.sh +74 -0
- darwin_shell/share/checkpoint.sh +437 -0
- darwin_shell/share/cli.sh +555 -0
- darwin_shell/share/core.sh +214 -0
- darwin_shell/share/datetime.sh +367 -0
- darwin_shell/share/env.sh +322 -0
- darwin_shell/share/file.sh +303 -0
- darwin_shell/share/i18n.sh +181 -0
- darwin_shell/share/json.sh +258 -0
- darwin_shell/share/locale/zh_CN.sh +456 -0
- darwin_shell/share/lock.sh +414 -0
- darwin_shell/share/log.sh +317 -0
- darwin_shell/share/notify.sh +576 -0
- darwin_shell/share/process.sh +394 -0
- darwin_shell/share/sched.sh +360 -0
- darwin_shell/share/stats.sh +540 -0
- darwin_shell/share/terminal.sh +226 -0
- darwin_shell-0.1.0.dist-info/METADATA +208 -0
- darwin_shell-0.1.0.dist-info/RECORD +25 -0
- darwin_shell-0.1.0.dist-info/WHEEL +5 -0
- darwin_shell-0.1.0.dist-info/entry_points.txt +2 -0
- darwin_shell-0.1.0.dist-info/licenses/LICENSE +21 -0
- darwin_shell-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# darwin-shell cli: declarative, spec-driven argument parsing.
|
|
3
|
+
# A spec is declared once with flag/value/optional_value/mutex calls; run then
|
|
4
|
+
# parses arguments, validates them and writes results into caller-owned
|
|
5
|
+
# variables. Unrecognised arguments are preserved for downstream passthrough.
|
|
6
|
+
# No business flags, date rules or calendar logic live in this module.
|
|
7
|
+
|
|
8
|
+
# Dependencies sit below this module. Load them before the guard so a failed
|
|
9
|
+
# dependency is retryable on the next source attempt.
|
|
10
|
+
if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
|
|
11
|
+
if declare -F dsh::use >/dev/null 2>&1; then
|
|
12
|
+
dsh::use i18n || return $?
|
|
13
|
+
else
|
|
14
|
+
_dsh_private_cli_self="${BASH_SOURCE[0]}"
|
|
15
|
+
source "${_dsh_private_cli_self%/*}/i18n.sh" || return $?
|
|
16
|
+
unset _dsh_private_cli_self
|
|
17
|
+
fi
|
|
18
|
+
fi
|
|
19
|
+
if ! declare -F dsh::core::fail >/dev/null 2>&1; then
|
|
20
|
+
if declare -F dsh::use >/dev/null 2>&1; then
|
|
21
|
+
dsh::use core || return $?
|
|
22
|
+
else
|
|
23
|
+
_dsh_private_cli_self="${BASH_SOURCE[0]}"
|
|
24
|
+
source "${_dsh_private_cli_self%/*}/core.sh" || return $?
|
|
25
|
+
unset _dsh_private_cli_self
|
|
26
|
+
fi
|
|
27
|
+
fi
|
|
28
|
+
|
|
29
|
+
if [[ -n "${_dsh_private_cli_sourced:-}" ]]; then
|
|
30
|
+
return 0
|
|
31
|
+
fi
|
|
32
|
+
_dsh_private_cli_sourced=1
|
|
33
|
+
readonly _dsh_private_cli_sourced
|
|
34
|
+
|
|
35
|
+
# Spec metadata. Keys are "<spec>:<option>" for per-option data and the bare
|
|
36
|
+
# spec name for ordered option lists and mutex groups.
|
|
37
|
+
declare -gA _dsh_private_cli_order=()
|
|
38
|
+
declare -gA _dsh_private_cli_kind=()
|
|
39
|
+
declare -gA _dsh_private_cli_byname=()
|
|
40
|
+
declare -gA _dsh_private_cli_shortname=()
|
|
41
|
+
declare -gA _dsh_private_cli_into=()
|
|
42
|
+
declare -gA _dsh_private_cli_check=()
|
|
43
|
+
declare -gA _dsh_private_cli_helptxt=()
|
|
44
|
+
declare -gA _dsh_private_cli_required=()
|
|
45
|
+
declare -gA _dsh_private_cli_default=()
|
|
46
|
+
declare -gA _dsh_private_cli_mutex=()
|
|
47
|
+
declare -gA _dsh_private_cli_seen=()
|
|
48
|
+
|
|
49
|
+
# The only output array for unrecognised / passthrough arguments. Callers take
|
|
50
|
+
# it via dsh::cli::remaining_append and clear it with spec creation or a new
|
|
51
|
+
# run. A single owned array avoids the shared-global juggling in the old code.
|
|
52
|
+
declare -ga DSH_CLI_REMAINING=()
|
|
53
|
+
|
|
54
|
+
# Variables that must never be written by a parsed option: they can change
|
|
55
|
+
# shell or process behaviour before the caller regains control.
|
|
56
|
+
readonly _dsh_private_cli_forbidden_vars=' BASH_ENV ENV SHELLOPTS BASHOPTS IFS CDPATH PROMPT_COMMAND LD_PRELOAD '
|
|
57
|
+
|
|
58
|
+
_dsh_private_cli_help() {
|
|
59
|
+
case "${1:-}" in
|
|
60
|
+
spec)
|
|
61
|
+
dsh::i18n::t 'Usage: dsh::cli::spec <name> [--help]'
|
|
62
|
+
printf '\n'
|
|
63
|
+
dsh::i18n::t 'Create or reset a named option spec. No filesystem access.'
|
|
64
|
+
;;
|
|
65
|
+
flag)
|
|
66
|
+
dsh::i18n::t 'Usage: dsh::cli::flag <spec> <name> [--short S] [--into VAR] [--help MSGID]'
|
|
67
|
+
printf '\n'
|
|
68
|
+
dsh::i18n::t 'Declare a boolean option; parsing writes true into VAR.'
|
|
69
|
+
;;
|
|
70
|
+
value)
|
|
71
|
+
dsh::i18n::t 'Usage: dsh::cli::value <spec> <name> [--short S] [--into VAR] [--required] [--check FN] [--default D] [--help MSGID]'
|
|
72
|
+
printf '\n'
|
|
73
|
+
dsh::i18n::t 'Declare an option that requires a value. FN is a caller-supplied validator.'
|
|
74
|
+
;;
|
|
75
|
+
optional_value)
|
|
76
|
+
dsh::i18n::t 'Usage: dsh::cli::optional_value <spec> <name> [--into VAR] [--default D] [--help MSGID]'
|
|
77
|
+
printf '\n'
|
|
78
|
+
dsh::i18n::t 'Declare an option whose value is optional; use --default when absent.'
|
|
79
|
+
;;
|
|
80
|
+
mutex)
|
|
81
|
+
dsh::i18n::t 'Usage: dsh::cli::mutex <spec> <name1> <name2> [...]'
|
|
82
|
+
printf '\n'
|
|
83
|
+
dsh::i18n::t 'Declare a mutually exclusive group. Repeated calls add groups.'
|
|
84
|
+
;;
|
|
85
|
+
help)
|
|
86
|
+
dsh::i18n::t 'Usage: dsh::cli::help <spec>'
|
|
87
|
+
printf '\n'
|
|
88
|
+
dsh::i18n::t 'Print the generated help table for a spec to stdout.'
|
|
89
|
+
;;
|
|
90
|
+
run)
|
|
91
|
+
dsh::i18n::t 'Usage: dsh::cli::run <spec> [--] <args...>'
|
|
92
|
+
printf '\n'
|
|
93
|
+
dsh::i18n::t 'Parse arguments by spec. Returns 2 on help or usage errors; never exits.'
|
|
94
|
+
;;
|
|
95
|
+
remaining_append)
|
|
96
|
+
dsh::i18n::t 'Usage: dsh::cli::remaining_append <array-name>'
|
|
97
|
+
printf '\n'
|
|
98
|
+
dsh::i18n::t 'Append DSH_CLI_REMAINING to a caller-owned array via nameref.'
|
|
99
|
+
;;
|
|
100
|
+
*)
|
|
101
|
+
dsh::i18n::t 'dsh::cli: no help for %1$s' "${1:-<empty>}" >&2
|
|
102
|
+
return "$DSH_RC_USAGE"
|
|
103
|
+
;;
|
|
104
|
+
esac
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_dsh_private_cli_fail() {
|
|
108
|
+
dsh::core::fail "$DSH_RC_USAGE" "$@"
|
|
109
|
+
return $?
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
_dsh_private_cli_valid_spec() {
|
|
113
|
+
[[ "${1:-}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_dsh_private_cli_valid_option() {
|
|
117
|
+
[[ "${1:-}" =~ ^[A-Za-z][A-Za-z0-9_-]*$ ]]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
_dsh_private_cli_valid_var() {
|
|
121
|
+
local var="${1:-}"
|
|
122
|
+
[[ "$var" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || return 1
|
|
123
|
+
[[ "$_dsh_private_cli_forbidden_vars" != *" $var "* ]]
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
_dsh_private_cli_ensure_spec() {
|
|
127
|
+
if ! _dsh_private_cli_valid_spec "$1"; then
|
|
128
|
+
_dsh_private_cli_fail 'dsh::cli: invalid spec name: %1$s' "${1:-<empty>}"
|
|
129
|
+
return $?
|
|
130
|
+
fi
|
|
131
|
+
if [[ -z "${_dsh_private_cli_order[$1]+x}" ]]; then
|
|
132
|
+
return 0
|
|
133
|
+
fi
|
|
134
|
+
return 0
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
_dsh_private_cli_register() {
|
|
138
|
+
local kind="$1" spec="$2" name="$3"
|
|
139
|
+
local short='' into='' check='' default='' help='' required=0
|
|
140
|
+
shift 3
|
|
141
|
+
while (( $# )); do
|
|
142
|
+
case "$1" in
|
|
143
|
+
--short)
|
|
144
|
+
if (( $# < 2 )); then
|
|
145
|
+
_dsh_private_cli_fail 'dsh::cli: --short requires a value'
|
|
146
|
+
return $?
|
|
147
|
+
fi
|
|
148
|
+
short="$2"
|
|
149
|
+
shift 2
|
|
150
|
+
;;
|
|
151
|
+
--into)
|
|
152
|
+
if (( $# < 2 )); then
|
|
153
|
+
_dsh_private_cli_fail 'dsh::cli: --into requires a value'
|
|
154
|
+
return $?
|
|
155
|
+
fi
|
|
156
|
+
into="$2"
|
|
157
|
+
shift 2
|
|
158
|
+
;;
|
|
159
|
+
--check)
|
|
160
|
+
if (( $# < 2 )); then
|
|
161
|
+
_dsh_private_cli_fail 'dsh::cli: --check requires a value'
|
|
162
|
+
return $?
|
|
163
|
+
fi
|
|
164
|
+
check="$2"
|
|
165
|
+
shift 2
|
|
166
|
+
;;
|
|
167
|
+
--default)
|
|
168
|
+
if (( $# < 2 )); then
|
|
169
|
+
_dsh_private_cli_fail 'dsh::cli: --default requires a value'
|
|
170
|
+
return $?
|
|
171
|
+
fi
|
|
172
|
+
default="$2"
|
|
173
|
+
shift 2
|
|
174
|
+
;;
|
|
175
|
+
--help)
|
|
176
|
+
if (( $# < 2 )); then
|
|
177
|
+
_dsh_private_cli_fail 'dsh::cli: --help requires a value'
|
|
178
|
+
return $?
|
|
179
|
+
fi
|
|
180
|
+
help="$2"
|
|
181
|
+
shift 2
|
|
182
|
+
;;
|
|
183
|
+
--required)
|
|
184
|
+
required=1
|
|
185
|
+
shift
|
|
186
|
+
;;
|
|
187
|
+
*)
|
|
188
|
+
_dsh_private_cli_fail 'dsh::cli: unknown option: %1$s' "$1"
|
|
189
|
+
return $?
|
|
190
|
+
;;
|
|
191
|
+
esac
|
|
192
|
+
done
|
|
193
|
+
|
|
194
|
+
_dsh_private_cli_ensure_spec "$spec" || return $?
|
|
195
|
+
if ! _dsh_private_cli_valid_option "$name"; then
|
|
196
|
+
_dsh_private_cli_fail 'dsh::cli: invalid option name: %1$s' "${name:-<empty>}"
|
|
197
|
+
return $?
|
|
198
|
+
fi
|
|
199
|
+
if [[ -n "$into" ]] && ! _dsh_private_cli_valid_var "$into"; then
|
|
200
|
+
_dsh_private_cli_fail 'dsh::cli: invalid target variable: %1$s' "$into"
|
|
201
|
+
return $?
|
|
202
|
+
fi
|
|
203
|
+
if [[ "$short" == -* ]] || [[ "$short" == *,* ]]; then
|
|
204
|
+
_dsh_private_cli_fail 'dsh::cli: invalid short name: %1$s' "$short"
|
|
205
|
+
return $?
|
|
206
|
+
fi
|
|
207
|
+
if [[ -n "$check" ]] && [[ "$kind" != value ]]; then
|
|
208
|
+
_dsh_private_cli_fail 'dsh::cli: --check is only valid for value options'
|
|
209
|
+
return $?
|
|
210
|
+
fi
|
|
211
|
+
|
|
212
|
+
local key="$spec:$name"
|
|
213
|
+
if [[ -n "${_dsh_private_cli_kind[$key]+x}" ]]; then
|
|
214
|
+
_dsh_private_cli_fail 'dsh::cli: option already declared: %1$s' "$name"
|
|
215
|
+
return $?
|
|
216
|
+
fi
|
|
217
|
+
if [[ -n "$short" ]]; then
|
|
218
|
+
local short_key="$spec:$short"
|
|
219
|
+
if [[ -n "${_dsh_private_cli_byname[$short_key]+x}" ]]; then
|
|
220
|
+
_dsh_private_cli_fail 'dsh::cli: short name already declared: %1$s' "$short"
|
|
221
|
+
return $?
|
|
222
|
+
fi
|
|
223
|
+
_dsh_private_cli_byname[$short_key]="$name"
|
|
224
|
+
fi
|
|
225
|
+
|
|
226
|
+
_dsh_private_cli_byname[$key]="$name"
|
|
227
|
+
_dsh_private_cli_shortname[$key]="$short"
|
|
228
|
+
_dsh_private_cli_kind[$key]="$kind"
|
|
229
|
+
_dsh_private_cli_into[$key]="$into"
|
|
230
|
+
_dsh_private_cli_check[$key]="$check"
|
|
231
|
+
_dsh_private_cli_default[$key]="$default"
|
|
232
|
+
_dsh_private_cli_helptxt[$key]="$help"
|
|
233
|
+
_dsh_private_cli_required[$key]="$required"
|
|
234
|
+
if [[ -z "${_dsh_private_cli_order[$spec]:-}" ]]; then
|
|
235
|
+
_dsh_private_cli_order[$spec]="$name"
|
|
236
|
+
else
|
|
237
|
+
_dsh_private_cli_order[$spec]+=" $name"
|
|
238
|
+
fi
|
|
239
|
+
return 0
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
_dsh_private_cli_write() {
|
|
243
|
+
local into="$1" value="$2"
|
|
244
|
+
[[ -n "$into" ]] || return 0
|
|
245
|
+
printf -v "$into" '%s' "$value"
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_dsh_private_cli_reset_spec() {
|
|
249
|
+
local spec="$1" array key
|
|
250
|
+
# Reset must remove per-option metadata as well as the order list; otherwise
|
|
251
|
+
# re-declaring a spec would keep stale options from the previous definition.
|
|
252
|
+
for array in order kind byname shortname into check helptxt required default mutex seen; do
|
|
253
|
+
local -n target="_dsh_private_cli_${array}"
|
|
254
|
+
for key in "${!target[@]}"; do
|
|
255
|
+
if [[ "$key" == "$spec" ]] || [[ "$key" == "$spec:"* ]]; then
|
|
256
|
+
unset "target[$key]"
|
|
257
|
+
fi
|
|
258
|
+
done
|
|
259
|
+
done
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
# Public: create or reset a named spec.
|
|
263
|
+
dsh::cli::spec() {
|
|
264
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
265
|
+
_dsh_private_cli_help spec
|
|
266
|
+
return $?
|
|
267
|
+
fi
|
|
268
|
+
if (( $# != 1 )); then
|
|
269
|
+
_dsh_private_cli_fail 'Usage: dsh::cli::spec <name>'
|
|
270
|
+
return $?
|
|
271
|
+
fi
|
|
272
|
+
local spec="$1"
|
|
273
|
+
_dsh_private_cli_valid_spec "$spec" || {
|
|
274
|
+
_dsh_private_cli_fail 'dsh::cli: invalid spec name: %1$s' "$spec"
|
|
275
|
+
return $?
|
|
276
|
+
}
|
|
277
|
+
_dsh_private_cli_reset_spec "$spec"
|
|
278
|
+
_dsh_private_cli_order[$spec]=''
|
|
279
|
+
_dsh_private_cli_mutex[$spec]=''
|
|
280
|
+
DSH_CLI_REMAINING=()
|
|
281
|
+
return 0
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
# Public: declare a boolean option.
|
|
285
|
+
dsh::cli::flag() {
|
|
286
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
287
|
+
_dsh_private_cli_help flag
|
|
288
|
+
return $?
|
|
289
|
+
fi
|
|
290
|
+
if (( $# < 2 )); then
|
|
291
|
+
_dsh_private_cli_fail 'Usage: dsh::cli::flag <spec> <name> [options]'
|
|
292
|
+
return $?
|
|
293
|
+
fi
|
|
294
|
+
_dsh_private_cli_register flag "$1" "$2" "${@:3}"
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
# Public: declare an option that requires a value.
|
|
298
|
+
dsh::cli::value() {
|
|
299
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
300
|
+
_dsh_private_cli_help value
|
|
301
|
+
return $?
|
|
302
|
+
fi
|
|
303
|
+
if (( $# < 2 )); then
|
|
304
|
+
_dsh_private_cli_fail 'Usage: dsh::cli::value <spec> <name> [options]'
|
|
305
|
+
return $?
|
|
306
|
+
fi
|
|
307
|
+
_dsh_private_cli_register value "$1" "$2" "${@:3}"
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
# Public: declare an option with an optional value.
|
|
311
|
+
dsh::cli::optional_value() {
|
|
312
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
313
|
+
_dsh_private_cli_help optional_value
|
|
314
|
+
return $?
|
|
315
|
+
fi
|
|
316
|
+
if (( $# < 2 )); then
|
|
317
|
+
_dsh_private_cli_fail 'Usage: dsh::cli::optional_value <spec> <name> [options]'
|
|
318
|
+
return $?
|
|
319
|
+
fi
|
|
320
|
+
_dsh_private_cli_register optional_value "$1" "$2" "${@:3}"
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
# Public: declare a mutually exclusive group.
|
|
324
|
+
dsh::cli::mutex() {
|
|
325
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
326
|
+
_dsh_private_cli_help mutex
|
|
327
|
+
return $?
|
|
328
|
+
fi
|
|
329
|
+
if (( $# < 3 )); then
|
|
330
|
+
_dsh_private_cli_fail 'dsh::cli::mutex requires a spec and at least two names'
|
|
331
|
+
return $?
|
|
332
|
+
fi
|
|
333
|
+
local spec="$1" name group
|
|
334
|
+
_dsh_private_cli_ensure_spec "$spec" || return $?
|
|
335
|
+
for name in "${@:2}"; do
|
|
336
|
+
if [[ -z "${_dsh_private_cli_kind[$spec:$name]+x}" ]]; then
|
|
337
|
+
_dsh_private_cli_fail 'dsh::cli: option not declared: %1$s' "$name"
|
|
338
|
+
return $?
|
|
339
|
+
fi
|
|
340
|
+
done
|
|
341
|
+
group="$*"
|
|
342
|
+
if [[ -z "${_dsh_private_cli_mutex[$spec]:-}" ]]; then
|
|
343
|
+
_dsh_private_cli_mutex[$spec]="$group"
|
|
344
|
+
else
|
|
345
|
+
_dsh_private_cli_mutex[$spec]+="|$group"
|
|
346
|
+
fi
|
|
347
|
+
return 0
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
# Public: print a generated help table for a spec.
|
|
351
|
+
dsh::cli::help() {
|
|
352
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
353
|
+
_dsh_private_cli_help help
|
|
354
|
+
return $?
|
|
355
|
+
fi
|
|
356
|
+
local spec="${1:-}"
|
|
357
|
+
if [[ -z "$spec" ]] || [[ -z "${_dsh_private_cli_order[$spec]+x}" ]]; then
|
|
358
|
+
_dsh_private_cli_fail 'dsh::cli: unknown spec: %1$s' "${spec:-<empty>}"
|
|
359
|
+
return $?
|
|
360
|
+
fi
|
|
361
|
+
local name key kind short desc label
|
|
362
|
+
dsh::i18n::t 'Usage: %1$s [options] [-- <passthrough...>]' "$spec"
|
|
363
|
+
printf '\n'
|
|
364
|
+
# shellcheck disable=SC2206
|
|
365
|
+
local -a order=(${_dsh_private_cli_order[$spec]})
|
|
366
|
+
for name in "${order[@]}"; do
|
|
367
|
+
key="$spec:$name"
|
|
368
|
+
kind="${_dsh_private_cli_kind[$key]}"
|
|
369
|
+
short="${_dsh_private_cli_shortname[$key]:-}"
|
|
370
|
+
if [[ "$kind" == value ]]; then
|
|
371
|
+
label=" --$name VALUE"
|
|
372
|
+
elif [[ "$kind" == optional_value ]]; then
|
|
373
|
+
label=" --$name [VALUE]"
|
|
374
|
+
else
|
|
375
|
+
label=" --$name"
|
|
376
|
+
fi
|
|
377
|
+
if [[ -n "$short" ]]; then
|
|
378
|
+
label="-$short, ${label# }"
|
|
379
|
+
fi
|
|
380
|
+
desc="$(dsh::i18n::t -n "${_dsh_private_cli_helptxt[$key]:-}")"
|
|
381
|
+
printf ' %-26s %s\n' "$label" "$desc"
|
|
382
|
+
done
|
|
383
|
+
return 0
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
_dsh_private_cli_check_value() {
|
|
387
|
+
local check="$1" name="$2" value="$3"
|
|
388
|
+
[[ -n "$check" ]] || return 0
|
|
389
|
+
if ! declare -F "$check" >/dev/null 2>&1; then
|
|
390
|
+
_dsh_private_cli_fail 'dsh::cli: check function not found: %1$s' "$check"
|
|
391
|
+
return $?
|
|
392
|
+
fi
|
|
393
|
+
if ! "$check" "$value"; then
|
|
394
|
+
_dsh_private_cli_fail 'dsh::cli: invalid value for --%1$s: %2$s' "$name" "$value"
|
|
395
|
+
return $?
|
|
396
|
+
fi
|
|
397
|
+
return 0
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
_dsh_private_cli_validate_group() {
|
|
401
|
+
local spec="$1" group="$2" name key count=0 names=''
|
|
402
|
+
for name in $group; do
|
|
403
|
+
key="$spec:$name"
|
|
404
|
+
if [[ -n "${_dsh_private_cli_seen[$key]:-}" ]]; then
|
|
405
|
+
count=$((count + 1))
|
|
406
|
+
if [[ -n "$names" ]]; then
|
|
407
|
+
names+=" "
|
|
408
|
+
fi
|
|
409
|
+
names+="--$name"
|
|
410
|
+
fi
|
|
411
|
+
done
|
|
412
|
+
if (( count > 1 )); then
|
|
413
|
+
_dsh_private_cli_fail 'dsh::cli: mutually exclusive options: %1$s' "$names"
|
|
414
|
+
return $?
|
|
415
|
+
fi
|
|
416
|
+
return 0
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
# Public: parse arguments by spec.
|
|
420
|
+
dsh::cli::run() {
|
|
421
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
422
|
+
_dsh_private_cli_help run
|
|
423
|
+
return $?
|
|
424
|
+
fi
|
|
425
|
+
local spec="${1:-}"
|
|
426
|
+
if (( $# >= 1 )); then
|
|
427
|
+
shift
|
|
428
|
+
fi
|
|
429
|
+
if [[ -z "$spec" ]] || [[ -z "${_dsh_private_cli_order[$spec]+x}" ]]; then
|
|
430
|
+
_dsh_private_cli_fail 'dsh::cli: unknown spec: %1$s' "${spec:-<empty>}"
|
|
431
|
+
return $?
|
|
432
|
+
fi
|
|
433
|
+
|
|
434
|
+
DSH_CLI_REMAINING=()
|
|
435
|
+
local key='' name token kind check
|
|
436
|
+
local -a order=()
|
|
437
|
+
# shellcheck disable=SC2206
|
|
438
|
+
order=(${_dsh_private_cli_order[$spec]})
|
|
439
|
+
for name in "${order[@]}"; do
|
|
440
|
+
unset "_dsh_private_cli_seen[$spec:$name]"
|
|
441
|
+
done
|
|
442
|
+
|
|
443
|
+
while (( $# )); do
|
|
444
|
+
token="$1"
|
|
445
|
+
case "$token" in
|
|
446
|
+
--)
|
|
447
|
+
DSH_CLI_REMAINING+=("${@:2}")
|
|
448
|
+
break
|
|
449
|
+
;;
|
|
450
|
+
-h|--h|--help)
|
|
451
|
+
dsh::cli::help "$spec"
|
|
452
|
+
return "$DSH_RC_USAGE"
|
|
453
|
+
;;
|
|
454
|
+
--*)
|
|
455
|
+
name="${token#--}"
|
|
456
|
+
key="$spec:$name"
|
|
457
|
+
if [[ -z "${_dsh_private_cli_kind[$key]+x}" ]]; then
|
|
458
|
+
DSH_CLI_REMAINING+=("$token")
|
|
459
|
+
shift
|
|
460
|
+
continue
|
|
461
|
+
fi
|
|
462
|
+
kind="${_dsh_private_cli_kind[$key]}"
|
|
463
|
+
_dsh_private_cli_seen[$key]=1
|
|
464
|
+
if [[ "$kind" == flag ]]; then
|
|
465
|
+
_dsh_private_cli_write "${_dsh_private_cli_into[$key]}" true
|
|
466
|
+
shift
|
|
467
|
+
continue
|
|
468
|
+
fi
|
|
469
|
+
if (( $# < 2 )) || [[ "$2" == --* ]]; then
|
|
470
|
+
if [[ "$kind" == optional_value ]]; then
|
|
471
|
+
_dsh_private_cli_write "${_dsh_private_cli_into[$key]}" "${_dsh_private_cli_default[$key]}"
|
|
472
|
+
shift
|
|
473
|
+
continue
|
|
474
|
+
fi
|
|
475
|
+
_dsh_private_cli_fail 'dsh::cli: missing value for --%1$s' "$name"
|
|
476
|
+
return $?
|
|
477
|
+
fi
|
|
478
|
+
check="${_dsh_private_cli_check[$key]}"
|
|
479
|
+
_dsh_private_cli_check_value "$check" "$name" "$2" || return $?
|
|
480
|
+
_dsh_private_cli_write "${_dsh_private_cli_into[$key]}" "$2"
|
|
481
|
+
shift 2
|
|
482
|
+
;;
|
|
483
|
+
-*)
|
|
484
|
+
name="${token#-}"
|
|
485
|
+
key="$spec:${_dsh_private_cli_byname[$spec:$name]:-}"
|
|
486
|
+
if [[ -z "${_dsh_private_cli_byname[$spec:$name]+x}" ]]; then
|
|
487
|
+
DSH_CLI_REMAINING+=("$token")
|
|
488
|
+
shift
|
|
489
|
+
continue
|
|
490
|
+
fi
|
|
491
|
+
_dsh_private_cli_seen[$key]=1
|
|
492
|
+
kind="${_dsh_private_cli_kind[$key]}"
|
|
493
|
+
name="${_dsh_private_cli_byname[$spec:$name]}"
|
|
494
|
+
if [[ "$kind" == flag ]]; then
|
|
495
|
+
_dsh_private_cli_write "${_dsh_private_cli_into[$key]}" true
|
|
496
|
+
shift
|
|
497
|
+
continue
|
|
498
|
+
fi
|
|
499
|
+
if (( $# < 2 )) || [[ "$2" == --* ]]; then
|
|
500
|
+
if [[ "$kind" == optional_value ]]; then
|
|
501
|
+
_dsh_private_cli_write "${_dsh_private_cli_into[$key]}" "${_dsh_private_cli_default[$key]}"
|
|
502
|
+
shift
|
|
503
|
+
continue
|
|
504
|
+
fi
|
|
505
|
+
_dsh_private_cli_fail 'dsh::cli: missing value for -%1$s' "$name"
|
|
506
|
+
return $?
|
|
507
|
+
fi
|
|
508
|
+
check="${_dsh_private_cli_check[$key]}"
|
|
509
|
+
_dsh_private_cli_check_value "$check" "$name" "$2" || return $?
|
|
510
|
+
_dsh_private_cli_write "${_dsh_private_cli_into[$key]}" "$2"
|
|
511
|
+
shift 2
|
|
512
|
+
;;
|
|
513
|
+
*)
|
|
514
|
+
DSH_CLI_REMAINING+=("$token")
|
|
515
|
+
shift
|
|
516
|
+
;;
|
|
517
|
+
esac
|
|
518
|
+
done
|
|
519
|
+
|
|
520
|
+
local group
|
|
521
|
+
for name in "${order[@]}"; do
|
|
522
|
+
key="$spec:$name"
|
|
523
|
+
if [[ "${_dsh_private_cli_required[$key]}" == 1 ]] \
|
|
524
|
+
&& [[ -z "${_dsh_private_cli_seen[$key]:-}" ]]; then
|
|
525
|
+
_dsh_private_cli_fail 'dsh::cli: missing required option: --%1$s' "$name"
|
|
526
|
+
return $?
|
|
527
|
+
fi
|
|
528
|
+
done
|
|
529
|
+
|
|
530
|
+
if [[ -n "${_dsh_private_cli_mutex[$spec]:-}" ]]; then
|
|
531
|
+
local IFS='|'
|
|
532
|
+
# shellcheck disable=SC2206
|
|
533
|
+
local -a groups=(${_dsh_private_cli_mutex[$spec]})
|
|
534
|
+
unset IFS
|
|
535
|
+
for group in "${groups[@]}"; do
|
|
536
|
+
_dsh_private_cli_validate_group "$spec" "$group" || return $?
|
|
537
|
+
done
|
|
538
|
+
fi
|
|
539
|
+
return 0
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
# Public: append unrecognised arguments to a caller-owned array.
|
|
543
|
+
dsh::cli::remaining_append() {
|
|
544
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
545
|
+
_dsh_private_cli_help remaining_append
|
|
546
|
+
return $?
|
|
547
|
+
fi
|
|
548
|
+
if (( $# != 1 )) || ! _dsh_private_cli_valid_var "$1"; then
|
|
549
|
+
_dsh_private_cli_fail 'Usage: dsh::cli::remaining_append <array-name>'
|
|
550
|
+
return $?
|
|
551
|
+
fi
|
|
552
|
+
local -n _dsh_private_cli_target="$1"
|
|
553
|
+
_dsh_private_cli_target+=("${DSH_CLI_REMAINING[@]}")
|
|
554
|
+
return 0
|
|
555
|
+
}
|