From 0fc3fcb9016b06546552fb1030d543f9069ae7f0 Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Wed, 5 Jan 2022 20:42:46 -0800 Subject: [PATCH] feat: Add `has_no_matched_args` to `ArgMatches` 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. --- src/parse/matches/arg_matches.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/parse/matches/arg_matches.rs b/src/parse/matches/arg_matches.rs index cc6c8781263..a9f74a3e9a5 100644 --- a/src/parse/matches/arg_matches.rs +++ b/src/parse/matches/arg_matches.rs @@ -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 @@ -1427,4 +1432,31 @@ 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()); + } }