Skip to content

Commit

Permalink
Merge #1727
Browse files Browse the repository at this point in the history
1727: Fix `-- subcommand` error r=pksunkara a=ldm0



Co-authored-by: Donough Liu <ldm2993593805@163.com>
  • Loading branch information
bors[bot] and ldm0 committed Mar 5, 2020
2 parents 233ae17 + 5b9f619 commit 19c20f7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/build/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ pub enum AppSettings {
/// **CAUTION:** This setting can interfere with [positional/free arguments], take care when
/// designing CLIs which allow inferred subcommands and have potential positional/free
/// arguments whose values could start with the same characters as subcommands. If this is the
/// case, it's recommended to use settings such as [`AppSeettings::ArgsNegateSubcommands`] in
/// case, it's recommended to use settings such as [`AppSettings::ArgsNegateSubcommands`] in
/// conjunction with this setting.
///
/// # Examples
Expand All @@ -716,7 +716,7 @@ pub enum AppSettings {
/// [`subcommands`]: ./struct..html
/// [positional/free arguments]: ./struct.Arg.html#method.index
/// [aliases]: ./struct.App.html#method.alias
/// [`AppSeettings::ArgsNegateSubcommands`]: ./enum.AppSettings.html#variant.ArgsNegateSubcommands
/// [`AppSettings::ArgsNegateSubcommands`]: ./enum.AppSettings.html#variant.ArgsNegateSubcommands
InferSubcommands,

/// Specifies that the parser should not assume the first argument passed is the binary name.
Expand Down
4 changes: 2 additions & 2 deletions src/build/arg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2929,7 +2929,7 @@ impl<'help> Arg<'help> {
/// `--option=val1,val2,val3` is three values for the `--option` argument. If you wish to
/// change the delimiter to another character you can use [`Arg::value_delimiter(char)`],
/// alternatively you can turn delimiting values **OFF** by using
/// [`Arg::unset_setting(ArgSettings::UseValueDelimiter`]
/// [`Arg::unset_setting(ArgSettings::UseValueDelimiter)`]
///
/// # Examples
///
Expand All @@ -2954,7 +2954,7 @@ impl<'help> Arg<'help> {
/// assert_eq!(m.value_of("mode"), Some("fast"));
/// ```
/// [`Arg::value_delimiter(char)`]: ./struct.Arg.html#method.value_delimiter
/// [`Arg::unset_setting(ArgSettings::UseValueDelimiter`]: ./enum.ArgSettings.html#variant.UseValueDelimiter
/// [`Arg::unset_setting(ArgSettings::UseValueDelimiter)`]: ./enum.ArgSettings.html#variant.UseValueDelimiter
/// [multiple values]: ./enum.ArgSettings.html#variant.MultipleValues
pub fn takes_value(self, tv: bool) -> Self {
if tv {
Expand Down
33 changes: 17 additions & 16 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,23 +541,24 @@ where
// get the next value from the iterator
continue;
}
}

if !(self.is_set(AS::ArgsNegateSubcommands) && self.is_set(AS::ValidArgFound)
|| self.is_set(AS::AllowExternalSubcommands)
|| self.is_set(AS::InferSubcommands))
{
let cands =
suggestions::did_you_mean(&*arg_os.to_string_lossy(), sc_names!(self.app));
if !cands.is_empty() {
let cands: Vec<_> = cands.iter().map(|cand| format!("'{}'", cand)).collect();
return Err(ClapError::invalid_subcommand(
arg_os.to_string_lossy().into_owned(),
cands.join(" or "),
self.app.bin_name.as_ref().unwrap_or(&self.app.name),
&*Usage::new(self).create_usage_with_title(&[]),
self.app.color(),
));
if !(self.is_set(AS::ArgsNegateSubcommands) && self.is_set(AS::ValidArgFound)
|| self.is_set(AS::AllowExternalSubcommands)
|| self.is_set(AS::InferSubcommands))
{
let cands =
suggestions::did_you_mean(&*arg_os.to_string_lossy(), sc_names!(self.app));
if !cands.is_empty() {
let cands: Vec<_> =
cands.iter().map(|cand| format!("'{}'", cand)).collect();
return Err(ClapError::invalid_subcommand(
arg_os.to_string_lossy().into_owned(),
cands.join(" or "),
self.app.bin_name.as_ref().unwrap_or(&self.app.name),
&*Usage::new(self).create_usage_with_title(&[]),
self.app.color(),
));
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,12 @@ fn issue_1161_multiple_hyphen_hyphen() {

assert_eq!(expected, actual);
}

#[test]
fn issue_1722_not_emit_error_when_arg_follows_similar_to_a_subcommand() {
let m = App::new("myprog")
.subcommand(App::new("subcommand"))
.arg(Arg::with_name("argument"))
.try_get_matches_from(vec!["myprog", "--", "subcommand"]);
assert_eq!(m.unwrap().value_of("argument"), Some("subcommand"));
}

1 comment on commit 19c20f7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 19c20f7 Previous: 233ae17 Ratio
build_empty 111 ns/iter (+/- 1) 92 ns/iter (+/- 1) 1.2
parse_empty 1054 ns/iter (+/- 18) 840 ns/iter (+/- 39) 1.3
parse_simple_with_complex 4181 ns/iter (+/- 65) 3247 ns/iter (+/- 110) 1.3
parse_simple_with_pos 3160 ns/iter (+/- 65) 2680 ns/iter (+/- 52) 1.2
parse_simple_with_opt 3355 ns/iter (+/- 53) 2743 ns/iter (+/- 70) 1.2
parse_simple_with_flag 3059 ns/iter (+/- 60) 2526 ns/iter (+/- 135) 1.2
build_with_pos_ref 277 ns/iter (+/- 6) 202 ns/iter (+/- 8) 1.4
build_with_pos 195 ns/iter (+/- 3) 158 ns/iter (+/- 4) 1.2
build_with_opt_ref 359 ns/iter (+/- 4) 271 ns/iter (+/- 7) 1.3
build_with_opt 294 ns/iter (+/- 6) 218 ns/iter (+/- 5) 1.3
build_with_flag_ref 318 ns/iter (+/- 4) 250 ns/iter (+/- 10) 1.3
build_with_flag 256 ns/iter (+/- 5) 210 ns/iter (+/- 9) 1.2
build_simple 809 ns/iter (+/- 14) 721 ns/iter (+/- 21) 1.1
build_from_usage 4769 ns/iter (+/- 103) 3816 ns/iter (+/- 170) 1.2
build_from_builder 2646 ns/iter (+/- 44) 2187 ns/iter (+/- 66) 1.2
build_from_macros 2772 ns/iter (+/- 71) 2062 ns/iter (+/- 74) 1.3
parse_complex 8133 ns/iter (+/- 185) 7120 ns/iter (+/- 311) 1.1
parse_complex_with_flag 9421 ns/iter (+/- 223) 7186 ns/iter (+/- 288) 1.3
parse_complex_with_opt 9278 ns/iter (+/- 119) 7595 ns/iter (+/- 354) 1.2
parse_complex_with_pos 9554 ns/iter (+/- 161) 7034 ns/iter (+/- 175) 1.4
parse_complex_with_sc 11121 ns/iter (+/- 251) 8967 ns/iter (+/- 289) 1.2
parse_complex_with_sc_flag 12071 ns/iter (+/- 206) 9644 ns/iter (+/- 442) 1.3
parse_complex_with_sc_opt 12395 ns/iter (+/- 265) 10407 ns/iter (+/- 408) 1.2
parse_complex_with_sc_pos 11767 ns/iter (+/- 214) 9870 ns/iter (+/- 268) 1.2
parse_complex1 15562 ns/iter (+/- 479) 12193 ns/iter (+/- 319) 1.3
parse_complex2 17882 ns/iter (+/- 472) 14205 ns/iter (+/- 423) 1.3
parse_args_negate_scs 17277 ns/iter (+/- 352) 13134 ns/iter (+/- 742) 1.3
parse_complex_with_sc_complex 13725 ns/iter (+/- 288) 11935 ns/iter (+/- 518) 1.1
example1 17058 ns/iter (+/- 295) 13870 ns/iter (+/- 726) 1.2
example2 425550 ns/iter (+/- 49,057) 390855 ns/iter (+/- 45,464) 1.1
example3 18816 ns/iter (+/- 374) 14934 ns/iter (+/- 605) 1.3
example4 13105 ns/iter (+/- 233) 9991 ns/iter (+/- 363) 1.3
example5 517820 ns/iter (+/- 39,343) 489692 ns/iter (+/- 35,523) 1.1
example6 9994 ns/iter (+/- 159) 7811 ns/iter (+/- 198) 1.3
example7 14435 ns/iter (+/- 311) 11006 ns/iter (+/- 299) 1.3
example8 14502 ns/iter (+/- 287) 11404 ns/iter (+/- 564) 1.3
example10 459988 ns/iter (+/- 43,442) 444941 ns/iter (+/- 38,129) 1.0
example4_template 12363 ns/iter (+/- 331) 10049 ns/iter (+/- 305) 1.2
build_rg_with_short_help 11990 ns/iter (+/- 215) 9446 ns/iter (+/- 232) 1.3
build_rg_with_long_help 12901 ns/iter (+/- 231) 11426 ns/iter (+/- 377) 1.1
write_rg_short_help 114229 ns/iter (+/- 1,920) 96313 ns/iter (+/- 2,852) 1.2
write_rg_long_help 261900 ns/iter (+/- 8,646) 209295 ns/iter (+/- 8,741) 1.3
parse_rg 26814 ns/iter (+/- 520) 21755 ns/iter (+/- 495) 1.2
parse_rg_with_complex 37916 ns/iter (+/- 746) 29925 ns/iter (+/- 1,100) 1.3
parse_rg_with_lots 872343 ns/iter (+/- 13,292) 663721 ns/iter (+/- 24,148) 1.3
build_rustup 14465 ns/iter (+/- 307) 12011 ns/iter (+/- 369) 1.2
parse_rustup 18205 ns/iter (+/- 357) 15383 ns/iter (+/- 635) 1.2
parse_rustup_with_sc 18361 ns/iter (+/- 394) 14692 ns/iter (+/- 599) 1.2

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.