Skip to content

Commit

Permalink
fix: don't match subcmd if prev arg have not match any subcmd (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Aug 11, 2023
1 parent ceb5bea commit e3cbf76
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ fn subcmds() {
cmda() { :; }
# @cmd
cmdb() { :; }
# @cmd
cmdbc() { :; }
"###;

snapshot_compgen!(
Expand All @@ -91,6 +93,7 @@ cmdb() { :; }
vec!["prog", "help", ""],
vec!["prog", "help", "c"],
vec!["prog", "help", "cmda", ""],
vec!["prog", "help", "cmdb"],
]
);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/snapshots/integration__compgen__subcmds.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


0 comments on commit e3cbf76

Please sign in to comment.