From 23f7b74da267ce9cd833fb935daedbebe1d8fa47 Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Sun, 10 Jul 2022 10:31:08 -0500 Subject: [PATCH] Fix `is_verbose` accepting any 1-character argument. --- CHANGELOG.md | 4 ++++ src/cli.rs | 28 ++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e6259955..f08a4118c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] - ReleaseDate +## Fixed + +- #930 - fix any parsing of 1-character subcommands + ## [v0.2.3] - 2022-07-09 ### Added diff --git a/src/cli.rs b/src/cli.rs index 6f9d8f889..c4dae060a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -72,10 +72,13 @@ fn is_verbose(arg: &str) -> bool { match arg { "--verbose" => true, // cargo can handle any number of "v"s - a => a - .get(1..) - .map(|a| a.chars().all(|x| x == 'v')) - .unwrap_or_default(), + a => { + a.starts_with('-') + && a.len() >= 2 + && a.get(1..) + .map(|a| a.chars().all(|x| x == 'v')) + .unwrap_or_default() + } } } @@ -238,3 +241,20 @@ pub fn parse(target_list: &TargetList) -> Result { color, }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn is_verbose_test() { + assert!(!is_verbose("b")); + assert!(!is_verbose("x")); + assert!(!is_verbose("-")); + assert!(!is_verbose("-V")); + assert!(is_verbose("-v")); + assert!(is_verbose("--verbose")); + assert!(is_verbose("-vvvv")); + assert!(!is_verbose("-version")); + } +}