Skip to content

Commit

Permalink
feat(complete): generate completions for visible aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
pzmarzly committed Apr 27, 2024
1 parent 0cc63a9 commit 5c35422
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
9 changes: 8 additions & 1 deletion clap_complete/src/generator/utils.rs
Expand Up @@ -47,8 +47,15 @@ pub fn subcommands(p: &Command) -> Vec<(String, String)> {
sc.get_name(),
sc_bin_name
);

subcmds.push((sc.get_name().to_string(), sc_bin_name.to_string()));

for alias in sc.get_visible_aliases() {
debug!(
"subcommands:iter: alias={}, bin_name={}",
alias, sc_bin_name
);
subcmds.push((alias.to_string(), sc_bin_name.to_string()));
}
}

subcmds
Expand Down
16 changes: 15 additions & 1 deletion clap_complete/tests/snapshots/sub_subcommands.bash
Expand Up @@ -55,7 +55,7 @@ _my-app() {

case "${cmd}" in
my__app)
opts="-C -c -h -V --conf --config --help --version [file] first second test some_cmd help"
opts="-C -c -h -V --conf --config --help --version [file] first second test some_cmd some_cmd_alias help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -152,6 +152,20 @@ _my-app() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__some_cmd)
opts="-h -V --help --version sub_cmd help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__some_cmd__help)
opts="sub_cmd help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
Expand Down
54 changes: 54 additions & 0 deletions clap_complete/tests/snapshots/sub_subcommands.zsh
Expand Up @@ -97,6 +97,60 @@ esac
;;
esac
;;
(some_cmd_alias)
_arguments "${_arguments_options[@]}" \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
":: :_my-app__some_cmd_commands" \
"*::: :->some_cmd" \
&& ret=0

case $state in
(some_cmd)
words=($line[1] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:my-app-some_cmd-command-$line[1]:"
case $line[1] in
(sub_cmd)
_arguments "${_arguments_options[@]}" \
'--config=[the other case to test]: :((Lest\ quotes,\ aren'\''t\ escaped.\:"help,with,comma"
Second\ to\ trigger\ display\ of\ options\:""))' \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'-V[Print version]' \
'--version[Print version]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
":: :_my-app__some_cmd__help_commands" \
"*::: :->help" \
&& ret=0

case $state in
(help)
words=($line[1] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:my-app-some_cmd-help-command-$line[1]:"
case $line[1] in
(sub_cmd)
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
esac
;;
esac
;;
esac
;;
esac
;;
(help)
_arguments "${_arguments_options[@]}" \
":: :_my-app__help_commands" \
Expand Down

0 comments on commit 5c35422

Please sign in to comment.