diff --git a/src/matcher.rs b/src/matcher.rs index 0e7affc9..f9167f22 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -77,7 +77,7 @@ impl<'a, 'b> Matcher<'a, 'b> { if root.delegated() { positional_args = args.iter().skip(1).map(|v| v.as_str()).collect(); } else { - let mut is_rest_args_positional = false; + let mut is_rest_args_positional = false; // arg(like -f --foo) will be positional arg if let Some(arg) = args.last() { if arg.starts_with('-') { arg_comp = ArgComp::FlagOrOption; @@ -156,7 +156,7 @@ impl<'a, 'b> Matcher<'a, 'b> { &mut split_last_arg_at, &prefix, ); - } else if let Some(subcmd) = cmd.find_subcommand(arg) { + } else if let Some(subcmd) = find_subcommand(cmd, arg, &positional_args) { match_command( &mut cmds, &mut cmd_level, @@ -196,7 +196,7 @@ impl<'a, 'b> Matcher<'a, 'b> { } else { flag_option_args[cmd_level].push((arg, vec![], None)); } - } else if let Some(subcmd) = cmd.find_subcommand(arg) { + } else if let Some(subcmd) = find_subcommand(cmd, arg, &positional_args) { match_command( &mut cmds, &mut cmd_level, @@ -811,6 +811,20 @@ impl<'a, 'b> Matcher<'a, 'b> { pub(crate) type CompItem = (String, String, CompColor); +fn find_subcommand<'a>( + cmd: &'a Command, + arg: &str, + positional_args: &Vec<&str>, +) -> Option<&'a Command> { + cmd.find_subcommand(arg).and_then(|v| { + if positional_args.is_empty() { + Some(v) + } else { + None + } + }) +} + fn add_positional_arg<'a>( positional_args: &mut Vec<&'a str>, arg: &'a str, diff --git a/tests/compgen.rs b/tests/compgen.rs index 3cd35b6a..398ed8c9 100644 --- a/tests/compgen.rs +++ b/tests/compgen.rs @@ -79,6 +79,8 @@ fn subcmds() { cmda() { :; } # @cmd cmdb() { :; } +# @cmd +cmdbc() { :; } "###; snapshot_compgen!( @@ -91,6 +93,7 @@ cmdb() { :; } vec!["prog", "help", ""], vec!["prog", "help", "c"], vec!["prog", "help", "cmda", ""], + vec!["prog", "help", "cmdb"], ] ); } diff --git a/tests/snapshots/integration__compgen__subcmds.snap b/tests/snapshots/integration__compgen__subcmds.snap index 634e954f..f0bc3ab9 100644 --- a/tests/snapshots/integration__compgen__subcmds.snap +++ b/tests/snapshots/integration__compgen__subcmds.snap @@ -6,12 +6,14 @@ expression: data __argc_value=FILE /color:green cmda /color:magenta cmdb /color:magenta +cmdbc /color:magenta help /color:magenta ************ COMPGEN `prog c` ************ __argc_value=FILE /color:green cmda /color:magenta cmdb /color:magenta +cmdbc /color:magenta ************ COMPGEN `prog cmda` ************ cmda /color:magenta @@ -22,13 +24,19 @@ __argc_value=path /color:green ************ COMPGEN `prog help ` ************ cmda /color:magenta cmdb /color:magenta +cmdbc /color:magenta help /color:magenta ************ COMPGEN `prog help c` ************ cmda /color:magenta cmdb /color:magenta +cmdbc /color:magenta ************ COMPGEN `prog help cmda ` ************ +************ COMPGEN `prog help cmdb` ************ +cmdb /color:magenta +cmdbc /color:magenta +