Skip to content

Commit

Permalink
exactly match a subcommand when using the infersubcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
rleungx committed Jun 13, 2018
1 parent 0db4f17 commit 3943ed3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,13 +656,12 @@ where

// Checks if the arg matches a subcommand name, or any of it's aliases (if defined)
fn possible_subcommand(&self, arg_os: &OsStr) -> (bool, Option<&str>) {
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
use std::os::unix::ffi::OsStrExt;
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
use osstringext::OsStrExt3;
debugln!("Parser::possible_subcommand: arg={:?}", arg_os);
fn starts(h: &str, n: &OsStr) -> bool {
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
use std::os::unix::ffi::OsStrExt;
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
use osstringext::OsStrExt3;

let n_bytes = n.as_bytes();
let h_bytes = OsStr::new(h).as_bytes();

Expand Down Expand Up @@ -694,6 +693,12 @@ where
.map(|sc| &sc.p.meta.name)
.collect::<Vec<_>>();

for sc in &v {
if OsStr::new(sc).as_bytes() == arg_os.as_bytes() {
return (true, Some(sc));
}
}

if v.len() == 1 {
return (true, Some(v[0]));
}
Expand Down
13 changes: 13 additions & 0 deletions tests/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ fn infer_subcommands_pass_close() {
assert_eq!(m.subcommand_name(), Some("test"));
}

#[test]
fn infer_subcommands_pass_exact_match() {
let m = App::new("prog")
.setting(AppSettings::InferSubcommands)
.subcommand(SubCommand::with_name("test"))
.subcommand(SubCommand::with_name("testa"))
.subcommand(SubCommand::with_name("testb"))
.get_matches_from(vec![
"prog", "test"
]);
assert_eq!(m.subcommand_name(), Some("test"));
}

#[cfg(feature = "suggestions")]
#[test]
fn infer_subcommands_fail_suggestions() {
Expand Down

0 comments on commit 3943ed3

Please sign in to comment.