From c728c85810957bbf70e0b3287dc4e3c12a4fff5f Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 11 Oct 2020 09:27:02 -0700 Subject: [PATCH] Fix quoting in errors when parsing arguments This closes quotes in error messages when parsing arguments. Test: Added tests to make sure the error message when parsing arguments is as expected, as well as added a test to check parsing options. Closes #52 --- src/lib.rs | 3 ++- tests/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ae16a6a..9dc662a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -445,7 +445,8 @@ pub fn parse_positional( ) -> Result<(), String> { let (slot, name) = positional; slot.fill_slot(arg).map_err(|s| { - ["Error parsing positional argument '", name, "' with value '", arg, ": ", &s].concat() + ["Error parsing positional argument '", name, "' with value '", arg, "': ", &s, "\n"] + .concat() }) } diff --git a/tests/lib.rs b/tests/lib.rs index 428015c..b27f3ef 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -206,6 +206,28 @@ fn assert_error(args: &[&str], err_msg: &str) { e.status.expect_err("error had a positive status"); } +mod options { + use super::*; + + #[derive(argh::FromArgs, Debug, PartialEq)] + /// Woot + struct Parsed { + #[argh(option, short = 'n')] + /// fooey + n: usize, + } + + #[test] + fn parsed() { + assert_output(&["-n", "5"], Parsed { n: 5 }); + assert_error::( + &["-n", "x"], + r###"Error parsing option '-n' with value 'x': invalid digit found in string +"###, + ); + } +} + mod positional { use super::*; @@ -304,6 +326,24 @@ Options: ); } + #[derive(argh::FromArgs, Debug, PartialEq)] + /// Woot + struct Parsed { + #[argh(positional)] + /// fooey + n: usize, + } + + #[test] + fn parsed() { + assert_output(&["5"], Parsed { n: 5 }); + assert_error::( + &["x"], + r###"Error parsing positional argument 'n' with value 'x': invalid digit found in string +"###, + ); + } + #[derive(FromArgs, Debug, PartialEq)] /// Woot struct WithOption {