Skip to content

Commit 0f3e729

Browse files
committed
feat(complete): Support to run ZSH completion as a script
1 parent cdb33b6 commit 0f3e729

10 files changed

+50
-10
lines changed

clap_complete/src/shells/zsh.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ _{name}() {{
4343
4444
{subcommand_details}
4545
46-
_{name} \"$@\"
46+
if [ \"$funcstack[1]\" = \"_{name}\" ]; then
47+
_{name} \"$@\"
48+
else
49+
compdef _{name} {name}
50+
fi
4751
",
4852
name = bin_name,
4953
initial_args = get_args_of(cmd, None),

clap_complete/tests/snapshots/aliases.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ _my-app_commands() {
3737
_describe -t commands 'my-app commands' commands "$@"
3838
}
3939

40-
_my-app "$@"
40+
if [ "$funcstack[1]" = "_my-app" ]; then
41+
_my-app "$@"
42+
else
43+
compdef _my-app my-app
44+
fi

clap_complete/tests/snapshots/basic.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,8 @@ _my-app__test_commands() {
9797
_describe -t commands 'my-app test commands' commands "$@"
9898
}
9999

100-
_my-app "$@"
100+
if [ "$funcstack[1]" = "_my-app" ]; then
101+
_my-app "$@"
102+
else
103+
compdef _my-app my-app
104+
fi

clap_complete/tests/snapshots/feature_sample.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ _my-app__test_commands() {
104104
_describe -t commands 'my-app test commands' commands "$@"
105105
}
106106

107-
_my-app "$@"
107+
if [ "$funcstack[1]" = "_my-app" ]; then
108+
_my-app "$@"
109+
else
110+
compdef _my-app my-app
111+
fi

clap_complete/tests/snapshots/quoting.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,8 @@ _my-app__help__help_commands() {
211211
_describe -t commands 'my-app help help commands' commands "$@"
212212
}
213213

214-
_my-app "$@"
214+
if [ "$funcstack[1]" = "_my-app" ]; then
215+
_my-app "$@"
216+
else
217+
compdef _my-app my-app
218+
fi

clap_complete/tests/snapshots/special_commands.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,8 @@ _my-app__test_commands() {
178178
_describe -t commands 'my-app test commands' commands "$@"
179179
}
180180

181-
_my-app "$@"
181+
if [ "$funcstack[1]" = "_my-app" ]; then
182+
_my-app "$@"
183+
else
184+
compdef _my-app my-app
185+
fi

clap_complete/tests/snapshots/sub_subcommands.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,8 @@ _my-app__test_commands() {
224224
_describe -t commands 'my-app test commands' commands "$@"
225225
}
226226

227-
_my-app "$@"
227+
if [ "$funcstack[1]" = "_my-app" ]; then
228+
_my-app "$@"
229+
else
230+
compdef _my-app my-app
231+
fi

clap_complete/tests/snapshots/two_multi_valued_arguments.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ _my-app_commands() {
2727
_describe -t commands 'my-app commands' commands "$@"
2828
}
2929

30-
_my-app "$@"
30+
if [ "$funcstack[1]" = "_my-app" ]; then
31+
_my-app "$@"
32+
else
33+
compdef _my-app my-app
34+
fi

clap_complete/tests/snapshots/value_hint.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ _my-app_commands() {
4747
_describe -t commands 'my-app commands' commands "$@"
4848
}
4949

50-
_my-app "$@"
50+
if [ "$funcstack[1]" = "_my-app" ]; then
51+
_my-app "$@"
52+
else
53+
compdef _my-app my-app
54+
fi

clap_complete/tests/snapshots/value_terminator.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ _my-app_commands() {
2727
_describe -t commands 'my-app commands' commands "$@"
2828
}
2929

30-
_my-app "$@"
30+
if [ "$funcstack[1]" = "_my-app" ]; then
31+
_my-app "$@"
32+
else
33+
compdef _my-app my-app
34+
fi

0 commit comments

Comments
 (0)