Skip to content

Commit 5951b52

Browse files
committed
Feat: added external subcommands test to suite
1 parent 46c49c7 commit 5951b52

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#compdef my-app
2+
3+
autoload -U is-at-least
4+
5+
_my-app() {
6+
typeset -A opt_args
7+
typeset -a _arguments_options
8+
local ret=1
9+
10+
if is-at-least 5.2; then
11+
_arguments_options=(-s -S -C)
12+
else
13+
_arguments_options=(-s -C)
14+
fi
15+
16+
local context curcontext="$curcontext" state line
17+
_arguments "${_arguments_options[@]}" : \
18+
'-h[Print help]' \
19+
'--help[Print help]' \
20+
":: :_my-app_commands" \
21+
"*::: :->my-app" \
22+
&& ret=0
23+
case $state in
24+
(my-app)
25+
words=($line[1] "${words[@]}")
26+
(( CURRENT += 1 ))
27+
curcontext="${curcontext%:*:*}:my-app-command-$line[1]:"
28+
case $line[1] in
29+
(external)
30+
_arguments "${_arguments_options[@]}" : \
31+
'-h[Print help]' \
32+
'--help[Print help]' \
33+
"*::external_command:_default" \
34+
&& ret=0
35+
;;
36+
(help)
37+
_arguments "${_arguments_options[@]}" : \
38+
":: :_my-app__help_commands" \
39+
"*::: :->help" \
40+
&& ret=0
41+
42+
case $state in
43+
(help)
44+
words=($line[1] "${words[@]}")
45+
(( CURRENT += 1 ))
46+
curcontext="${curcontext%:*:*}:my-app-help-command-$line[1]:"
47+
case $line[1] in
48+
(external)
49+
_arguments "${_arguments_options[@]}" : \
50+
&& ret=0
51+
;;
52+
(help)
53+
_arguments "${_arguments_options[@]}" : \
54+
&& ret=0
55+
;;
56+
esac
57+
;;
58+
esac
59+
;;
60+
esac
61+
;;
62+
esac
63+
}
64+
65+
(( $+functions[_my-app_commands] )) ||
66+
_my-app_commands() {
67+
local commands; commands=(
68+
'external:An external subcommand' \
69+
'help:Print this message or the help of the given subcommand(s)' \
70+
)
71+
_describe -t commands 'my-app commands' commands "$@"
72+
}
73+
(( $+functions[_my-app__external_commands] )) ||
74+
_my-app__external_commands() {
75+
local commands; commands=()
76+
_describe -t commands 'my-app external commands' commands "$@"
77+
}
78+
(( $+functions[_my-app__help_commands] )) ||
79+
_my-app__help_commands() {
80+
local commands; commands=(
81+
'external:An external subcommand' \
82+
'help:Print this message or the help of the given subcommand(s)' \
83+
)
84+
_describe -t commands 'my-app help commands' commands "$@"
85+
}
86+
(( $+functions[_my-app__help__external_commands] )) ||
87+
_my-app__help__external_commands() {
88+
local commands; commands=()
89+
_describe -t commands 'my-app help external commands' commands "$@"
90+
}
91+
(( $+functions[_my-app__help__help_commands] )) ||
92+
_my-app__help__help_commands() {
93+
local commands; commands=()
94+
_describe -t commands 'my-app help help commands' commands "$@"
95+
}
96+
97+
if [ "$funcstack[1]" = "_my-app" ]; then
98+
_my-app "$@"
99+
else
100+
compdef _my-app my-app
101+
fi

clap_complete/tests/testsuite/common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ pub(crate) fn special_commands_command(name: &'static str) -> clap::Command {
7575
.subcommand(clap::Command::new("some-hidden-cmd").hide(true))
7676
}
7777

78+
pub(crate) fn external_subcommand(name: &'static str) -> clap::Command {
79+
clap::Command::new(name)
80+
.subcommand(
81+
clap::Command::new("external")
82+
.allow_external_subcommands(true)
83+
.about("An external subcommand")
84+
)
85+
}
86+
7887
pub(crate) fn quoting_command(name: &'static str) -> clap::Command {
7988
clap::Command::new(name)
8089
.version("3.0")

clap_complete/tests/testsuite/zsh.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ fn sub_subcommands() {
8282
);
8383
}
8484

85+
#[test]
86+
fn external_subcommands() {
87+
let name = "my-app";
88+
let cmd = common::external_subcommand(name);
89+
common::assert_matches(
90+
snapbox::file!["../snapshots/external_subcommands.zsh"],
91+
clap_complete::shells::Zsh,
92+
cmd,
93+
name,
94+
);
95+
}
96+
8597
#[test]
8698
fn custom_bin_name() {
8799
let name = "my-app";

0 commit comments

Comments
 (0)