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
This commit is contained in:
Erick Tryzelaar 2020-10-11 09:27:02 -07:00
parent f3da6ae16c
commit c728c85810
2 changed files with 42 additions and 1 deletions

View File

@ -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()
})
}

View File

@ -206,6 +206,28 @@ fn assert_error<T: FromArgs + Debug>(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::<Parsed>(
&["-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::<Parsed>(
&["x"],
r###"Error parsing positional argument 'n' with value 'x': invalid digit found in string
"###,
);
}
#[derive(FromArgs, Debug, PartialEq)]
/// Woot
struct WithOption {