Skip to content

Commit b8153db

Browse files
committed
autocomplete for help
1 parent aa79719 commit b8153db

1 file changed

Lines changed: 43 additions & 20 deletions

File tree

local/etc/uceap.d/completion.sh

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
11
function completion() {
2-
commands=("${funcs[@]//_/-}")
3-
if [ $# -eq 1 ]; then
4-
shell="$1"
5-
case "$shell" in
6-
bash)
7-
echo complete -W \"${commands[*]}\" uceap
8-
;;
9-
zsh)
10-
echo "_uceap() { _arguments \"1: :(${commands[*]})\" }"
11-
echo "compdef _uceap uceap"
12-
;;
13-
*)
14-
echo "[error] Unsupported shell: $shell"
15-
return 1
16-
;;
17-
esac
18-
else
19-
echo "[error] Invalid number of arguments"
20-
return 1
21-
fi
2+
commands=("${funcs[@]//_/-}")
3+
if [ $# -eq 1 ]; then
4+
shell="$1"
5+
case "$shell" in
6+
bash)
7+
cat <<EOF
8+
_uceap_completions() {
9+
local cur prev opts
10+
COMPREPLY=()
11+
cur="\${COMP_WORDS[COMP_CWORD]}"
12+
prev="\${COMP_WORDS[COMP_CWORD-1]}"
13+
opts="${commands[*]}"
14+
15+
if [[ \$COMP_CWORD -eq 1 ]]; then
16+
COMPREPLY=( \$(compgen -W "\$opts help" -- "\$cur") )
17+
elif [[ \$COMP_CWORD -eq 2 && "\$prev" == "help" ]]; then
18+
COMPREPLY=( \$(compgen -W "\$opts" -- "\$cur") )
19+
fi
20+
}
21+
complete -F _uceap_completions uceap
22+
EOF
23+
;;
24+
zsh)
25+
echo '_uceap() {
26+
local -a commands
27+
commands=('"${commands[*]}"')
28+
if (( CURRENT == 2 )); then
29+
_arguments "1: :((help ${commands[*]}))"
30+
elif (( CURRENT == 3 )) && [[ $words[2] == help ]]; then
31+
_arguments "2: :(${commands[*]})"
32+
fi
33+
}
34+
compdef _uceap uceap'
35+
;;
36+
*)
37+
echo "[error] Unsupported shell: $shell"
38+
return 1
39+
;;
40+
esac
41+
else
42+
echo "[error] Invalid number of arguments"
43+
return 1
44+
fi
2245
}
2346

2447
_completion_desc="generate shell completion scripts"

0 commit comments

Comments
 (0)