|
@@ -1,38 +1,95 @@
|
1
|
1
|
#!/bin/bash
|
2
|
2
|
|
3
|
3
|
__SATURNIN_COMPLETE_CMDNAME=__MKIT_PROJ_PKGNAME__
|
|
4
|
+__SATURNIN_COMPLETE_M_OPTS="-D -V -d -h -v --full-debug --help --verbose --debug --version --version-semver"
|
4
|
5
|
|
5
|
6
|
#shellcheck disable=SC2016
|
6
|
|
-__SATURNIN_COMPLETE_CODETMPL='
|
7
|
|
-__%s() {
|
8
|
|
- local cur opts sopts
|
|
7
|
+__SATURNIN_COMPLETE_CODE='#!/bin/bash
|
|
8
|
+
|
|
9
|
+__'"$__SATURNIN_COMPLETE_CMDNAME"'() {
|
|
10
|
+ local Meta=$1 # current metacommand
|
|
11
|
+ local Partial=$2 # current unfinished word
|
|
12
|
+ local Previous=$3 # word preceding the unfinished one
|
|
13
|
+ local ScmdWords # words from subcommand on, if already present
|
|
14
|
+ local Scmds=() # all subcommands
|
|
15
|
+ local parser=__${Meta}__scparse # parse util function
|
|
16
|
+ local asker=__${Meta}__scask # util function to ask Saturnin
|
|
17
|
+ local comptext # compgen result text
|
|
18
|
+ mapfile -t Scmds <<<"$("$Meta" --saturnin-get-subcommands)"
|
9
|
19
|
COMPREPLY=()
|
10
|
|
- cur="${COMP_WORDS[COMP_CWORD]}"
|
11
|
|
- sopts="-D -V -d -h -v"
|
12
|
|
- opts="--full-debug --help --verbose --debug --version --version-semver"
|
13
|
|
- case "$cur" in
|
14
|
|
- -*)
|
15
|
|
- COMPREPLY=(
|
16
|
|
- $(compgen -W "$sopts $opts" -- "${cur}")
|
17
|
|
- )
|
18
|
|
- ;;
|
19
|
|
- --*)
|
20
|
|
- COMPREPLY=(
|
21
|
|
- $(compgen -W "$opts" -- "${cur}")
|
22
|
|
- )
|
23
|
|
- ;;
|
24
|
|
- *)
|
25
|
|
- COMPREPLY=(
|
26
|
|
- $(compgen -W "$(%s --saturnin-get-subcommands)" "${cur}")
|
27
|
|
- )
|
|
20
|
+ if $parser HAS; then
|
|
21
|
+ COMP_WORDBREAKS=" "
|
|
22
|
+ mapfile -t ScmdWords <<<"$($parser WORDS)"
|
|
23
|
+ comptext=$(compgen -W "$($asker)" -- "$Partial")
|
|
24
|
+ else
|
|
25
|
+ case $Partial in
|
|
26
|
+ -*)
|
|
27
|
+ comptext=$(
|
|
28
|
+ compgen -W "'"$__SATURNIN_COMPLETE_M_OPTS"'" -- "$Partial"
|
|
29
|
+ )
|
|
30
|
+ ;;
|
|
31
|
+ *)
|
|
32
|
+ comptext=$(compgen -W "${Scmds[*]}" "$Partial")
|
|
33
|
+ esac
|
|
34
|
+ fi
|
|
35
|
+ case $comptext in
|
|
36
|
+ "") COMPREPLY=() ;;
|
|
37
|
+ *) mapfile -t COMPREPLY <<<"$comptext" ;;
|
|
38
|
+ esac
|
|
39
|
+ {
|
|
40
|
+ declare -p Meta
|
|
41
|
+ declare -p Partial
|
|
42
|
+ declare -p Previous
|
|
43
|
+ declare -p ScmdWords
|
|
44
|
+ declare -p comptext
|
|
45
|
+ declare -p COMPREPLY
|
|
46
|
+ } | "__${Meta}__debug"
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+__'"$__SATURNIN_COMPLETE_CMDNAME"'__debug() {
|
|
50
|
+ #
|
|
51
|
+ # Send stdin to debug file, or /dev/null if debug is off
|
|
52
|
+ #
|
|
53
|
+ local dfile=/dev/null
|
|
54
|
+ if test "$__SATURNIN_COMPLETE_DEBUG" == true; then
|
|
55
|
+ dfile=/tmp/__saturnin_complete-'"$__SATURNIN_COMPLETE_CMDNAME"'.debug
|
|
56
|
+ fi
|
|
57
|
+ cat >"$dfile"
|
|
58
|
+}
|
|
59
|
+
|
|
60
|
+__'"$__SATURNIN_COMPLETE_CMDNAME"'__scask() {
|
|
61
|
+ #
|
|
62
|
+ # Ask and pass args from function plus all the filtered words
|
|
63
|
+ #
|
|
64
|
+ "$Meta" --saturnin-sccompgen "$Meta" "$Partial" "$Previous" "${ScmdWords[@]}"
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+__'"$__SATURNIN_COMPLETE_CMDNAME"'__scparse() {
|
|
68
|
+ local what=$1
|
|
69
|
+ set -- "${COMP_WORDS[@]}"
|
|
70
|
+ test "$1" == "$Meta" || echo "__${Meta}__scparse(): completion error" >&2
|
|
71
|
+ shift
|
|
72
|
+ local scswgaps=" ${Scmds[*]} "
|
|
73
|
+ while true; do
|
|
74
|
+ test -n "$1" || return 1
|
|
75
|
+ test "${1#-}" == "$1" || { shift; continue; }
|
|
76
|
+ test "${scswgaps/ $1 /}" == "$scswgaps" && { shift; continue; }
|
|
77
|
+ break
|
|
78
|
+ done
|
|
79
|
+ case $what in
|
|
80
|
+ HAS) : ;;
|
|
81
|
+ WORDS) echo "$1"
|
|
82
|
+ shift
|
|
83
|
+ while test $# -gt 0; do
|
|
84
|
+ echo "$1"
|
|
85
|
+ shift
|
|
86
|
+ done
|
|
87
|
+ ;;
|
28
|
88
|
esac
|
29
|
|
-}'
|
30
|
|
-
|
31
|
|
-eval "$(
|
32
|
|
- #shellcheck disable=SC2059
|
33
|
|
- printf "$__SATURNIN_COMPLETE_CODETMPL" \
|
34
|
|
- $__SATURNIN_COMPLETE_CMDNAME \
|
35
|
|
- $__SATURNIN_COMPLETE_CMDNAME
|
36
|
|
-)"
|
|
89
|
+ return 0
|
|
90
|
+}
|
|
91
|
+'
|
|
92
|
+
|
|
93
|
+eval "$__SATURNIN_COMPLETE_CODE"
|
37
|
94
|
|
38
|
95
|
complete -F "__$__SATURNIN_COMPLETE_CMDNAME" "$__SATURNIN_COMPLETE_CMDNAME"
|