1const HELP_ARG: &str = "--help";
2
3pub trait Args<T> {
5 fn has_help(&self) -> bool;
7
8 fn all(&self) -> Vec<T>;
10}
11
12impl<T: AsRef<str> + Clone> Args<T> for Vec<T> {
13 fn has_help(&self) -> bool {
14 self.iter().any(|arg| arg.as_ref() == HELP_ARG)
15 }
16
17 fn all(&self) -> Self {
18 self.clone()
19 }
20}
21
22impl Args<String> for pico_args::Arguments {
23 fn has_help(&self) -> bool {
24 self.clone().contains(HELP_ARG)
25 }
26
27 fn all(&self) -> Vec<String> {
28 self.clone()
29 .finish()
30 .into_iter()
31 .map(|arg| arg.to_string_lossy().into_owned())
32 .collect()
33 }
34}
35
36pub fn get() -> Vec<String> {
38 let mut args = std::env::args();
39 args.next();
40 args.collect::<Vec<String>>()
41}
42
43#[cfg(test)]
44mod tests {
45 use rstest::rstest;
46
47 use super::*;
48
49 #[rstest]
50 #[case::empty_vec(vec![], false)]
51 #[case::no_help(vec!["arg1", "arg2"], false)]
52 #[case::has_help(vec!["--help"], true)]
53 #[case::help_among_others(vec!["foo", "--help", "bar"], true)]
54 fn test_has_help_for_vec_returns_expected(#[case] args: Vec<&str>, #[case] expected: bool) {
55 pretty_assertions::assert_eq!(args.has_help(), expected);
56 }
57
58 #[rstest]
59 #[case::empty_vec(Vec::<String>::new(), Vec::<String>::new())]
60 #[case::clones_all(vec!["a".to_owned(), "b".to_owned()], vec!["a".to_owned(), "b".to_owned()])]
61 fn test_all_for_vec_returns_clone(#[case] args: Vec<String>, #[case] expected: Vec<String>) {
62 pretty_assertions::assert_eq!(args.all(), expected);
63 }
64}