Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add has_no_matched_args to ArgMatches #3265

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 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,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());
}
}