-
Notifications
You must be signed in to change notification settings - Fork 42
/
auto-complete-tpl.go
69 lines (67 loc) · 2.24 KB
/
auto-complete-tpl.go
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
package utils
var BashCompleteScriptTpl = `#!/usr/bin/env {{.Shell}}
# ------------------------------------------------------------------------------
# FILE: {{.Path}}
# AUTHOR: inhere (https://github.com/inhere)
# VERSION: 1.0.0
# DESCRIPTION: zsh shell complete for cli app: {{.BinName}}
# ------------------------------------------------------------------------------
# usage: source {{.Path}}
# run 'complete' to see registered complete function.
_complete_for_{{.BinName}} () {
local cur prev
_get_comp_words_by_ref -n = cur prev
COMPREPLY=()
commands="{{join .CmdNames " "}} help"
case "$prev" in{{range $k,$v := .NameOpts}}
{{$k}})
COMPREPLY=($(compgen -W "{{$v}}" -- "$cur"))
return 0
;;{{end}}
help)
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
return 0
;;
esac
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
} &&
# complete -TaskFunc {auto_complete_func} {bin_filename}
# complete -TaskFunc _complete_for_{{.BinName}} -A file {{.BinName}} {{.BinName}}.exe
complete -TaskFunc _complete_for_{{.BinName}} {{.BinName}} {{.BinName}}.exe
`
var ZshCompleteScriptTpl = `#compdef {{.BinName}}
# ------------------------------------------------------------------------------
# FILE: {{.Path}}
# AUTHOR: inhere (https://github.com/inhere)
# VERSION: 1.0.0
# DESCRIPTION: zsh shell complete for cli app: {{.BinName}}
# ------------------------------------------------------------------------------
# usage: source {{.Path}}
_complete_for_{{.BinName}} () {
typeset -a commands
commands+=({{range $k,$v := .NameDes}}
'{{$k}}[{{$v}}]'{{end}}
'help[Display help information]'
)
if (( CURRENT == 2 )); then
# explain commands
_values 'cliapp commands' ${commands[@]}
return
fi
case ${words[2]} in{{range $k,$vs := .NameOpts}}
{{$k}})
_values 'command options' \{{range $vs}}
{{.}}{{end}}
;;{{end}}
help)
_values "${commands[@]}"
;;
*)
# use files by default
_files
;;
esac
}
compdef _complete_for_{{.BinName}} {{.BinName}}
compdef _complete_for_{{.BinName}} {{.BinName}}.exe
`