Skip to content

Commit

Permalink
Merge pull request #4115 from talklittle/completion-help-parser-flag
Browse files Browse the repository at this point in the history
feat(complete): Add completion for help subcommands
  • Loading branch information
epage committed Aug 26, 2022
2 parents 2f4e42f + 0d0be51 commit 294868f
Show file tree
Hide file tree
Showing 33 changed files with 975 additions and 53 deletions.
6 changes: 6 additions & 0 deletions clap_complete/src/generator/utils.rs
Expand Up @@ -198,6 +198,12 @@ mod tests {
("help".to_string(), "my-cmd help".to_string()),
("config".to_string(), "my-cmd test config".to_string()),
("help".to_string(), "my-cmd test help".to_string()),
("config".to_string(), "my-cmd test help config".to_string()),
("help".to_string(), "my-cmd test help help".to_string()),
("test".to_string(), "my-cmd help test".to_string()),
("hello".to_string(), "my-cmd help hello".to_string()),
("help".to_string(), "my-cmd help help".to_string()),
("config".to_string(), "my-cmd help test config".to_string()),
]
);
}
Expand Down
30 changes: 29 additions & 1 deletion clap_complete/tests/snapshots/basic.bash
Expand Up @@ -39,7 +39,7 @@ _my-app() {
return 0
;;
my__app__help)
opts="-c [SUBCOMMAND]..."
opts="-c test help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand All @@ -52,6 +52,34 @@ _my-app() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__help__help)
opts="-c"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__help__test)
opts="-c"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__test)
opts="-d -c -h --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
Expand Down
8 changes: 8 additions & 0 deletions clap_complete/tests/snapshots/basic.elvish
Expand Up @@ -33,6 +33,14 @@ set edit:completion:arg-completer[my-app] = {|@words|
}
&'my-app;help'= {
cand -c 'c'
cand test 'Subcommand'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'my-app;help;test'= {
cand -c 'c'
}
&'my-app;help;help'= {
cand -c 'c'
}
]
$completions[$command]
Expand Down
6 changes: 5 additions & 1 deletion clap_complete/tests/snapshots/basic.fish
Expand Up @@ -6,4 +6,8 @@ complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this messag
complete -c my-app -n "__fish_seen_subcommand_from test" -s d
complete -c my-app -n "__fish_seen_subcommand_from test" -s c
complete -c my-app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help information'
complete -c my-app -n "__fish_seen_subcommand_from help" -s c
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -s c
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c my-app -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from test" -s c
complete -c my-app -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from help" -s c
10 changes: 10 additions & 0 deletions clap_complete/tests/snapshots/basic.ps1
Expand Up @@ -37,6 +37,16 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
break
}
'my-app;help' {
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
'my-app;help;test' {
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
break
}
'my-app;help;help' {
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
break
}
Expand Down
38 changes: 36 additions & 2 deletions clap_complete/tests/snapshots/basic.zsh
Expand Up @@ -39,12 +39,33 @@ _arguments "${_arguments_options[@]}" /
(help)
_arguments "${_arguments_options[@]}" /
'*-c[]' /
'*::subcommand -- The subcommand whose help message to display:' /
":: :_my-app__help_commands" /
"*::: :->help" /
&& ret=0

case $state in
(help)
words=($line[1] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:my-app-help-command-$line[1]:"
case $line[1] in
(test)
_arguments "${_arguments_options[@]}" /
'*-c[]' /
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" /
'*-c[]' /
&& ret=0
;;
esac
;;
esac
;;
esac
;;
esac
}

(( $+functions[_my-app_commands] )) ||
Expand All @@ -57,9 +78,22 @@ _my-app_commands() {
}
(( $+functions[_my-app__help_commands] )) ||
_my-app__help_commands() {
local commands; commands=()
local commands; commands=(
'test:Subcommand' /
'help:Print this message or the help of the given subcommand(s)' /
)
_describe -t commands 'my-app help commands' commands "$@"
}
(( $+functions[_my-app__help__help_commands] )) ||
_my-app__help__help_commands() {
local commands; commands=()
_describe -t commands 'my-app help help commands' commands "$@"
}
(( $+functions[_my-app__help__test_commands] )) ||
_my-app__help__test_commands() {
local commands; commands=()
_describe -t commands 'my-app help test commands' commands "$@"
}
(( $+functions[_my-app__test_commands] )) ||
_my-app__test_commands() {
local commands; commands=()
Expand Down
30 changes: 29 additions & 1 deletion clap_complete/tests/snapshots/feature_sample.bash
Expand Up @@ -39,7 +39,7 @@ _my-app() {
return 0
;;
my__app__help)
opts="[SUBCOMMAND]..."
opts="test help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand All @@ -52,6 +52,34 @@ _my-app() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__help__help)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__help__test)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__test)
opts="-h -V --case --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
Expand Down
6 changes: 6 additions & 0 deletions clap_complete/tests/snapshots/feature_sample.elvish
Expand Up @@ -37,6 +37,12 @@ set edit:completion:arg-completer[my-app] = {|@words|
cand --version 'Print version information'
}
&'my-app;help'= {
cand test 'tests things'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'my-app;help;test'= {
}
&'my-app;help;help'= {
}
]
$completions[$command]
Expand Down
2 changes: 2 additions & 0 deletions clap_complete/tests/snapshots/feature_sample.fish
Expand Up @@ -6,3 +6,5 @@ complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this messag
complete -c my-app -n "__fish_seen_subcommand_from test" -l case -d 'the case to test' -r
complete -c my-app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help information'
complete -c my-app -n "__fish_seen_subcommand_from test" -s V -l version -d 'Print version information'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'tests things'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
8 changes: 8 additions & 0 deletions clap_complete/tests/snapshots/feature_sample.ps1
Expand Up @@ -42,6 +42,14 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
break
}
'my-app;help' {
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
'my-app;help;test' {
break
}
'my-app;help;help' {
break
}
})
Expand Down
36 changes: 34 additions & 2 deletions clap_complete/tests/snapshots/feature_sample.zsh
Expand Up @@ -45,12 +45,31 @@ _arguments "${_arguments_options[@]}" /
;;
(help)
_arguments "${_arguments_options[@]}" /
'*::subcommand -- The subcommand whose help message to display:' /
":: :_my-app__help_commands" /
"*::: :->help" /
&& ret=0

case $state in
(help)
words=($line[1] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:my-app-help-command-$line[1]:"
case $line[1] in
(test)
_arguments "${_arguments_options[@]}" /
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" /
&& ret=0
;;
esac
;;
esac
;;
esac
;;
esac
}

(( $+functions[_my-app_commands] )) ||
Expand All @@ -63,9 +82,22 @@ _my-app_commands() {
}
(( $+functions[_my-app__help_commands] )) ||
_my-app__help_commands() {
local commands; commands=()
local commands; commands=(
'test:tests things' /
'help:Print this message or the help of the given subcommand(s)' /
)
_describe -t commands 'my-app help commands' commands "$@"
}
(( $+functions[_my-app__help__help_commands] )) ||
_my-app__help__help_commands() {
local commands; commands=()
_describe -t commands 'my-app help help commands' commands "$@"
}
(( $+functions[_my-app__help__test_commands] )) ||
_my-app__help__test_commands() {
local commands; commands=()
_describe -t commands 'my-app help test commands' commands "$@"
}
(( $+functions[_my-app__test_commands] )) ||
_my-app__test_commands() {
local commands; commands=()
Expand Down

0 comments on commit 294868f

Please sign in to comment.