Skip to content

Commit

Permalink
Add has_no_matched_args to ArgMatches
Browse files Browse the repository at this point in the history
This member function provides a simple way to check if no arguments
were matched at all, in v3. The same in v2 could be achieved by
accessing the `args` field in `ArgMatches` and checking if it was
empty. However, in v3 the field was made private in the public API
and for the same reason, this method was added.
  • Loading branch information
ohsayan committed Jan 6, 2022
1 parent ef823bb commit a9593b1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/parse/matches/arg_matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,11 @@ impl ArgMatches {
true
}
}

/// Returns true if no arguments were matched
pub fn has_no_matched_args(&self) -> bool {
self.args.is_empty()
}
}

// Private methods
Expand Down Expand Up @@ -1427,4 +1432,32 @@ mod tests {
.len();
assert_eq!(l, 1);
}

#[test]
fn has_no_matched_args() {
// test with no args
let args: &[&str] = &[];
let ret = crate::App::new("test")
.arg(crate::Arg::new("super").short('s').takes_value(true))
.try_get_matches_from(args)
.unwrap();
assert!(ret.has_no_matched_args());


// test with app name
let args: &[&str] = &["test"];
let ret = crate::App::new("test")
.arg(crate::Arg::new("super").short('s').takes_value(true))
.try_get_matches_from(args)
.unwrap();
assert!(ret.has_no_matched_args());

// test with known args
let args: &[&str] = &["test", "--super", "man"];
let ret = crate::App::new("test")
.arg(crate::Arg::new("super").long("super").takes_value(true))
.try_get_matches_from(args)
.unwrap();
assert!(!ret.has_no_matched_args());
}
}

0 comments on commit a9593b1

Please sign in to comment.