Skip to content

Commit 859bf3c

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

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

cmdcomp/v2/templates/bash.sh.jinja

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ cmd_cur=$(( cmd_cur + 1 ))
3535
{#- macro def -#}
3636
{%- macro command_completion(command, depth) -%}
3737
{% if command.has_subcommands -%}
38-
opts="{{ (command.subcommand_names_with_alias + command.keyword_names_with_alias)|join(' ') }}"
39-
if [[ ${COMP_CWORD} -eq {{ depth }} ]] ; then
38+
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
39+
opts="{{ command.keyword_names_with_alias|join(' ') }}"
40+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
41+
return 0
42+
elif [ $cur -eq $COMP_CWORD ] ; then
43+
opts="{{ command.subcommand_names_with_alias|join(' ') }}"
4044
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
4145
return 0
4246
fi
4347
{%- elif command.has_positional_arguments or command.has_positional_wildcard_argument -%}
44-
opts="{{ command.keyword_names_with_alias|join(' ') }}"
4548
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
49+
opts="{{ command.keyword_names_with_alias|join(' ') }}"
4650
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
4751
return 0
4852
fi
@@ -84,24 +88,25 @@ case $(( COMP_CWORD - cmd_cur + 1)) in
8488
;;
8589
{% endif -%}
8690
esac
87-
{%- endif -%}
91+
{%- endif %}
8892
{%- endmacro -%}
8993

9094
#!/bin/bash
9195

9296
{{ func_name }}() {
93-
local word cur cmd cmd_cur opts
97+
local word cmd opts cur cmd_cur opts_cur
9498
COMPREPLY=()
95-
cur=0
96-
cmd_cur=0
9799
cmd=""
98100
opts=""
101+
cur=0
102+
cmd_cur=0
103+
opts_cur=0
99104

100105
for word in ${COMP_WORDS[@]}; do
101106
case "${cmd},${word}" in
102107
",$1")
103108
cmd="{{ func_name }}"
104-
cur=$(( cur + 1 ))
109+
cur=$(( cur + opts_cur + 1 ))
105110
;;
106111
{% for (tag, command_name), subcommand in append_key_tag(commands[app_name].subcommands, func_name).items() recursive %}
107112
{%- set new_tag = tag + "_" + command_name|replace("-","_") %}
@@ -110,11 +115,12 @@ esac
110115
|{{ tag }},{{ alias }}
111116
{%- endfor -%})
112117
cmd="{{ new_tag }}"
113-
cur=$(( cur + 1 ))
118+
cur=$(( cur + opts_cur + 1 ))
114119
;;
115120
{{ loop(append_key_tag(subcommand.subcommands, new_tag).items()) -}}
116121
{% endfor %}
117122
*)
123+
opts_cur=$(( opts_cur + 1 ))
118124
;;
119125
esac
120126
done

samples/v2/output.bash

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,60 @@
11
#!/bin/bash
22

33
_cliname() {
4-
local word cur cmd cmd_cur opts
4+
local word cmd opts cur cmd_cur opts_cur
55
COMPREPLY=()
6-
cur=0
7-
cmd_cur=0
86
cmd=""
97
opts=""
8+
cur=0
9+
cmd_cur=0
10+
opts_cur=0
1011

1112
for word in ${COMP_WORDS[@]}; do
1213
case "${cmd},${word}" in
1314
",$1")
1415
cmd="_cliname"
15-
cur=$(( cur + 1 ))
16+
cur=$(( cur + opts_cur + 1 ))
1617
;;
1718

1819
_cliname,list|_cliname,ls)
1920
cmd="_cliname_list"
20-
cur=$(( cur + 1 ))
21+
cur=$(( cur + opts_cur + 1 ))
2122
;;
2223

2324
_cliname,cd)
2425
cmd="_cliname_cd"
25-
cur=$(( cur + 1 ))
26+
cur=$(( cur + opts_cur + 1 ))
2627
;;
2728

2829
_cliname,test)
2930
cmd="_cliname_test"
30-
cur=$(( cur + 1 ))
31+
cur=$(( cur + opts_cur + 1 ))
3132
;;
3233

3334
_cliname_test,rubocop)
3435
cmd="_cliname_test_rubocop"
35-
cur=$(( cur + 1 ))
36+
cur=$(( cur + opts_cur + 1 ))
3637
;;
3738

3839
_cliname_test,pytest)
3940
cmd="_cliname_test_pytest"
40-
cur=$(( cur + 1 ))
41+
cur=$(( cur + opts_cur + 1 ))
4142
;;
4243

4344
*)
45+
opts_cur=$(( opts_cur + 1 ))
4446
;;
4547
esac
4648
done
4749

4850
case "${cmd}" in
4951
_cliname)
50-
opts="list ls cd test --verbose --no-verbose --version -V --config --help"
51-
if [[ ${COMP_CWORD} -eq 1 ]] ; then
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"
5258
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
5359
return 0
5460
fi
@@ -93,8 +99,8 @@ _cliname() {
9399
;;
94100

95101
_cliname_list)
96-
opts="--all -a"
97102
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
103+
opts="--all -a"
98104
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
99105
return 0
100106
fi
@@ -130,8 +136,8 @@ _cliname() {
130136
;;
131137

132138
_cliname_cd)
133-
opts="-P"
134139
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
140+
opts="-P"
135141
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
136142
return 0
137143
fi
@@ -166,8 +172,12 @@ _cliname() {
166172
;;
167173

168174
_cliname_test)
169-
opts="rubocop pytest"
170-
if [[ ${COMP_CWORD} -eq 2 ]] ; then
175+
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
176+
opts=""
177+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
178+
return 0
179+
elif [ $cur -eq $COMP_CWORD ] ; then
180+
opts="rubocop pytest"
171181
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
172182
return 0
173183
fi

0 commit comments

Comments
 (0)