Skip to content

Commit

Permalink
Merge pull request #4899 from clubby789/zsh-subcommand-catchall
Browse files Browse the repository at this point in the history
fix: (complete, zsh) Don't emit catchall when we have subcommands
  • Loading branch information
epage authored May 12, 2023
2 parents 2d4644a + b1b2231 commit 2fd3e4c
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 1 deletion.
4 changes: 3 additions & 1 deletion clap_complete/src/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,9 @@ fn write_positionals_of(p: &Command) -> String {
}

let cardinality_value;
let cardinality = if is_multi_valued {
// If we have any subcommands, we'll emit a catch-all argument, so we shouldn't
// emit one here.
let cardinality = if is_multi_valued && !p.has_subcommands() {
match arg.get_value_terminator() {
Some(terminator) => {
cardinality_value = format!("*{}:", escape_value(terminator));
Expand Down
12 changes: 12 additions & 0 deletions clap_complete/tests/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@ fn two_multi_valued_arguments() {
name,
);
}

#[test]
fn subcommand_last() {
let name = "my-app";
let cmd = common::subcommand_last(name);
common::assert_matches_path(
"tests/snapshots/subcommand_last.bash",
clap_complete::shells::Bash,
cmd,
name,
);
}
6 changes: 6 additions & 0 deletions clap_complete/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ pub fn two_multi_valued_arguments_command(name: &'static str) -> clap::Command {
)
}

pub fn subcommand_last(name: &'static str) -> clap::Command {
clap::Command::new(name)
.arg(clap::Arg::new("free").last(true))
.subcommands([clap::Command::new("foo"), clap::Command::new("bar")])
}

pub fn assert_matches_path(
expected_path: impl AsRef<std::path::Path>,
gen: impl clap_complete::Generator,
Expand Down
12 changes: 12 additions & 0 deletions clap_complete/tests/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ fn two_multi_valued_arguments() {
name,
);
}

#[test]
fn subcommand_last() {
let name = "my-app";
let cmd = common::subcommand_last(name);
common::assert_matches_path(
"tests/snapshots/subcommand_last.elvish",
clap_complete::shells::Elvish,
cmd,
name,
);
}
12 changes: 12 additions & 0 deletions clap_complete/tests/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ fn two_multi_valued_arguments() {
name,
);
}

#[test]
fn subcommand_last() {
let name = "my-app";
let cmd = common::subcommand_last(name);
common::assert_matches_path(
"tests/snapshots/subcommand_last.fish",
clap_complete::shells::Fish,
cmd,
name,
);
}
12 changes: 12 additions & 0 deletions clap_complete/tests/powershell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ fn two_multi_valued_arguments() {
name,
);
}

#[test]
fn subcommand_last() {
let name = "my-app";
let cmd = common::subcommand_last(name);
common::assert_matches_path(
"tests/snapshots/subcommand_last.ps1",
clap_complete::shells::PowerShell,
cmd,
name,
);
}
140 changes: 140 additions & 0 deletions clap_complete/tests/snapshots/subcommand_last.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
_my-app() {
local i cur prev opts cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""

for i in ${COMP_WORDS[@]}
do
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
my__app,bar)
cmd="my__app__bar"
;;
my__app,foo)
cmd="my__app__foo"
;;
my__app,help)
cmd="my__app__help"
;;
my__app__help,bar)
cmd="my__app__help__bar"
;;
my__app__help,foo)
cmd="my__app__help__foo"
;;
my__app__help,help)
cmd="my__app__help__help"
;;
*)
;;
esac
done

case "${cmd}" in
my__app)
opts="-h --help [free] foo bar help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
my__app__bar)
opts="-h --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__foo)
opts="-h --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__help)
opts="foo bar 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__help__bar)
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__foo)
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__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
;;
esac
}

complete -F _my-app -o bashdefault -o default my-app
48 changes: 48 additions & 0 deletions clap_complete/tests/snapshots/subcommand_last.elvish
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

use builtin;
use str;

set edit:completion:arg-completer[my-app] = {|@words|
fn spaces {|n|
builtin:repeat $n ' ' | str:join ''
}
fn cand {|text desc|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
}
var command = 'my-app'
for word $words[1..-1] {
if (str:has-prefix $word '-') {
break
}
set command = $command';'$word
}
var completions = [
&'my-app'= {
cand -h 'Print help'
cand --help 'Print help'
cand foo 'foo'
cand bar 'bar'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'my-app;foo'= {
cand -h 'Print help'
cand --help 'Print help'
}
&'my-app;bar'= {
cand -h 'Print help'
cand --help 'Print help'
}
&'my-app;help'= {
cand foo 'foo'
cand bar 'bar'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'my-app;help;foo'= {
}
&'my-app;help;bar'= {
}
&'my-app;help;help'= {
}
]
$completions[$command]
}
9 changes: 9 additions & 0 deletions clap_complete/tests/snapshots/subcommand_last.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
complete -c my-app -n "__fish_use_subcommand" -s h -l help -d 'Print help'
complete -c my-app -n "__fish_use_subcommand" -f -a "foo"
complete -c my-app -n "__fish_use_subcommand" -f -a "bar"
complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c my-app -n "__fish_seen_subcommand_from foo" -s h -l help -d 'Print help'
complete -c my-app -n "__fish_seen_subcommand_from bar" -s h -l help -d 'Print help'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from foo; and not __fish_seen_subcommand_from bar; and not __fish_seen_subcommand_from help" -f -a "foo"
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from foo; and not __fish_seen_subcommand_from bar; and not __fish_seen_subcommand_from help" -f -a "bar"
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from foo; and not __fish_seen_subcommand_from bar; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
60 changes: 60 additions & 0 deletions clap_complete/tests/snapshots/subcommand_last.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$commandElements = $commandAst.CommandElements
$command = @(
'my-app'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-') -or
$element.Value -eq $wordToComplete) {
break
}
$element.Value
}) -join ';'

$completions = @(switch ($command) {
'my-app' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('foo', 'foo', [CompletionResultType]::ParameterValue, 'foo')
[CompletionResult]::new('bar', 'bar', [CompletionResultType]::ParameterValue, 'bar')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
'my-app;foo' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
break
}
'my-app;bar' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
break
}
'my-app;help' {
[CompletionResult]::new('foo', 'foo', [CompletionResultType]::ParameterValue, 'foo')
[CompletionResult]::new('bar', 'bar', [CompletionResultType]::ParameterValue, 'bar')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
'my-app;help;foo' {
break
}
'my-app;help;bar' {
break
}
'my-app;help;help' {
break
}
})

$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
Loading

0 comments on commit 2fd3e4c

Please sign in to comment.