Skip to content

Commit 8cba8ab

Browse files
committed
Update: template.
1 parent 859bf3c commit 8cba8ab

File tree

2 files changed

+82
-83
lines changed

2 files changed

+82
-83
lines changed

cmdcomp/v2/templates/bash.sh.jinja

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,36 @@ cmd_cur=$(( cmd_cur + 1 ))
3434

3535
{#- macro def -#}
3636
{%- macro command_completion(command, depth) -%}
37+
cmd_cur=$cur
38+
while [ $cur -lt $COMP_CWORD ] ; do
39+
cur=$(( cur + 1 ))
40+
41+
{%- if command.has_keyword_arguments %}
42+
case "${COMP_WORDS[cur-1]}" in
43+
{%- for keyword, argument in command.keyword_arguments.items() %}
44+
{{ ([keyword] + argument.aliases)|join("|") }})
45+
{{ argument_completion(argument)|indent(6) }}
46+
;;
47+
{% endfor %}
48+
*)
49+
break
50+
;;
51+
esac
52+
{%- endif %}
53+
done
54+
{%- if command.has_positional_arguments %}
55+
if [[ ! ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
56+
cur=$COMP_CWORD
57+
case $(( COMP_CWORD - cmd_cur + 1)) in
58+
{%- for position, argument in command.positional_arguments.items() %}
59+
{{ position }})
60+
{{ argument_completion(argument)|indent(4) }}
61+
;;
62+
{% endfor -%}
63+
esac
64+
fi
65+
{%- endif %}
66+
3767
{% if command.has_subcommands -%}
3868
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
3969
opts="{{ command.keyword_names_with_alias|join(' ') }}"
@@ -53,44 +83,18 @@ fi
5383
{%- else -%}
5484
opts="{{ command.keyword_names_with_alias|join(' ') }}"
5585
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
56-
return 0
5786
{%- endif %}
5887

59-
cmd_cur=$cur
60-
while [ $cur -lt $COMP_CWORD ] ; do
61-
cur=$(( cur + 1 ))
62-
63-
{%- if command.has_keyword_arguments %}
64-
case "${COMP_WORDS[cur-1]}" in
65-
{%- for keyword, argument in command.keyword_arguments.items() %}
66-
{{ ([keyword] + argument.aliases)|join("|") }})
67-
{{ argument_completion(argument)|indent(6) }}
68-
;;
69-
{% endfor %}
70-
*)
71-
break
72-
;;
73-
esac
74-
{%- endif %}
75-
done
76-
{%- if command.has_positional_arguments or command.has_positional_wildcard_argument %}
77-
cur=$COMP_CWORD
78-
case $(( COMP_CWORD - cmd_cur + 1)) in
79-
{%- for position, argument in command.positional_arguments.items() %}
80-
{{ position }})
81-
{{ argument_completion(argument)|indent(4) }}
82-
;;
83-
{% endfor -%}
8488
{%- if command.has_positional_wildcard_argument %}
85-
*)
86-
cur=$COMP_CWORD
87-
{{ argument_completion(command.positional_wildcard_argument)|indent(4) }}
88-
;;
89-
{% endif -%}
90-
esac
89+
cur=$COMP_CWORD
90+
{{ argument_completion(command.positional_wildcard_argument) }}
9191
{%- endif %}
9292
{%- endmacro -%}
9393

94+
{#- macro def -#}
95+
{%- macro file_completion(base_path) -%}
96+
{%- endmacro -%}
97+
9498
#!/bin/bash
9599

96100
{{ func_name }}() {

samples/v2/output.bash

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ _cliname() {
4949

5050
case "${cmd}" in
5151
_cliname)
52-
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
53-
opts="--verbose --no-verbose --version -V --config --help"
54-
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
55-
return 0
56-
elif [ $cur -eq $COMP_CWORD ] ; then
57-
opts="list ls cd test"
58-
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
59-
return 0
60-
fi
61-
6252
cmd_cur=$cur
6353
while [ $cur -lt $COMP_CWORD ] ; do
6454
cur=$(( cur + 1 ))
@@ -95,16 +85,20 @@ _cliname() {
9585
esac
9686
done
9787

98-
return 0
99-
;;
100-
101-
_cliname_list)
10288
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
103-
opts="--all -a"
89+
opts="--verbose --no-verbose --version -V --config --help"
90+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
91+
return 0
92+
elif [ $cur -eq $COMP_CWORD ] ; then
93+
opts="list ls cd test"
10494
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
10595
return 0
10696
fi
10797

98+
return 0
99+
;;
100+
101+
_cliname_list)
108102
cmd_cur=$cur
109103
while [ $cur -lt $COMP_CWORD ] ; do
110104
cur=$(( cur + 1 ))
@@ -118,30 +112,25 @@ _cliname() {
118112
;;
119113
esac
120114
done
115+
116+
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
117+
opts="--all -a"
118+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
119+
return 0
120+
fi
121121
cur=$COMP_CWORD
122-
case $(( COMP_CWORD - cmd_cur + 1)) in
123-
*)
124-
cur=$COMP_CWORD
125-
if [ $cur -eq $COMP_CWORD ] ; then
126-
file_completion "."
122+
if [ $cur -eq $COMP_CWORD ] ; then
123+
file_completion "."
127124

128-
return 0
129-
else
130-
cmd_cur=$(( cmd_cur + 2 ))
131-
fi
132-
;;
133-
esac
125+
return 0
126+
else
127+
cmd_cur=$(( cmd_cur + 2 ))
128+
fi
134129

135130
return 0
136131
;;
137132

138133
_cliname_cd)
139-
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
140-
opts="-P"
141-
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
142-
return 0
143-
fi
144-
145134
cmd_cur=$cur
146135
while [ $cur -lt $COMP_CWORD ] ; do
147136
cur=$(( cur + 1 ))
@@ -155,23 +144,36 @@ _cliname() {
155144
;;
156145
esac
157146
done
158-
cur=$COMP_CWORD
159-
case $(( COMP_CWORD - cmd_cur + 1)) in
160-
1)
161-
if [ $cur -eq $COMP_CWORD ] ; then
147+
if [[ ! ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
148+
cur=$COMP_CWORD
149+
case $(( COMP_CWORD - cmd_cur + 1)) in
150+
1)
151+
if [ $cur -eq $COMP_CWORD ] ; then
162152
file_completion "$HOME"
163153

164154
return 0
165155
else
166156
cmd_cur=$(( cmd_cur + 2 ))
167157
fi
168-
;;
169-
esac
158+
;;
159+
esac
160+
fi
161+
162+
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
163+
opts="-P"
164+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
165+
return 0
166+
fi
170167

171168
return 0
172169
;;
173170

174171
_cliname_test)
172+
cmd_cur=$cur
173+
while [ $cur -lt $COMP_CWORD ] ; do
174+
cur=$(( cur + 1 ))
175+
done
176+
175177
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
176178
opts=""
177179
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
@@ -182,19 +184,10 @@ _cliname() {
182184
return 0
183185
fi
184186

185-
cmd_cur=$cur
186-
while [ $cur -lt $COMP_CWORD ] ; do
187-
cur=$(( cur + 1 ))
188-
done
189-
190187
return 0
191188
;;
192189

193190
_cliname_test_rubocop)
194-
opts="--auto-correct -A"
195-
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
196-
return 0
197-
198191
cmd_cur=$cur
199192
while [ $cur -lt $COMP_CWORD ] ; do
200193
cur=$(( cur + 1 ))
@@ -209,19 +202,21 @@ _cliname() {
209202
esac
210203
done
211204

205+
opts="--auto-correct -A"
206+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
207+
212208
return 0
213209
;;
214210

215211
_cliname_test_pytest)
216-
opts=""
217-
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
218-
return 0
219-
220212
cmd_cur=$cur
221213
while [ $cur -lt $COMP_CWORD ] ; do
222214
cur=$(( cur + 1 ))
223215
done
224216

217+
opts=""
218+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
219+
225220
return 0
226221
;;
227222

0 commit comments

Comments
 (0)