This repository was archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli
More file actions
executable file
·68 lines (54 loc) · 2.12 KB
/
cli
File metadata and controls
executable file
·68 lines (54 loc) · 2.12 KB
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
#!/usr/bin/bash
#
# SPDX-FileCopyrightText: 2019 Dmytro Kolomoiets <amerlyq@gmail.com> and contributors
#
# SPDX-License-Identifier: Apache-2.0
#
#%SUMMARY: help and command completion for main app
#%PERF: 35ms
set -fCueEo pipefail
main=${SUITE_NAME:?}
dbase=${SUITE_CACHE_HOME:?}
# NEED: short preview on formats with description when completing very first word
suggest(){ [[ $1 == -r ]] && local r=$1 && shift; "$main" kirie/suggest "$dbase/$1" "${r:+$dbase}" "${@:2}"; }
# NOTE: recursive help for all subcommands on single page
exedesc(){ suggest -r "$1" -xtype f -printf '%P' -execdir sed -n '/^#%SUMMARY:\s*/{s//:/;p;q}' {} \; \
| column --table --table-columns 'COMMAND,SUMMARY' --table-wrap 2 -s: -o' | '; }
die(){ >&2 echo "Err: $1"; help "${@:2}" >&2; exit 2; }
help(){ local cmd=$1
local nmg=${cmd##*/}
local dir=${cmd%$nmg}
echo "[${main}] $cmd"
# ALT:BET? $ suggest -r "$cmd" | tree -aAC --noreport --dirsfirst --fromfile -- .
# TRY: print help w/o symlinks annotated / and --help-all with -> symlink
LC_ALL=C tree --noreport -aAC --prune --matchdirs --dirsfirst \
-I "_*" -P "${nmg}*" -- "$dbase/$dir" | tail -n +2
}
getpath(){ declare -rn cmdpath=$1; local cmd=$2 count
cmdpath=$("$main" kirie/suggest "$dbase/$cmd")
count=${cmdpath//[^$'\n']}${cmdpath:+.}
count=${#count}
((count < 1)) && die "unknown command (or dangling symlink)" "$cmd"
((count > 1)) && die "found multiple N=$count commands" "$cmd"
[[ -x $dbase/$cmd ]] || die "incomplete command" "$cmd"
}
arg=${1-}
cmd=${2-}
shift || die "no arguments (print help)" ""
case $arg
in -h|--help) help "$cmd"
;; -H|--help-all) exedesc "$cmd"
;; --list-descriptions) exedesc "$cmd"
;; --suggest-next-completion)
exe=$("$main" kirie/suggest --all "$dbase/$cmd" "$dbase")
if [[ ! $exe =~ $'\n' && $exe == /* ]]; then
# HACK: check if option is present in script/binary cmd
if grep -q -- '--suggest-next-completion' "$exe"; then
exec "$exe" --suggest-next-completion "${@:3}"
fi
else
printf '%s\n' "$exe"
fi
;; --suggest-next-option) "$main" kirie/suggest "$dbase/$cmd" "$dbase"
;; *) exe=; getpath exe "$arg"; exec "$exe" "$@"
esac