From ec0a5a2b69aa92625034846209e350d2abd65c92 Mon Sep 17 00:00:00 2001 From: Zaba505 Date: Sun, 13 Sep 2020 19:42:10 -0400 Subject: [PATCH] fix(flagset): recognize single dash as a positional arg (#13) * test(flagset): add case for single dash position arg * fix(flagset): recognize single dash as positional arg --- src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e44dca8..90e2a93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -329,7 +329,7 @@ impl FlagSet { let mut av = args .into_iter() - .flat_map(|arg| arg.chars().chain(" ".chars())) + .flat_map(|arg| arg.trim_matches(' ').chars().chain(" ".chars())) .peekable(); loop { @@ -344,7 +344,7 @@ impl FlagSet { '-' => { av.next(); let arg = av.peek(); - if arg.is_none() { + if arg.is_none() || *arg.unwrap() == ' ' { av.next(); self.args.push("-".to_string()); return Ok(()); @@ -974,6 +974,19 @@ mod tests { assert_eq!(args[1], "world"); } + #[test] + fn parse_single_dash_stdin() { + let mut flags = FlagSet::new("test"); + + if let Err(err) = flags.parse(vec!["-"]) { + panic!(err); + } + + let args = flags.args(); + assert_eq!(args.len(), 1); + assert_eq!(args[0], "-"); + } + #[test] fn parse_terminate_flags() { let mut flags = FlagSet::new("test");