-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion_tmpl.go
More file actions
200 lines (176 loc) · 8.34 KB
/
completion_tmpl.go
File metadata and controls
200 lines (176 loc) · 8.34 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package quickclop
import (
"text/template"
)
// Bash 补全脚本模板
var bashCompletionTmpl = template.Must(template.New("bash_completion").Parse(`
# bash completion for {{ .AppName }}
_{{ .AppName }}() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# 基本选项
opts="--help -h --config -c {{ range .Fields }}{{ if not .IsNested }}{{ if .Short }}-{{ .Short }} {{ end }}{{ if .Long }}--{{ .Long }} {{ end }}{{ end }}{{ end }}"
# 子命令
{{ if .HasSubcommands }}
subcmds="{{ range .Subcommands }}{{ .Name }} {{ end }}"
# 如果当前是第一个参数,可能是子命令
if [[ ${COMP_CWORD} -eq 1 ]]; then
COMPREPLY=( $(compgen -W "${opts} ${subcmds}" -- ${cur}) )
return 0
fi
# 检查是否在处理子命令
for subcmd in ${subcmds}; do
if [[ "${COMP_WORDS[1]}" == "${subcmd}" ]]; then
case "${subcmd}" in
{{ range .Subcommands }}
"{{ .Name }}")
# 子命令特定的补全
_{{ $.AppName }}_{{ .Name }} "${cur}" "${prev}"
return 0
;;
{{ end }}
esac
fi
done
{{ end }}
# 选项补全
case "${prev}" in
{{ range .Fields }}{{ if not .IsNested }}{{ if or .Short .Long }}
{{ if .Short }}"-{{ .Short }}"{{ end }}{{ if and .Short .Long }}|{{ end }}{{ if .Long }}"--{{ .Long }}"{{ end }})
{{ if eq .Completion "file" }}
# 文件补全
COMPREPLY=( $(compgen -f "${cur}") )
{{ else if eq .Completion "dir" }}
# 目录补全
COMPREPLY=( $(compgen -d "${cur}") )
{{ else if eq .Completion "custom" }}
# 自定义补全
local values="{{ .CompletionValues }}"
COMPREPLY=( $(compgen -W "${values}" -- ${cur}) )
{{ else }}
# 无特定补全
COMPREPLY=()
{{ end }}
return 0
;;
{{ end }}{{ end }}{{ end }}
"--config"|"-c")
# 配置文件补全
COMPREPLY=( $(compgen -f "${cur}") )
return 0
;;
*)
;;
esac
# 默认补全所有选项
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
{{ range .Subcommands }}
# {{ .Name }} 子命令的补全函数
_{{ $.AppName }}_{{ .Name }}() {
local cur="${1}"
local prev="${2}"
local opts="{{ range .Fields }}{{ if not .IsNested }}{{ if .Short }}-{{ .Short }} {{ end }}{{ if .Long }}--{{ .Long }} {{ end }}{{ end }}{{ end }}"
case "${prev}" in
{{ range .Fields }}{{ if not .IsNested }}{{ if or .Short .Long }}
{{ if .Short }}"-{{ .Short }}"{{ end }}{{ if and .Short .Long }}|{{ end }}{{ if .Long }}"--{{ .Long }}"{{ end }})
{{ if eq .Completion "file" }}
# 文件补全
COMPREPLY=( $(compgen -f "${cur}") )
{{ else if eq .Completion "dir" }}
# 目录补全
COMPREPLY=( $(compgen -d "${cur}") )
{{ else if eq .Completion "custom" }}
# 自定义补全
local values="{{ .CompletionValues }}"
COMPREPLY=( $(compgen -W "${values}" -- ${cur}) )
{{ else }}
# 无特定补全
COMPREPLY=()
{{ end }}
return 0
;;
{{ end }}{{ end }}{{ end }}
*)
;;
esac
# 默认补全所有选项
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
}
{{ end }}
complete -F _{{ .AppName }} {{ .AppName }}
`))
// Zsh 补全脚本模板
var zshCompletionTmpl = template.Must(template.New("zsh_completion").Parse(`
#compdef {{ .AppName }}
_{{ .AppName }}() {
local line state
_arguments -C \
{{ range .Fields }}{{ if not .IsNested }}{{ if or .Short .Long }}
{{ if and .Short .Long }}'({{ if .Short }}-{{ .Short }}{{ end }} {{ if .Long }}--{{ .Long }}{{ end }})'{{ end }}{{ if .Short }}'-{{ .Short }}[{{ .Usage }}]{{ if eq .Completion "file" }}:file:_files{{ else if eq .Completion "dir" }}:directory:_files -/{{ else if eq .Completion "custom" }}:custom:({{ .CompletionValues }}){{ end }}'{{ end }} \
{{ if and .Short .Long }}'({{ if .Short }}-{{ .Short }}{{ end }} {{ if .Long }}--{{ .Long }}{{ end }})'{{ end }}{{ if .Long }}'--{{ .Long }}[{{ .Usage }}]{{ if eq .Completion "file" }}:file:_files{{ else if eq .Completion "dir" }}:directory:_files -/{{ else if eq .Completion "custom" }}:custom:({{ .CompletionValues }}){{ end }}'{{ end }} \
{{ end }}{{ end }}{{ end }}
'--config[指定配置文件路径]:file:_files' \
'--help[显示帮助信息]' \
{{ if .HasSubcommands }}
'1: :->command' \
'*::arg:->args' \
{{ end }}
{{ if .HasSubcommands }}
case $state in
command)
_values 'command' \
{{ range .Subcommands }}
'{{ .Name }}[{{ .Usage }}]' \
{{ end }}
;;
args)
case $line[1] in
{{ range .Subcommands }}
{{ .Name }})
_{{ $.AppName }}_{{ .Name }}
;;
{{ end }}
esac
;;
esac
{{ end }}
}
{{ range .Subcommands }}
_{{ $.AppName }}_{{ .Name }}() {
_arguments \
{{ range .Fields }}{{ if not .IsNested }}{{ if or .Short .Long }}
{{ if and .Short .Long }}'({{ if .Short }}-{{ .Short }}{{ end }} {{ if .Long }}--{{ .Long }}{{ end }})'{{ end }}{{ if .Short }}'-{{ .Short }}[{{ .Usage }}]{{ if eq .Completion "file" }}:file:_files{{ else if eq .Completion "dir" }}:directory:_files -/{{ else if eq .Completion "custom" }}:custom:({{ .CompletionValues }}){{ end }}'{{ end }} \
{{ if and .Short .Long }}'({{ if .Short }}-{{ .Short }}{{ end }} {{ if .Long }}--{{ .Long }}{{ end }})'{{ end }}{{ if .Long }}'--{{ .Long }}[{{ .Usage }}]{{ if eq .Completion "file" }}:file:_files{{ else if eq .Completion "dir" }}:directory:_files -/{{ else if eq .Completion "custom" }}:custom:({{ .CompletionValues }}){{ end }}'{{ end }} \
{{ end }}{{ end }}{{ end }}
'--help[显示帮助信息]'
}
{{ end }}
_{{ .AppName }}
`))
// Fish 补全脚本模板
var fishCompletionTmpl = template.Must(template.New("fish_completion").Parse(`
# fish completion for {{ .AppName }}
# 全局选项
complete -c {{ .AppName }} -l help -s h -d "显示帮助信息"
complete -c {{ .AppName }} -l config -s c -d "指定配置文件路径" -r -F
{{ range .Fields }}{{ if not .IsNested }}{{ if or .Short .Long }}
# {{ .Usage }}
{{ if .Short }}complete -c {{ $.AppName }} -s {{ .Short }}{{ if not (eq .Type "bool") }} -r{{ end }}{{ if .Long }} -l {{ .Long }}{{ end }} -d "{{ .Usage }}"{{ if eq .Completion "file" }} -F{{ else if eq .Completion "dir" }} -F -a "(__fish_complete_directories)"{{ else if eq .Completion "custom" }} -a "{{ .CompletionValues }}"{{ end }}{{ end }}
{{ if and (not .Short) .Long }}complete -c {{ $.AppName }} -l {{ .Long }}{{ if not (eq .Type "bool") }} -r{{ end }} -d "{{ .Usage }}"{{ if eq .Completion "file" }} -F{{ else if eq .Completion "dir" }} -F -a "(__fish_complete_directories)"{{ else if eq .Completion "custom" }} -a "{{ .CompletionValues }}"{{ end }}{{ end }}
{{ end }}{{ end }}{{ end }}
{{ if .HasSubcommands }}
# 子命令
{{ range .Subcommands }}
complete -c {{ $.AppName }} -f -n "__fish_use_subcommand" -a {{ .Name }} -d "{{ .Usage }}"
# {{ .Name }} 子命令选项
{{ range .Fields }}{{ if not .IsNested }}{{ if or .Short .Long }}
{{ if .Short }}complete -c {{ $.AppName }} -n "__fish_seen_subcommand_from {{ .Name }}" -s {{ .Short }}{{ if not (eq .Type "bool") }} -r{{ end }}{{ if .Long }} -l {{ .Long }}{{ end }} -d "{{ .Usage }}"{{ if eq .Completion "file" }} -F{{ else if eq .Completion "dir" }} -F -a "(__fish_complete_directories)"{{ else if eq .Completion "custom" }} -a "{{ .CompletionValues }}"{{ end }}{{ end }}
{{ if and (not .Short) .Long }}complete -c {{ $.AppName }} -n "__fish_seen_subcommand_from {{ .Name }}" -l {{ .Long }}{{ if not (eq .Type "bool") }} -r{{ end }} -d "{{ .Usage }}"{{ if eq .Completion "file" }} -F{{ else if eq .Completion "dir" }} -F -a "(__fish_complete_directories)"{{ else if eq .Completion "custom" }} -a "{{ .CompletionValues }}"{{ end }}{{ end }}
{{ end }}{{ end }}{{ end }}
{{ end }}
{{ end }}
`))