Skip to content

Commit 04beb60

Browse files
committed
Fix: execute command.
1 parent 572e762 commit 04beb60

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

cmdcomp/v2/completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def generate_v2(shell: ShellType, config: V2Config) -> str:
1313
env = Environment(
1414
loader=FileSystemLoader(Path(__file__).parent / "templates"),
1515
)
16-
env.filters["ident"] = lambda x: re.sub(r"[,.-]", "_", x)
16+
env.filters["ident"] = lambda x: re.sub(r"[\*\.,-]", "_", str(x))
1717
template = env.get_template(f"{shell.value}.sh.jinja")
1818

1919
return template.render(

cmdcomp/v2/templates/zsh.sh.jinja

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{%- elif argument.type == 'file' -%}
2323
:file:_files{%- if argument.base_path is not none %} -W "{{ argument.base_path }}"{%- endif -%}
2424
{%- elif argument.type == 'command' -%}
25-
:command:_values '{{ arg_name|trim("-") }}' $({{ argument.execute }})
25+
:command:_values '{{ arg_name|ident }}' '"$_{{ arg_name|ident }}_execute_result"'
2626
{%- elif argument.type == 'flag' -%}
2727
{%- endif -%}
2828
{%- endmacro -%}
@@ -51,6 +51,11 @@
5151
{%- endfor %}
5252
)
5353
{% endif %}
54+
{%- for arg_name, argument in command.arguments.items() -%}
55+
{%- if argument.type == "command" %}
56+
local _{{ arg_name|ident }}_execute_result=$({{ argument.execute }})
57+
{%- endif %}
58+
{%- endfor %}
5459
_arguments -C \
5560
{%- for kwd_name, keyword in command.keyword_arguments.items() %}
5661
{{ candidate(kwd_name, keyword) }}'{{ description(keyword) }}{{ contents(kwd_name, keyword) }}' \

samples/v2/config.cmdcomp.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ type = "file"
5252
base_path = "$HOME"
5353
description = "change project directory."
5454

55+
[root.subcommands.scripts]
56+
description = "operate scripts."
57+
[root.subcommands.scripts.subcommands.run]
58+
description = "run script."
59+
60+
[root.subcommands.scripts.subcommands.run.arguments.--all]
61+
type = "flag"
62+
alias = "-a"
63+
description = "run all scripts."
64+
65+
[root.subcommands.scripts.subcommands.run.arguments."*"]
66+
type = "command"
67+
description = "run script."
68+
execute = "echo 'script1.sh script2.sh script3.sh'"
69+
5570
[root.subcommands.test]
5671
description = "test command."
5772

samples/v2/config.cmdcomp.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ root:
4343
type: file
4444
base_path: $HOME
4545
description: "change project directory."
46+
scripts:
47+
description: "operate scripts."
48+
subcommands:
49+
run:
50+
description: "run script."
51+
arguments:
52+
--all:
53+
type: flag
54+
alias: "-a"
55+
description: "run all scripts."
56+
"*":
57+
type: command
58+
description: "run script."
59+
execute: "echo 'script1.sh script2.sh script3.sh'"
4660
test:
4761
description: "test command."
4862
subcommands:

samples/v2/output.bash

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ _cliname() {
3030
cur=$(( cur + opts_cur + 1 ))
3131
;;
3232

33+
_cliname,scripts)
34+
cmd="_cliname_scripts"
35+
cur=$(( cur + opts_cur + 1 ))
36+
;;
37+
38+
_cliname_scripts,run)
39+
cmd="_cliname_scripts_run"
40+
cur=$(( cur + opts_cur + 1 ))
41+
;;
42+
3343
_cliname,test)
3444
cmd="_cliname_test"
3545
cur=$(( cur + opts_cur + 1 ))
@@ -94,7 +104,7 @@ _cliname() {
94104
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
95105
return 0
96106
elif [ $cur -eq $COMP_CWORD ] ; then
97-
opts="list ls cd test"
107+
opts="list ls cd scripts test"
98108
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
99109
return 0
100110
fi
@@ -172,6 +182,57 @@ _cliname() {
172182
return 0
173183
;;
174184

185+
_cliname_scripts)
186+
cmd_cur=$cur
187+
while [ $cur -lt $COMP_CWORD ] ; do
188+
cur=$(( cur + 1 ))
189+
done
190+
191+
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
192+
opts=""
193+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
194+
return 0
195+
elif [ $cur -eq $COMP_CWORD ] ; then
196+
opts="run"
197+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
198+
return 0
199+
fi
200+
201+
return 0
202+
;;
203+
204+
_cliname_scripts_run)
205+
cmd_cur=$cur
206+
while [ $cur -lt $COMP_CWORD ] ; do
207+
cur=$(( cur + 1 ))
208+
case "${COMP_WORDS[cur-1]}" in
209+
--all|-a)
210+
cmd_cur=$(( cmd_cur + 1 ))
211+
;;
212+
213+
*)
214+
break
215+
;;
216+
esac
217+
done
218+
219+
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
220+
opts="--all -a"
221+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
222+
return 0
223+
fi
224+
cur=$COMP_CWORD
225+
if [ $cur -eq $COMP_CWORD ] ; then
226+
COMPREPLY=( $(compgen -W "echo 'script1.sh script2.sh script3.sh'" -- "$cur") )
227+
228+
return 0
229+
else
230+
cmd_cur=$(( cmd_cur + 2 ))
231+
fi
232+
233+
return 0
234+
;;
235+
175236
_cliname_test)
176237
cmd_cur=$cur
177238
while [ $cur -lt $COMP_CWORD ] ; do

samples/v2/output.zsh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ _cliname() {
1616
__cliname_subcmds=(
1717
{list,ls}'[list project files.]'
1818
cd'[cd project directory.]'
19+
scripts'[operate scripts.]'
1920
test'[test command.]'
2021
)
2122

@@ -47,6 +48,35 @@ _cliname() {
4748
&& ret=0
4849
;;
4950

51+
scripts)
52+
local -a __scripts_subcmds
53+
__scripts_subcmds=(
54+
run'[run script.]'
55+
)
56+
57+
_arguments -C \
58+
'1: :_values "subcommand" ${__scripts_subcmds[@]}' \
59+
'*:: :->args' \
60+
&& ret=0
61+
62+
cmd_name=$words[1]
63+
case $state in
64+
args)
65+
case $cmd_name in
66+
run)
67+
local ___execute_result=$(echo 'script1.sh script2.sh script3.sh')
68+
_arguments -C \
69+
{--all,-a}'[run all scripts.]' \
70+
'*:command:_values '_' '"$___execute_result"'' \
71+
&& ret=0
72+
;;
73+
74+
esac
75+
;;
76+
77+
esac
78+
;;
79+
5080
test)
5181
local -a __test_subcmds
5282
__test_subcmds=(

0 commit comments

Comments
 (0)