From 535d4fcc930a4f99aa2a90054d866fb2acb7908a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sun, 4 Apr 2021 02:20:39 -0300 Subject: [PATCH] Added oof::matches_any_arg, can detect --help etc. --- oof/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/oof/src/lib.rs b/oof/src/lib.rs index bff131b..2fb5122 100644 --- a/oof/src/lib.rs +++ b/oof/src/lib.rs @@ -212,6 +212,17 @@ pub fn filter_flags( Ok((new_args, result_flags)) } +/// Says if any text matches any arg +pub fn matches_any_arg(args: &[T], texts: &[U]) -> bool +where + T: AsRef, + U: AsRef, +{ + texts + .iter() + .any(|text| args.iter().any(|arg| arg.as_ref() == text.as_ref())) +} + #[cfg(test)] mod tests { use crate::*; @@ -311,6 +322,28 @@ mod tests { // Should panic here filter_flags(vec![], &flags_info).unwrap_err(); } + + #[test] + fn test_matches_any_arg_function() { + let args = gen_args("program a -h b"); + assert!(matches_any_arg(&args, &["--help", "-h"])); + + let args = gen_args("program a b --help"); + assert!(matches_any_arg(&args, &["--help", "-h"])); + + let args = gen_args("--version program a b"); + assert!(matches_any_arg(&args, &["--version", "-v"])); + + let args = gen_args("program -v a --version b"); + assert!(matches_any_arg(&args, &["--version", "-v"])); + + // Cases without it + let args = gen_args("program a b c"); + assert!(!matches_any_arg(&args, &["--help", "-h"])); + + let args = gen_args("program a --version -v b c"); + assert!(!matches_any_arg(&args, &["--help", "-h"])); + } } /// Create a flag with long flag (?).