test(derive): Provide better error info

`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked.  I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.

So let's switch to `Parse::try_parse_from`.

Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context

USAGE:
    clap_derive [OPTIONS]

For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok

failures:

---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
 context", None), ("\n\n", None), ("USAGE:\n    clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    vector

test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--test arg_enum'
```
This commit is contained in:
Ed Page 2021-10-30 09:55:50 -05:00
parent 2ce916133b
commit 4b7ed54d7e
19 changed files with 362 additions and 220 deletions

View file

@ -27,13 +27,13 @@ fn basic() {
Opt { Opt {
arg: ArgChoice::Foo arg: ArgChoice::Foo
}, },
Opt::parse_from(&["", "foo"]) Opt::try_parse_from(&["", "foo"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::Bar arg: ArgChoice::Bar
}, },
Opt::parse_from(&["", "bar"]) Opt::try_parse_from(&["", "bar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "fOo"]).is_err());
} }
@ -68,19 +68,19 @@ fn default_value() {
Opt { Opt {
arg: ArgChoice::Foo arg: ArgChoice::Foo
}, },
Opt::parse_from(&["", "foo"]) Opt::try_parse_from(&["", "foo"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::Bar arg: ArgChoice::Bar
}, },
Opt::parse_from(&["", "bar"]) Opt::try_parse_from(&["", "bar"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::Bar arg: ArgChoice::Bar
}, },
Opt::parse_from(&[""]) Opt::try_parse_from(&[""]).unwrap()
); );
} }
@ -103,13 +103,13 @@ fn multi_word_is_renamed_kebab() {
Opt { Opt {
arg: ArgChoice::FooBar arg: ArgChoice::FooBar
}, },
Opt::parse_from(&["", "foo-bar"]) Opt::try_parse_from(&["", "foo-bar"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::BAR_BAZ arg: ArgChoice::BAR_BAZ
}, },
Opt::parse_from(&["", "bar-baz"]) Opt::try_parse_from(&["", "bar-baz"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err()); assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
} }
@ -132,7 +132,7 @@ fn variant_with_defined_casing() {
Opt { Opt {
arg: ArgChoice::FooBar arg: ArgChoice::FooBar
}, },
Opt::parse_from(&["", "FOO_BAR"]) Opt::try_parse_from(&["", "FOO_BAR"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err()); assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
} }
@ -155,7 +155,7 @@ fn casing_is_propagated_from_parent() {
Opt { Opt {
arg: ArgChoice::FooBar arg: ArgChoice::FooBar
}, },
Opt::parse_from(&["", "FOO_BAR"]) Opt::try_parse_from(&["", "FOO_BAR"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err()); assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
} }
@ -179,7 +179,7 @@ fn casing_propagation_is_overridden() {
Opt { Opt {
arg: ArgChoice::FooBar arg: ArgChoice::FooBar
}, },
Opt::parse_from(&["", "fooBar"]) Opt::try_parse_from(&["", "fooBar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err()); assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
assert!(Opt::try_parse_from(&["", "FOO_BAR"]).is_err()); assert!(Opt::try_parse_from(&["", "FOO_BAR"]).is_err());
@ -202,13 +202,13 @@ fn case_insensitive() {
Opt { Opt {
arg: ArgChoice::Foo arg: ArgChoice::Foo
}, },
Opt::parse_from(&["", "foo"]) Opt::try_parse_from(&["", "foo"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::Foo arg: ArgChoice::Foo
}, },
Opt::parse_from(&["", "fOo"]) Opt::try_parse_from(&["", "fOo"]).unwrap()
); );
} }
@ -229,7 +229,7 @@ fn case_insensitive_set_to_false() {
Opt { Opt {
arg: ArgChoice::Foo arg: ArgChoice::Foo
}, },
Opt::parse_from(&["", "foo"]) Opt::try_parse_from(&["", "foo"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "fOo"]).is_err());
} }
@ -252,13 +252,13 @@ fn alias() {
Opt { Opt {
arg: ArgChoice::TOTP arg: ArgChoice::TOTP
}, },
Opt::parse_from(&["", "totp"]) Opt::try_parse_from(&["", "totp"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::TOTP arg: ArgChoice::TOTP
}, },
Opt::parse_from(&["", "TOTP"]) Opt::try_parse_from(&["", "TOTP"]).unwrap()
); );
} }
@ -280,19 +280,19 @@ fn multiple_alias() {
Opt { Opt {
arg: ArgChoice::TOTP arg: ArgChoice::TOTP
}, },
Opt::parse_from(&["", "totp"]) Opt::try_parse_from(&["", "totp"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::TOTP arg: ArgChoice::TOTP
}, },
Opt::parse_from(&["", "TOTP"]) Opt::try_parse_from(&["", "TOTP"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: ArgChoice::TOTP arg: ArgChoice::TOTP
}, },
Opt::parse_from(&["", "t"]) Opt::try_parse_from(&["", "t"]).unwrap()
); );
} }
@ -310,18 +310,18 @@ fn option() {
arg: Option<ArgChoice>, arg: Option<ArgChoice>,
} }
assert_eq!(Opt { arg: None }, Opt::parse_from(&[""])); assert_eq!(Opt { arg: None }, Opt::try_parse_from(&[""]).unwrap());
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(ArgChoice::Foo) arg: Some(ArgChoice::Foo)
}, },
Opt::parse_from(&["", "foo"]) Opt::try_parse_from(&["", "foo"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(ArgChoice::Bar) arg: Some(ArgChoice::Bar)
}, },
Opt::parse_from(&["", "bar"]) Opt::try_parse_from(&["", "bar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "fOo"]).is_err());
} }
@ -340,19 +340,22 @@ fn option_option() {
arg: Option<Option<ArgChoice>>, arg: Option<Option<ArgChoice>>,
} }
assert_eq!(Opt { arg: None }, Opt::parse_from(&[""])); assert_eq!(Opt { arg: None }, Opt::try_parse_from(&[""]).unwrap());
assert_eq!(Opt { arg: Some(None) }, Opt::parse_from(&["", "--arg"])); assert_eq!(
Opt { arg: Some(None) },
Opt::try_parse_from(&["", "--arg"]).unwrap()
);
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(Some(ArgChoice::Foo)) arg: Some(Some(ArgChoice::Foo))
}, },
Opt::parse_from(&["", "--arg", "foo"]) Opt::try_parse_from(&["", "--arg", "foo"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(Some(ArgChoice::Bar)) arg: Some(Some(ArgChoice::Bar))
}, },
Opt::parse_from(&["", "--arg", "bar"]) Opt::try_parse_from(&["", "--arg", "bar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "--arg", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "--arg", "fOo"]).is_err());
} }
@ -371,18 +374,18 @@ fn vector() {
arg: Vec<ArgChoice>, arg: Vec<ArgChoice>,
} }
assert_eq!(Opt { arg: vec![] }, Opt::parse_from(&[""])); assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&[""]).unwrap());
assert_eq!( assert_eq!(
Opt { Opt {
arg: vec![ArgChoice::Foo] arg: vec![ArgChoice::Foo]
}, },
Opt::parse_from(&["", "-a", "foo"]) Opt::try_parse_from(&["", "-a", "foo"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: vec![ArgChoice::Foo, ArgChoice::Bar] arg: vec![ArgChoice::Foo, ArgChoice::Bar]
}, },
Opt::parse_from(&["", "-a", "foo", "bar"]) Opt::try_parse_from(&["", "-a", "foo", "bar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
} }
@ -401,19 +404,22 @@ fn option_vector() {
arg: Option<Vec<ArgChoice>>, arg: Option<Vec<ArgChoice>>,
} }
assert_eq!(Opt { arg: None }, Opt::parse_from(&[""])); assert_eq!(Opt { arg: None }, Opt::try_parse_from(&[""]).unwrap());
assert_eq!(Opt { arg: Some(vec![]) }, Opt::parse_from(&["", "-a"])); assert_eq!(
Opt { arg: Some(vec![]) },
Opt::try_parse_from(&["", "-a"]).unwrap()
);
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![ArgChoice::Foo]) arg: Some(vec![ArgChoice::Foo])
}, },
Opt::parse_from(&["", "-a", "foo"]) Opt::try_parse_from(&["", "-a", "foo"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![ArgChoice::Foo, ArgChoice::Bar]) arg: Some(vec![ArgChoice::Foo, ArgChoice::Bar])
}, },
Opt::parse_from(&["", "-a", "foo", "bar"]) Opt::try_parse_from(&["", "-a", "foo", "bar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
} }

View file

@ -9,7 +9,7 @@ fn test_single_word_enum_variant_is_default_renamed_into_kebab_case() {
assert_eq!( assert_eq!(
Opt::Command { foo: 0 }, Opt::Command { foo: 0 },
Opt::parse_from(&["test", "command", "0"]) Opt::try_parse_from(&["test", "command", "0"]).unwrap()
); );
} }
@ -22,7 +22,7 @@ fn test_multi_word_enum_variant_is_renamed() {
assert_eq!( assert_eq!(
Opt::FirstCommand { foo: 0 }, Opt::FirstCommand { foo: 0 },
Opt::parse_from(&["test", "first-command", "0"]) Opt::try_parse_from(&["test", "first-command", "0"]).unwrap()
); );
} }
@ -37,7 +37,7 @@ fn test_standalone_long_generates_kebab_case() {
assert_eq!( assert_eq!(
Opt { FOO_OPTION: true }, Opt { FOO_OPTION: true },
Opt::parse_from(&["test", "--foo-option"]) Opt::try_parse_from(&["test", "--foo-option"]).unwrap()
); );
} }
@ -51,7 +51,7 @@ fn test_custom_long_overwrites_default_name() {
assert_eq!( assert_eq!(
Opt { foo_option: true }, Opt { foo_option: true },
Opt::parse_from(&["test", "--foo"]) Opt::try_parse_from(&["test", "--foo"]).unwrap()
); );
} }
@ -65,7 +65,7 @@ fn test_standalone_long_uses_previous_defined_custom_name() {
assert_eq!( assert_eq!(
Opt { foo_option: true }, Opt { foo_option: true },
Opt::parse_from(&["test", "--foo"]) Opt::try_parse_from(&["test", "--foo"]).unwrap()
); );
} }
@ -79,7 +79,7 @@ fn test_standalone_long_ignores_afterwards_defined_custom_name() {
assert_eq!( assert_eq!(
Opt { foo_option: true }, Opt { foo_option: true },
Opt::parse_from(&["test", "--foo-option"]) Opt::try_parse_from(&["test", "--foo-option"]).unwrap()
); );
} }
@ -92,7 +92,10 @@ fn test_standalone_short_generates_kebab_case() {
FOO_OPTION: bool, FOO_OPTION: bool,
} }
assert_eq!(Opt { FOO_OPTION: true }, Opt::parse_from(&["test", "-f"])); assert_eq!(
Opt { FOO_OPTION: true },
Opt::try_parse_from(&["test", "-f"]).unwrap()
);
} }
#[test] #[test]
@ -103,7 +106,10 @@ fn test_custom_short_overwrites_default_name() {
foo_option: bool, foo_option: bool,
} }
assert_eq!(Opt { foo_option: true }, Opt::parse_from(&["test", "-o"])); assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "-o"]).unwrap()
);
} }
#[test] #[test]
@ -114,7 +120,10 @@ fn test_standalone_short_uses_previous_defined_custom_name() {
foo_option: bool, foo_option: bool,
} }
assert_eq!(Opt { foo_option: true }, Opt::parse_from(&["test", "-o"])); assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "-o"]).unwrap()
);
} }
#[test] #[test]
@ -125,7 +134,10 @@ fn test_standalone_short_ignores_afterwards_defined_custom_name() {
foo_option: bool, foo_option: bool,
} }
assert_eq!(Opt { foo_option: true }, Opt::parse_from(&["test", "-f"])); assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "-f"]).unwrap()
);
} }
#[test] #[test]
@ -138,7 +150,7 @@ fn test_standalone_long_uses_previous_defined_casing() {
assert_eq!( assert_eq!(
Opt { foo_option: true }, Opt { foo_option: true },
Opt::parse_from(&["test", "--FOO_OPTION"]) Opt::try_parse_from(&["test", "--FOO_OPTION"]).unwrap()
); );
} }
@ -150,7 +162,10 @@ fn test_standalone_short_uses_previous_defined_casing() {
foo_option: bool, foo_option: bool,
} }
assert_eq!(Opt { foo_option: true }, Opt::parse_from(&["test", "-F"])); assert_eq!(
Opt { foo_option: true },
Opt::try_parse_from(&["test", "-F"]).unwrap()
);
} }
#[test] #[test]
@ -164,7 +179,7 @@ fn test_standalone_long_works_with_verbatim_casing() {
assert_eq!( assert_eq!(
Opt { _fOO_oPtiON: true }, Opt { _fOO_oPtiON: true },
Opt::parse_from(&["test", "--_fOO_oPtiON"]) Opt::try_parse_from(&["test", "--_fOO_oPtiON"]).unwrap()
); );
} }
@ -176,7 +191,10 @@ fn test_standalone_short_works_with_verbatim_casing() {
_foo: bool, _foo: bool,
} }
assert_eq!(Opt { _foo: true }, Opt::parse_from(&["test", "-_"])); assert_eq!(
Opt { _foo: true },
Opt::try_parse_from(&["test", "-_"]).unwrap()
);
} }
#[test] #[test]
@ -188,7 +206,10 @@ fn test_rename_all_is_propagated_from_struct_to_fields() {
foo: bool, foo: bool,
} }
assert_eq!(Opt { foo: true }, Opt::parse_from(&["test", "--FOO"])); assert_eq!(
Opt { foo: true },
Opt::try_parse_from(&["test", "--FOO"]).unwrap()
);
} }
#[test] #[test]
@ -210,7 +231,7 @@ fn test_rename_all_is_not_propagated_from_struct_into_flattened() {
Opt { Opt {
foo: Foo { foo: true } foo: Foo { foo: true }
}, },
Opt::parse_from(&["test", "--foo"]) Opt::try_parse_from(&["test", "--foo"]).unwrap()
); );
} }
@ -235,7 +256,7 @@ fn test_rename_all_is_not_propagated_from_struct_into_subcommand() {
Opt { Opt {
foo: Foo::Command { foo: true } foo: Foo::Command { foo: true }
}, },
Opt::parse_from(&["test", "command", "--foo"]) Opt::try_parse_from(&["test", "command", "--foo"]).unwrap()
); );
} }
@ -253,12 +274,12 @@ fn test_rename_all_is_propagated_from_enum_to_variants_and_their_fields() {
assert_eq!( assert_eq!(
Opt::FirstVariant, Opt::FirstVariant,
Opt::parse_from(&["test", "FIRST_VARIANT"]) Opt::try_parse_from(&["test", "FIRST_VARIANT"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt::SecondVariant { foo: true }, Opt::SecondVariant { foo: true },
Opt::parse_from(&["test", "SECOND_VARIANT", "--FOO"]) Opt::try_parse_from(&["test", "SECOND_VARIANT", "--FOO"]).unwrap()
); );
} }
@ -280,12 +301,12 @@ fn test_rename_all_is_propagation_can_be_overridden() {
assert_eq!( assert_eq!(
Opt::FirstVariant { foo_option: true }, Opt::FirstVariant { foo_option: true },
Opt::parse_from(&["test", "first-variant", "--foo-option"]) Opt::try_parse_from(&["test", "first-variant", "--foo-option"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt::SecondVariant { foo_option: true }, Opt::SecondVariant { foo_option: true },
Opt::parse_from(&["test", "SECOND_VARIANT", "--foo-option"]) Opt::try_parse_from(&["test", "SECOND_VARIANT", "--foo-option"]).unwrap()
); );
} }
@ -299,7 +320,7 @@ fn test_lower_is_renamed() {
assert_eq!( assert_eq!(
Opt { foo_option: true }, Opt { foo_option: true },
Opt::parse_from(&["test", "--foooption"]) Opt::try_parse_from(&["test", "--foooption"]).unwrap()
); );
} }
@ -313,6 +334,6 @@ fn test_upper_is_renamed() {
assert_eq!( assert_eq!(
Opt { foo_option: true }, Opt { foo_option: true },
Opt::parse_from(&["test", "--FOOOPTION"]) Opt::try_parse_from(&["test", "--FOOOPTION"]).unwrap()
); );
} }

View file

@ -21,7 +21,10 @@ fn required_argument() {
struct Opt { struct Opt {
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "42"])); assert_eq!(
Opt { arg: 42 },
Opt::try_parse_from(&["test", "42"]).unwrap()
);
assert!(Opt::try_parse_from(&["test"]).is_err()); assert!(Opt::try_parse_from(&["test"]).is_err());
assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err());
} }
@ -32,8 +35,11 @@ fn optional_argument() {
struct Opt { struct Opt {
arg: Option<i32>, arg: Option<i32>,
} }
assert_eq!(Opt { arg: Some(42) }, Opt::parse_from(&["test", "42"])); assert_eq!(
assert_eq!(Opt { arg: None }, Opt::parse_from(&["test"])); Opt { arg: Some(42) },
Opt::try_parse_from(&["test", "42"]).unwrap()
);
assert_eq!(Opt { arg: None }, Opt::try_parse_from(&["test"]).unwrap());
assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err());
} }
@ -44,8 +50,11 @@ fn argument_with_default() {
#[clap(default_value = "42")] #[clap(default_value = "42")]
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 24 }, Opt::parse_from(&["test", "24"])); assert_eq!(
assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test"])); Opt { arg: 24 },
Opt::try_parse_from(&["test", "24"]).unwrap()
);
assert_eq!(Opt { arg: 42 }, Opt::try_parse_from(&["test"]).unwrap());
assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err());
} }
@ -55,11 +64,14 @@ fn arguments() {
struct Opt { struct Opt {
arg: Vec<i32>, arg: Vec<i32>,
} }
assert_eq!(Opt { arg: vec![24] }, Opt::parse_from(&["test", "24"])); assert_eq!(
assert_eq!(Opt { arg: vec![] }, Opt::parse_from(&["test"])); Opt { arg: vec![24] },
Opt::try_parse_from(&["test", "24"]).unwrap()
);
assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!( assert_eq!(
Opt { arg: vec![24, 42] }, Opt { arg: vec![24, 42] },
Opt::parse_from(&["test", "24", "42"]) Opt::try_parse_from(&["test", "24", "42"]).unwrap()
); );
} }
@ -98,7 +110,10 @@ fn auto_value_name() {
assert!(help.contains("MY_SPECIAL_ARG")); assert!(help.contains("MY_SPECIAL_ARG"));
// Ensure the implicit `num_vals` is just 1 // Ensure the implicit `num_vals` is just 1
assert_eq!(Opt { my_special_arg: 10 }, Opt::parse_from(&["test", "10"])); assert_eq!(
Opt { my_special_arg: 10 },
Opt::try_parse_from(&["test", "10"]).unwrap()
);
} }
#[test] #[test]
@ -116,5 +131,8 @@ fn explicit_value_name() {
assert!(help.contains("BROWNIE_POINTS")); assert!(help.contains("BROWNIE_POINTS"));
assert!(!help.contains("MY_SPECIAL_ARG")); assert!(!help.contains("MY_SPECIAL_ARG"));
// Ensure the implicit `num_vals` is just 1 // Ensure the implicit `num_vals` is just 1
assert_eq!(Opt { my_special_arg: 10 }, Opt::parse_from(&["test", "10"])); assert_eq!(
Opt { my_special_arg: 10 },
Opt::try_parse_from(&["test", "10"]).unwrap()
);
} }

View file

@ -21,11 +21,14 @@ fn basic() {
#[clap(short = 'a', long = "arg")] #[clap(short = 'a', long = "arg")]
arg: Vec<i32>, arg: Vec<i32>,
} }
assert_eq!(Opt { arg: vec![24] }, Opt::parse_from(&["test", "-a24"])); assert_eq!(
assert_eq!(Opt { arg: vec![] }, Opt::parse_from(&["test"])); Opt { arg: vec![24] },
Opt::try_parse_from(&["test", "-a24"]).unwrap()
);
assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!( assert_eq!(
Opt { arg: vec![24, 42] }, Opt { arg: vec![24, 42] },
Opt::parse_from(&["test", "--arg", "24", "42"]) Opt::try_parse_from(&["test", "--arg", "24", "42"]).unwrap()
); );
} }
@ -37,7 +40,7 @@ fn update_basic() {
single_value: i32, single_value: i32,
} }
let mut opt = Opt::parse_from(&["test", "-a0"]); let mut opt = Opt::try_parse_from(&["test", "-a0"]).unwrap();
opt.update_from(&["test", "-a42"]); opt.update_from(&["test", "-a42"]);
@ -49,5 +52,5 @@ fn unit_struct() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
struct Opt; struct Opt;
assert_eq!(Opt {}, Opt::parse_from(&["test"])); assert_eq!(Opt {}, Opt::try_parse_from(&["test"]).unwrap());
} }

View file

@ -27,13 +27,13 @@ fn boxed_flatten_subcommand() {
arg: Box::new(Ext { arg: 1 }) arg: Box::new(Ext { arg: 1 })
}) })
}, },
Opt::parse_from(&["test", "flame", "1"]) Opt::try_parse_from(&["test", "flame", "1"]).unwrap()
); );
} }
#[test] #[test]
fn update_boxed_flatten_subcommand() { fn update_boxed_flatten_subcommand() {
let mut opt = Opt::parse_from(&["test", "flame", "1"]); let mut opt = Opt::try_parse_from(&["test", "flame", "1"]).unwrap();
opt.update_from(&["test", "flame", "42"]); opt.update_from(&["test", "flame", "42"]);

View file

@ -50,10 +50,11 @@ fn test_path_opt_simple() {
option_path_1: None, option_path_1: None,
option_path_2: Some(PathBuf::from("j.zip")), option_path_2: Some(PathBuf::from("j.zip")),
}, },
PathOpt::parse_from(&[ PathOpt::try_parse_from(&[
"test", "-p", "/usr/bin", "-v", "/a/b/c", "-v", "/d/e/f", "-v", "/g/h/i", "-q", "test", "-p", "/usr/bin", "-v", "/a/b/c", "-v", "/d/e/f", "-v", "/g/h/i", "-q",
"j.zip", "j.zip",
]) ])
.unwrap()
); );
} }
@ -71,13 +72,13 @@ struct HexOpt {
fn test_parse_hex() { fn test_parse_hex() {
assert_eq!( assert_eq!(
HexOpt { number: 5 }, HexOpt { number: 5 },
HexOpt::parse_from(&["test", "-n", "5"]) HexOpt::try_parse_from(&["test", "-n", "5"]).unwrap()
); );
assert_eq!( assert_eq!(
HexOpt { HexOpt {
number: 0x00ab_cdef number: 0x00ab_cdef
}, },
HexOpt::parse_from(&["test", "-n", "abcdef"]) HexOpt::try_parse_from(&["test", "-n", "abcdef"]).unwrap()
); );
let err = HexOpt::try_parse_from(&["test", "-n", "gg"]).unwrap_err(); let err = HexOpt::try_parse_from(&["test", "-n", "gg"]).unwrap_err();
@ -130,7 +131,7 @@ fn test_every_custom_parser() {
c: "C", c: "C",
d: "D" d: "D"
}, },
NoOpOpt::parse_from(&["test", "-a=?", "-b=?", "-c=?", "-d=?"]) NoOpOpt::try_parse_from(&["test", "-a=?", "-b=?", "-c=?", "-d=?"]).unwrap()
); );
} }
@ -180,7 +181,7 @@ fn test_parser_with_default_value() {
integer: 9000, integer: 9000,
path: PathBuf::from("src/lib.rs"), path: PathBuf::from("src/lib.rs"),
}, },
DefaultedOpt::parse_from(&[ DefaultedOpt::try_parse_from(&[
"test", "test",
"-b", "-b",
"E²=p²c²+m²c⁴", "E²=p²c²+m²c⁴",
@ -189,6 +190,7 @@ fn test_parser_with_default_value() {
"-p", "-p",
"src/lib.rs", "src/lib.rs",
]) ])
.unwrap()
); );
} }
@ -227,9 +229,10 @@ fn test_parser_occurrences() {
little_unsigned: 4, little_unsigned: 4,
custom: Foo(5), custom: Foo(5),
}, },
Occurrences::parse_from(&[ Occurrences::try_parse_from(&[
"test", "-s", "--signed", "--signed", "-l", "-rrrr", "-cccc", "--custom", "test", "-s", "--signed", "--signed", "-l", "-rrrr", "-cccc", "--custom",
]) ])
.unwrap()
); );
} }
@ -268,7 +271,7 @@ fn test_custom_bool() {
tribool: None, tribool: None,
bitset: vec![], bitset: vec![],
}, },
Opt::parse_from(&["test", "-dfalse"]) Opt::try_parse_from(&["test", "-dfalse"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -277,7 +280,7 @@ fn test_custom_bool() {
tribool: None, tribool: None,
bitset: vec![], bitset: vec![],
}, },
Opt::parse_from(&["test", "-dtrue"]) Opt::try_parse_from(&["test", "-dtrue"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -286,7 +289,7 @@ fn test_custom_bool() {
tribool: None, tribool: None,
bitset: vec![], bitset: vec![],
}, },
Opt::parse_from(&["test", "-dtrue", "-vfalse"]) Opt::try_parse_from(&["test", "-dtrue", "-vfalse"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -295,7 +298,7 @@ fn test_custom_bool() {
tribool: None, tribool: None,
bitset: vec![], bitset: vec![],
}, },
Opt::parse_from(&["test", "-dtrue", "-vtrue"]) Opt::try_parse_from(&["test", "-dtrue", "-vtrue"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -304,7 +307,7 @@ fn test_custom_bool() {
tribool: Some(false), tribool: Some(false),
bitset: vec![], bitset: vec![],
}, },
Opt::parse_from(&["test", "-dtrue", "-tfalse"]) Opt::try_parse_from(&["test", "-dtrue", "-tfalse"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -313,7 +316,7 @@ fn test_custom_bool() {
tribool: Some(true), tribool: Some(true),
bitset: vec![], bitset: vec![],
}, },
Opt::parse_from(&["test", "-dtrue", "-ttrue"]) Opt::try_parse_from(&["test", "-dtrue", "-ttrue"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -322,7 +325,8 @@ fn test_custom_bool() {
tribool: None, tribool: None,
bitset: vec![false, true, false, false], bitset: vec![false, true, false, false],
}, },
Opt::parse_from(&["test", "-dtrue", "-bfalse", "-btrue", "-bfalse", "-bfalse"]) Opt::try_parse_from(&["test", "-dtrue", "-bfalse", "-btrue", "-bfalse", "-bfalse"])
.unwrap()
); );
} }
@ -336,7 +340,10 @@ fn test_cstring() {
assert!(Opt::try_parse_from(&["test"]).is_err()); assert!(Opt::try_parse_from(&["test"]).is_err());
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "bla"]).c_string.to_bytes(), Opt::try_parse_from(&["test", "bla"])
.unwrap()
.c_string
.to_bytes(),
b"bla" b"bla"
); );
assert!(Opt::try_parse_from(&["test", "bla\0bla"]).is_err()); assert!(Opt::try_parse_from(&["test", "bla\0bla"]).is_err());

View file

@ -11,8 +11,8 @@ fn default_value() {
#[clap(default_value = "3")] #[clap(default_value = "3")]
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 3 }, Opt::parse_from(&["test"])); assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(Opt { arg: 1 }, Opt::parse_from(&["test", "1"])); assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
let help = get_long_help::<Opt>(); let help = get_long_help::<Opt>();
assert!(help.contains("[default: 3]")); assert!(help.contains("[default: 3]"));
@ -25,8 +25,8 @@ fn default_value_t() {
#[clap(default_value_t = 3)] #[clap(default_value_t = 3)]
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 3 }, Opt::parse_from(&["test"])); assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(Opt { arg: 1 }, Opt::parse_from(&["test", "1"])); assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
let help = get_long_help::<Opt>(); let help = get_long_help::<Opt>();
assert!(help.contains("[default: 3]")); assert!(help.contains("[default: 3]"));
@ -39,8 +39,8 @@ fn auto_default_value_t() {
#[clap(default_value_t)] #[clap(default_value_t)]
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 0 }, Opt::parse_from(&["test"])); assert_eq!(Opt { arg: 0 }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(Opt { arg: 1 }, Opt::parse_from(&["test", "1"])); assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
let help = get_long_help::<Opt>(); let help = get_long_help::<Opt>();
assert!(help.contains("[default: 0]")); assert!(help.contains("[default: 0]"));

View file

@ -31,7 +31,7 @@ fn warning_never_struct() {
Opt { Opt {
s: "foo".to_string() s: "foo".to_string()
}, },
Opt::parse_from(&["test", "foo"]) Opt::try_parse_from(&["test", "foo"]).unwrap()
); );
} }
@ -48,6 +48,6 @@ fn warning_never_enum() {
Opt::Foo { Opt::Foo {
s: "foo".to_string() s: "foo".to_string()
}, },
Opt::parse_from(&["test", "foo", "foo"]) Opt::try_parse_from(&["test", "foo", "foo"]).unwrap()
); );
} }

View file

@ -15,7 +15,7 @@ fn explicit_short_long_no_rename() {
Opt { Opt {
foo: vec!["short".into(), "long".into()] foo: vec!["short".into(), "long".into()]
}, },
Opt::parse_from(&["test", "-.", "short", "--.foo", "long"]) Opt::try_parse_from(&["test", "-.", "short", "--.foo", "long"]).unwrap()
); );
} }

View file

@ -22,9 +22,18 @@ fn unique_flag() {
alice: bool, alice: bool,
} }
assert_eq!(Opt { alice: false }, Opt::parse_from(&["test"])); assert_eq!(
assert_eq!(Opt { alice: true }, Opt::parse_from(&["test", "-a"])); Opt { alice: false },
assert_eq!(Opt { alice: true }, Opt::parse_from(&["test", "--alice"])); Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!(
Opt { alice: true },
Opt::try_parse_from(&["test", "-a"]).unwrap()
);
assert_eq!(
Opt { alice: true },
Opt::try_parse_from(&["test", "--alice"]).unwrap()
);
assert!(Opt::try_parse_from(&["test", "-i"]).is_err()); assert!(Opt::try_parse_from(&["test", "-i"]).is_err());
assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err());
assert!(Opt::try_parse_from(&["test", "-a", "-a"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a", "-a"]).is_err());
@ -41,19 +50,25 @@ fn multiple_flag() {
bob: u8, bob: u8,
} }
assert_eq!(Opt { alice: 0, bob: 0 }, Opt::parse_from(&["test"])); assert_eq!(
assert_eq!(Opt { alice: 1, bob: 0 }, Opt::parse_from(&["test", "-a"])); Opt { alice: 0, bob: 0 },
Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!(
Opt { alice: 1, bob: 0 },
Opt::try_parse_from(&["test", "-a"]).unwrap()
);
assert_eq!( assert_eq!(
Opt { alice: 2, bob: 0 }, Opt { alice: 2, bob: 0 },
Opt::parse_from(&["test", "-a", "-a"]) Opt::try_parse_from(&["test", "-a", "-a"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { alice: 2, bob: 2 }, Opt { alice: 2, bob: 2 },
Opt::parse_from(&["test", "-a", "--alice", "-bb"]) Opt::try_parse_from(&["test", "-a", "--alice", "-bb"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { alice: 3, bob: 1 }, Opt { alice: 3, bob: 1 },
Opt::parse_from(&["test", "-aaa", "--bob"]) Opt::try_parse_from(&["test", "-aaa", "--bob"]).unwrap()
); );
assert!(Opt::try_parse_from(&["test", "-i"]).is_err()); assert!(Opt::try_parse_from(&["test", "-i"]).is_err());
assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err());
@ -73,19 +88,19 @@ fn non_bool_flags() {
bob: std::sync::atomic::AtomicBool, bob: std::sync::atomic::AtomicBool,
} }
let falsey = Opt::parse_from(&["test"]); let falsey = Opt::try_parse_from(&["test"]).unwrap();
assert!(!falsey.alice.load(std::sync::atomic::Ordering::Relaxed)); assert!(!falsey.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(!falsey.bob.load(std::sync::atomic::Ordering::Relaxed)); assert!(!falsey.bob.load(std::sync::atomic::Ordering::Relaxed));
let alice = Opt::parse_from(&["test", "-a"]); let alice = Opt::try_parse_from(&["test", "-a"]).unwrap();
assert!(alice.alice.load(std::sync::atomic::Ordering::Relaxed)); assert!(alice.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(!alice.bob.load(std::sync::atomic::Ordering::Relaxed)); assert!(!alice.bob.load(std::sync::atomic::Ordering::Relaxed));
let bob = Opt::parse_from(&["test", "-b"]); let bob = Opt::try_parse_from(&["test", "-b"]).unwrap();
assert!(!bob.alice.load(std::sync::atomic::Ordering::Relaxed)); assert!(!bob.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(bob.bob.load(std::sync::atomic::Ordering::Relaxed)); assert!(bob.bob.load(std::sync::atomic::Ordering::Relaxed));
let both = Opt::parse_from(&["test", "-b", "-a"]); let both = Opt::try_parse_from(&["test", "-b", "-a"]).unwrap();
assert!(both.alice.load(std::sync::atomic::Ordering::Relaxed)); assert!(both.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(both.bob.load(std::sync::atomic::Ordering::Relaxed)); assert!(both.bob.load(std::sync::atomic::Ordering::Relaxed));
} }
@ -105,41 +120,41 @@ fn combined_flags() {
alice: false, alice: false,
bob: 0 bob: 0
}, },
Opt::parse_from(&["test"]) Opt::try_parse_from(&["test"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
alice: true, alice: true,
bob: 0 bob: 0
}, },
Opt::parse_from(&["test", "-a"]) Opt::try_parse_from(&["test", "-a"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
alice: true, alice: true,
bob: 0 bob: 0
}, },
Opt::parse_from(&["test", "-a"]) Opt::try_parse_from(&["test", "-a"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
alice: false, alice: false,
bob: 1 bob: 1
}, },
Opt::parse_from(&["test", "-b"]) Opt::try_parse_from(&["test", "-b"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
alice: true, alice: true,
bob: 1 bob: 1
}, },
Opt::parse_from(&["test", "--alice", "--bob"]) Opt::try_parse_from(&["test", "--alice", "--bob"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
alice: true, alice: true,
bob: 4 bob: 4
}, },
Opt::parse_from(&["test", "-bb", "-a", "-bb"]) Opt::try_parse_from(&["test", "-bb", "-a", "-bb"]).unwrap()
); );
} }

View file

@ -33,7 +33,7 @@ fn flatten() {
Opt { Opt {
common: Common { arg: 42 } common: Common { arg: 42 }
}, },
Opt::parse_from(&["test", "42"]) Opt::try_parse_from(&["test", "42"]).unwrap()
); );
assert!(Opt::try_parse_from(&["test"]).is_err()); assert!(Opt::try_parse_from(&["test"]).is_err());
assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err());
@ -56,7 +56,7 @@ fn flatten_twice() {
#[clap(flatten)] #[clap(flatten)]
c2: Common, c2: Common,
} }
Opt::parse_from(&["test", "42", "43"]); Opt::try_parse_from(&["test", "42", "43"]).unwrap();
} }
#[test] #[test]
@ -91,14 +91,14 @@ fn flatten_in_subcommand() {
all: false, all: false,
common: Common { arg: 42 } common: Common { arg: 42 }
}, },
Opt::parse_from(&["test", "fetch", "42"]) Opt::try_parse_from(&["test", "fetch", "42"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt::Add(Add { Opt::Add(Add {
interactive: true, interactive: true,
common: Common { arg: 43 } common: Common { arg: 43 }
}), }),
Opt::parse_from(&["test", "add", "-i", "43"]) Opt::try_parse_from(&["test", "add", "-i", "43"]).unwrap()
); );
} }
@ -119,13 +119,13 @@ fn update_args_with_flatten() {
common: Common { arg: 42 }, common: Common { arg: 42 },
}; };
opt.try_update_from(&["test"]).unwrap(); opt.try_update_from(&["test"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "42"]), opt); assert_eq!(Opt::try_parse_from(&["test", "42"]).unwrap(), opt);
let mut opt = Opt { let mut opt = Opt {
common: Common { arg: 42 }, common: Common { arg: 42 },
}; };
opt.try_update_from(&["test", "52"]).unwrap(); opt.try_update_from(&["test", "52"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "52"]), opt); assert_eq!(Opt::try_parse_from(&["test", "52"]).unwrap(), opt);
} }
#[derive(Subcommand, PartialEq, Debug)] #[derive(Subcommand, PartialEq, Debug)]
@ -155,11 +155,11 @@ enum Opt {
fn merge_subcommands_with_flatten() { fn merge_subcommands_with_flatten() {
assert_eq!( assert_eq!(
Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 42, arg2: 44 })), Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 42, arg2: 44 })),
Opt::parse_from(&["test", "command1", "42", "44"]) Opt::try_parse_from(&["test", "command1", "42", "44"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt::Command2(Command2 { arg2: 43 }), Opt::Command2(Command2 { arg2: 43 }),
Opt::parse_from(&["test", "command2", "43"]) Opt::try_parse_from(&["test", "command2", "43"]).unwrap()
); );
} }
@ -168,15 +168,24 @@ fn update_subcommands_with_flatten() {
let mut opt = Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 12, arg2: 14 })); let mut opt = Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 12, arg2: 14 }));
opt.try_update_from(&["test", "command1", "42", "44"]) opt.try_update_from(&["test", "command1", "42", "44"])
.unwrap(); .unwrap();
assert_eq!(Opt::parse_from(&["test", "command1", "42", "44"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "command1", "42", "44"]).unwrap(),
opt
);
let mut opt = Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 12, arg2: 14 })); let mut opt = Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 12, arg2: 14 }));
opt.try_update_from(&["test", "command1", "42"]).unwrap(); opt.try_update_from(&["test", "command1", "42"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "command1", "42", "14"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "command1", "42", "14"]).unwrap(),
opt
);
let mut opt = Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 12, arg2: 14 })); let mut opt = Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 12, arg2: 14 }));
opt.try_update_from(&["test", "command2", "43"]).unwrap(); opt.try_update_from(&["test", "command2", "43"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "command2", "43"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "command2", "43"]).unwrap(),
opt
);
} }
#[test] #[test]
@ -198,7 +207,7 @@ fn flatten_with_doc_comment() {
Opt { Opt {
common: Common { arg: 42 } common: Common { arg: 42 }
}, },
Opt::parse_from(&["test", "42"]) Opt::try_parse_from(&["test", "42"]).unwrap()
); );
let help = utils::get_help::<Opt>(); let help = utils::get_help::<Opt>();

View file

@ -51,7 +51,7 @@ fn test_no_cmd() {
verbose: 0, verbose: 0,
cmd: None cmd: None
}, },
Opt2::parse_from(&["test"]) Opt2::try_parse_from(&["test"]).unwrap()
); );
} }
@ -63,7 +63,7 @@ fn test_fetch() {
verbose: 3, verbose: 3,
cmd: Sub::Fetch {} cmd: Sub::Fetch {}
}, },
Opt::parse_from(&["test", "-vvv", "fetch"]) Opt::try_parse_from(&["test", "-vvv", "fetch"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -71,7 +71,7 @@ fn test_fetch() {
verbose: 0, verbose: 0,
cmd: Sub::Fetch {} cmd: Sub::Fetch {}
}, },
Opt::parse_from(&["test", "--force", "fetch"]) Opt::try_parse_from(&["test", "--force", "fetch"]).unwrap()
); );
} }
@ -83,7 +83,7 @@ fn test_add() {
verbose: 0, verbose: 0,
cmd: Sub::Add {} cmd: Sub::Add {}
}, },
Opt::parse_from(&["test", "add"]) Opt::try_parse_from(&["test", "add"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -91,7 +91,7 @@ fn test_add() {
verbose: 2, verbose: 2,
cmd: Sub::Add {} cmd: Sub::Add {}
}, },
Opt::parse_from(&["test", "-vv", "add"]) Opt::try_parse_from(&["test", "-vv", "add"]).unwrap()
); );
} }
@ -141,7 +141,7 @@ fn test_subsubcommand() {
cmd: Sub3::Quux {} cmd: Sub3::Quux {}
} }
}, },
Opt3::parse_from(&["test", "--all", "foo", "lib.rs", "quux"]) Opt3::try_parse_from(&["test", "--all", "foo", "lib.rs", "quux"]).unwrap()
); );
} }

View file

@ -49,7 +49,7 @@ fn test_slice() {
files: Vec::new(), files: Vec::new(),
values: vec![], values: vec![],
}, },
Opt::parse_from(&["test", "-l", "1"]) Opt::try_parse_from(&["test", "-l", "1"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -58,7 +58,7 @@ fn test_slice() {
files: Vec::new(), files: Vec::new(),
values: vec![], values: vec![],
}, },
Opt::parse_from(&["test", "--level", "1"]) Opt::try_parse_from(&["test", "--level", "1"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -67,7 +67,7 @@ fn test_slice() {
files: Vec::new(), files: Vec::new(),
values: vec![], values: vec![],
}, },
Opt::parse_from(&["test", "--set-level", "1"]) Opt::try_parse_from(&["test", "--set-level", "1"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -76,7 +76,7 @@ fn test_slice() {
files: Vec::new(), files: Vec::new(),
values: vec![], values: vec![],
}, },
Opt::parse_from(&["test", "--lvl", "1"]) Opt::try_parse_from(&["test", "--lvl", "1"]).unwrap()
); );
} }
@ -89,7 +89,7 @@ fn test_multi_args() {
files: vec!["file".to_string()], files: vec!["file".to_string()],
values: vec![], values: vec![],
}, },
Opt::parse_from(&["test", "-l", "1", "file"]) Opt::try_parse_from(&["test", "-l", "1", "file"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
@ -98,7 +98,7 @@ fn test_multi_args() {
files: vec!["FILE".to_string()], files: vec!["FILE".to_string()],
values: vec![1], values: vec![1],
}, },
Opt::parse_from(&["test", "-l", "1", "--values", "1", "--", "FILE"]) Opt::try_parse_from(&["test", "-l", "1", "--values", "1", "--", "FILE"]).unwrap()
); );
} }
@ -117,7 +117,7 @@ fn test_bool() {
files: vec![], files: vec![],
values: vec![], values: vec![],
}, },
Opt::parse_from(&["test", "-l", "1", "--x=1"]) Opt::try_parse_from(&["test", "-l", "1", "--x=1"]).unwrap()
); );
let result = Opt::try_parse_from(&["test", "-l", "1", "--x", "1"]); let result = Opt::try_parse_from(&["test", "-l", "1", "--x", "1"]);
assert!(result.is_err()); assert!(result.is_err());
@ -138,13 +138,13 @@ struct HexOpt {
fn test_parse_hex_function_path() { fn test_parse_hex_function_path() {
assert_eq!( assert_eq!(
HexOpt { number: 5 }, HexOpt { number: 5 },
HexOpt::parse_from(&["test", "-n", "5"]) HexOpt::try_parse_from(&["test", "-n", "5"]).unwrap()
); );
assert_eq!( assert_eq!(
HexOpt { HexOpt {
number: 0x00ab_cdef number: 0x00ab_cdef
}, },
HexOpt::parse_from(&["test", "-n", "abcdef"]) HexOpt::try_parse_from(&["test", "-n", "abcdef"]).unwrap()
); );
let err = HexOpt::try_parse_from(&["test", "-n", "gg"]).unwrap_err(); let err = HexOpt::try_parse_from(&["test", "-n", "gg"]).unwrap_err();

View file

@ -26,9 +26,18 @@ fn required_option() {
#[clap(short, long)] #[clap(short, long)]
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "-a42"])); assert_eq!(
assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "-a", "42"])); Opt { arg: 42 },
assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "--arg", "42"])); Opt::try_parse_from(&["test", "-a42"]).unwrap()
);
assert_eq!(
Opt { arg: 42 },
Opt::try_parse_from(&["test", "-a", "42"]).unwrap()
);
assert_eq!(
Opt { arg: 42 },
Opt::try_parse_from(&["test", "--arg", "42"]).unwrap()
);
assert!(Opt::try_parse_from(&["test"]).is_err()); assert!(Opt::try_parse_from(&["test"]).is_err());
assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err());
} }
@ -40,8 +49,11 @@ fn optional_option() {
#[clap(short)] #[clap(short)]
arg: Option<i32>, arg: Option<i32>,
} }
assert_eq!(Opt { arg: Some(42) }, Opt::parse_from(&["test", "-a42"])); assert_eq!(
assert_eq!(Opt { arg: None }, Opt::parse_from(&["test"])); Opt { arg: Some(42) },
Opt::try_parse_from(&["test", "-a42"]).unwrap()
);
assert_eq!(Opt { arg: None }, Opt::try_parse_from(&["test"]).unwrap());
assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err());
} }
@ -52,8 +64,11 @@ fn option_with_default() {
#[clap(short, default_value = "42")] #[clap(short, default_value = "42")]
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 24 }, Opt::parse_from(&["test", "-a24"])); assert_eq!(
assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test"])); Opt { arg: 24 },
Opt::try_parse_from(&["test", "-a24"]).unwrap()
);
assert_eq!(Opt { arg: 42 }, Opt::try_parse_from(&["test"]).unwrap());
assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err());
} }
@ -64,8 +79,11 @@ fn option_with_raw_default() {
#[clap(short, default_value = "42")] #[clap(short, default_value = "42")]
arg: i32, arg: i32,
} }
assert_eq!(Opt { arg: 24 }, Opt::parse_from(&["test", "-a24"])); assert_eq!(
assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test"])); Opt { arg: 24 },
Opt::try_parse_from(&["test", "-a24"]).unwrap()
);
assert_eq!(Opt { arg: 42 }, Opt::try_parse_from(&["test"]).unwrap());
assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err());
} }
@ -76,11 +94,14 @@ fn options() {
#[clap(short, long, multiple_occurrences(true))] #[clap(short, long, multiple_occurrences(true))]
arg: Vec<i32>, arg: Vec<i32>,
} }
assert_eq!(Opt { arg: vec![24] }, Opt::parse_from(&["test", "-a24"])); assert_eq!(
assert_eq!(Opt { arg: vec![] }, Opt::parse_from(&["test"])); Opt { arg: vec![24] },
Opt::try_parse_from(&["test", "-a24"]).unwrap()
);
assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!( assert_eq!(
Opt { arg: vec![24, 42] }, Opt { arg: vec![24, 42] },
Opt::parse_from(&["test", "-a24", "--arg", "42"]) Opt::try_parse_from(&["test", "-a24", "--arg", "42"]).unwrap()
); );
} }
@ -91,10 +112,13 @@ fn default_value() {
#[clap(short, default_value = "test")] #[clap(short, default_value = "test")]
arg: String, arg: String,
} }
assert_eq!(Opt { arg: "test".into() }, Opt::parse_from(&["test"])); assert_eq!(
Opt { arg: "test".into() },
Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!( assert_eq!(
Opt { arg: "foo".into() }, Opt { arg: "foo".into() },
Opt::parse_from(&["test", "-afoo"]) Opt::try_parse_from(&["test", "-afoo"]).unwrap()
); );
} }
@ -115,8 +139,11 @@ fn option_from_str() {
a: Option<A>, a: Option<A>,
} }
assert_eq!(Opt { a: None }, Opt::parse_from(&["test"])); assert_eq!(Opt { a: None }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(Opt { a: Some(A) }, Opt::parse_from(&["test", "foo"])); assert_eq!(
Opt { a: Some(A) },
Opt::try_parse_from(&["test", "foo"]).unwrap()
);
} }
#[test] #[test]
@ -131,10 +158,13 @@ fn optional_argument_for_optional_option() {
Opt { Opt {
arg: Some(Some(42)) arg: Some(Some(42))
}, },
Opt::parse_from(&["test", "-a42"]) Opt::try_parse_from(&["test", "-a42"]).unwrap()
); );
assert_eq!(Opt { arg: Some(None) }, Opt::parse_from(&["test", "-a"])); assert_eq!(
assert_eq!(Opt { arg: None }, Opt::parse_from(&["test"])); Opt { arg: Some(None) },
Opt::try_parse_from(&["test", "-a"]).unwrap()
);
assert_eq!(Opt { arg: None }, Opt::try_parse_from(&["test"]).unwrap());
assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err());
} }
@ -165,42 +195,42 @@ fn two_option_options() {
arg: Some(Some(42)), arg: Some(Some(42)),
field: Some(Some("f".into())) field: Some(Some("f".into()))
}, },
Opt::parse_from(&["test", "-a42", "--field", "f"]) Opt::try_parse_from(&["test", "-a42", "--field", "f"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(Some(42)), arg: Some(Some(42)),
field: Some(None) field: Some(None)
}, },
Opt::parse_from(&["test", "-a42", "--field"]) Opt::try_parse_from(&["test", "-a42", "--field"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(None), arg: Some(None),
field: Some(None) field: Some(None)
}, },
Opt::parse_from(&["test", "-a", "--field"]) Opt::try_parse_from(&["test", "-a", "--field"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(None), arg: Some(None),
field: Some(Some("f".into())) field: Some(Some("f".into()))
}, },
Opt::parse_from(&["test", "-a", "--field", "f"]) Opt::try_parse_from(&["test", "-a", "--field", "f"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: None, arg: None,
field: Some(None) field: Some(None)
}, },
Opt::parse_from(&["test", "--field"]) Opt::try_parse_from(&["test", "--field"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: None, arg: None,
field: None field: None
}, },
Opt::parse_from(&["test"]) Opt::try_parse_from(&["test"]).unwrap()
); );
} }
@ -213,52 +243,55 @@ fn optional_vec() {
} }
assert_eq!( assert_eq!(
Opt { arg: Some(vec![1]) }, Opt { arg: Some(vec![1]) },
Opt::parse_from(&["test", "-a", "1"]) Opt::try_parse_from(&["test", "-a", "1"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![1, 2]) arg: Some(vec![1, 2])
}, },
Opt::parse_from(&["test", "-a1", "-a2"]) Opt::try_parse_from(&["test", "-a1", "-a2"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![1, 2]) arg: Some(vec![1, 2])
}, },
Opt::parse_from(&["test", "-a1", "-a2", "-a"]) Opt::try_parse_from(&["test", "-a1", "-a2", "-a"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![1, 2]) arg: Some(vec![1, 2])
}, },
Opt::parse_from(&["test", "-a1", "-a", "-a2"]) Opt::try_parse_from(&["test", "-a1", "-a", "-a2"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![1, 2]) arg: Some(vec![1, 2])
}, },
Opt::parse_from(&["test", "-a", "1", "2"]) Opt::try_parse_from(&["test", "-a", "1", "2"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![1, 2, 3]) arg: Some(vec![1, 2, 3])
}, },
Opt::parse_from(&["test", "-a", "1", "2", "-a", "3"]) Opt::try_parse_from(&["test", "-a", "1", "2", "-a", "3"]).unwrap()
); );
assert_eq!(Opt { arg: Some(vec![]) }, Opt::parse_from(&["test", "-a"]));
assert_eq!( assert_eq!(
Opt { arg: Some(vec![]) }, Opt { arg: Some(vec![]) },
Opt::parse_from(&["test", "-a", "-a"]) Opt::try_parse_from(&["test", "-a"]).unwrap()
); );
assert_eq!(Opt { arg: None }, Opt::parse_from(&["test"])); assert_eq!(
Opt { arg: Some(vec![]) },
Opt::try_parse_from(&["test", "-a", "-a"]).unwrap()
);
assert_eq!(Opt { arg: None }, Opt::try_parse_from(&["test"]).unwrap());
} }
#[test] #[test]
@ -277,7 +310,7 @@ fn two_optional_vecs() {
arg: Some(vec![1]), arg: Some(vec![1]),
b: Some(vec![]) b: Some(vec![])
}, },
Opt::parse_from(&["test", "-a", "1", "-b"]) Opt::try_parse_from(&["test", "-a", "1", "-b"]).unwrap()
); );
assert_eq!( assert_eq!(
@ -285,7 +318,7 @@ fn two_optional_vecs() {
arg: Some(vec![1]), arg: Some(vec![1]),
b: Some(vec![]) b: Some(vec![])
}, },
Opt::parse_from(&["test", "-a", "-b", "-a1"]) Opt::try_parse_from(&["test", "-a", "-b", "-a1"]).unwrap()
); );
assert_eq!( assert_eq!(
@ -293,10 +326,13 @@ fn two_optional_vecs() {
arg: Some(vec![1, 2]), arg: Some(vec![1, 2]),
b: Some(vec![1, 2]) b: Some(vec![1, 2])
}, },
Opt::parse_from(&["test", "-a1", "-a2", "-b1", "-b2"]) Opt::try_parse_from(&["test", "-a1", "-a2", "-b1", "-b2"]).unwrap()
); );
assert_eq!(Opt { arg: None, b: None }, Opt::parse_from(&["test"])); assert_eq!(
Opt { arg: None, b: None },
Opt::try_parse_from(&["test"]).unwrap()
);
} }
#[test] #[test]
@ -324,7 +360,7 @@ fn required_option_type() {
req_str: Some(("arg").into()), req_str: Some(("arg").into()),
cmd: None, cmd: None,
}, },
Opt::parse_from(&["test", "arg"]) Opt::try_parse_from(&["test", "arg"]).unwrap()
); );
assert_eq!( assert_eq!(
@ -332,7 +368,7 @@ fn required_option_type() {
req_str: None, req_str: None,
cmd: Some(SubCommands::ExSub { verbose: 1 }), cmd: Some(SubCommands::ExSub { verbose: 1 }),
}, },
Opt::parse_from(&["test", "ex-sub", "-v"]) Opt::try_parse_from(&["test", "ex-sub", "-v"]).unwrap()
); );
assert!(Opt::try_parse_from(&["test"]).is_err()); assert!(Opt::try_parse_from(&["test"]).is_err());

View file

@ -24,6 +24,6 @@ fn raw_bool_literal() {
a: "one".into(), a: "one".into(),
b: "--help".into() b: "--help".into()
}, },
Opt::parse_from(&["test", "one", "--", "--help"]) Opt::try_parse_from(&["test", "one", "--", "--help"]).unwrap()
); );
} }

View file

@ -12,6 +12,6 @@ fn raw_idents() {
Opt { Opt {
r#type: vec!["long".into(), "short".into()] r#type: vec!["long".into(), "short".into()]
}, },
Opt::parse_from(&["test", "--type", "long", "-t", "short"]) Opt::try_parse_from(&["test", "--type", "long", "-t", "short"]).unwrap()
); );
} }

View file

@ -20,7 +20,7 @@ fn skip_1() {
assert!(Opt::try_parse_from(&["test", "-x", "10", "20"]).is_err()); assert!(Opt::try_parse_from(&["test", "-x", "10", "20"]).is_err());
let mut opt = Opt::parse_from(&["test", "-x", "10"]); let mut opt = Opt::try_parse_from(&["test", "-x", "10"]).unwrap();
assert_eq!( assert_eq!(
opt, opt,
Opt { Opt {
@ -52,7 +52,7 @@ fn skip_2() {
} }
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "-x", "10", "20", "30"]), Opt::try_parse_from(&["test", "-x", "10", "20", "30"]).unwrap(),
Opt { Opt {
x: 10, x: 10,
ss: String::from(""), ss: String::from(""),
@ -90,7 +90,7 @@ fn skip_enum() {
} }
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "-n", "10"]), Opt::try_parse_from(&["test", "-n", "10"]).unwrap(),
Opt { Opt {
number: 10, number: 10,
k: Kind::B, k: Kind::B,
@ -120,7 +120,7 @@ fn skip_help_doc_comments() {
} }
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "-n", "10"]), Opt::try_parse_from(&["test", "-n", "10"]).unwrap(),
Opt { Opt {
n: 10, n: 10,
a: 0, a: 0,
@ -145,7 +145,7 @@ fn skip_val() {
} }
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "-n", "10"]), Opt::try_parse_from(&["test", "-n", "10"]).unwrap(),
Opt { Opt {
number: 10, number: 10,
k: "key".to_string(), k: "key".to_string(),

View file

@ -28,7 +28,7 @@ fn special_types_bool() {
Opt { Opt {
arg: inner::bool("success".into()) arg: inner::bool("success".into())
}, },
Opt::parse_from(&["test", "success"]) Opt::try_parse_from(&["test", "success"]).unwrap()
); );
} }
@ -48,7 +48,7 @@ fn special_types_option() {
Opt { Opt {
arg: Some("success".into()) arg: Some("success".into())
}, },
Opt::parse_from(&["test", "success"]) Opt::try_parse_from(&["test", "success"]).unwrap()
); );
} }
@ -68,6 +68,6 @@ fn special_types_vec() {
Opt { Opt {
arg: vec!["success".into()] arg: vec!["success".into()]
}, },
Opt::parse_from(&["test", "success"]) Opt::try_parse_from(&["test", "success"]).unwrap()
); );
} }

View file

@ -45,7 +45,7 @@ fn test_fetch() {
force: false, force: false,
repo: "origin".to_string() repo: "origin".to_string()
}, },
Opt::parse_from(&["test", "fetch", "--all", "origin"]) Opt::try_parse_from(&["test", "fetch", "--all", "origin"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt::Fetch { Opt::Fetch {
@ -53,7 +53,7 @@ fn test_fetch() {
force: true, force: true,
repo: "origin".to_string() repo: "origin".to_string()
}, },
Opt::parse_from(&["test", "fetch", "-f", "origin"]) Opt::try_parse_from(&["test", "fetch", "-f", "origin"]).unwrap()
); );
} }
@ -64,14 +64,14 @@ fn test_add() {
interactive: false, interactive: false,
verbose: false verbose: false
}, },
Opt::parse_from(&["test", "add"]) Opt::try_parse_from(&["test", "add"]).unwrap()
); );
assert_eq!( assert_eq!(
Opt::Add { Opt::Add {
interactive: true, interactive: true,
verbose: true verbose: true
}, },
Opt::parse_from(&["test", "add", "-i", "-v"]) Opt::try_parse_from(&["test", "add", "-i", "-v"]).unwrap()
); );
} }
@ -100,7 +100,7 @@ fn test_hyphenated_subcommands() {
Opt2::DoSomething { Opt2::DoSomething {
arg: "blah".to_string() arg: "blah".to_string()
}, },
Opt2::parse_from(&["test", "do-something", "blah"]) Opt2::try_parse_from(&["test", "do-something", "blah"]).unwrap()
); );
} }
@ -113,9 +113,12 @@ enum Opt3 {
#[test] #[test]
fn test_null_commands() { fn test_null_commands() {
assert_eq!(Opt3::Add, Opt3::parse_from(&["test", "add"])); assert_eq!(Opt3::Add, Opt3::try_parse_from(&["test", "add"]).unwrap());
assert_eq!(Opt3::Init, Opt3::parse_from(&["test", "init"])); assert_eq!(Opt3::Init, Opt3::try_parse_from(&["test", "init"]).unwrap());
assert_eq!(Opt3::Fetch, Opt3::parse_from(&["test", "fetch"])); assert_eq!(
Opt3::Fetch,
Opt3::try_parse_from(&["test", "fetch"]).unwrap()
);
} }
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
@ -144,14 +147,14 @@ fn test_tuple_commands() {
Opt4::Add(Add { Opt4::Add(Add {
file: "f".to_string() file: "f".to_string()
}), }),
Opt4::parse_from(&["test", "add", "f"]) Opt4::try_parse_from(&["test", "add", "f"]).unwrap()
); );
assert_eq!(Opt4::Init, Opt4::parse_from(&["test", "init"])); assert_eq!(Opt4::Init, Opt4::try_parse_from(&["test", "init"]).unwrap());
assert_eq!( assert_eq!(
Opt4::Fetch(Fetch { Opt4::Fetch(Fetch {
remote: "origin".to_string() remote: "origin".to_string()
}), }),
Opt4::parse_from(&["test", "fetch", "origin"]) Opt4::try_parse_from(&["test", "fetch", "origin"]).unwrap()
); );
let output = get_long_help::<Opt4>(); let output = get_long_help::<Opt4>();
@ -184,7 +187,7 @@ fn global_passed_down() {
} }
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "global"]), Opt::try_parse_from(&["test", "global"]).unwrap(),
Opt { Opt {
other: false, other: false,
sub: Subcommands::Global(GlobalCmd { other: false }) sub: Subcommands::Global(GlobalCmd { other: false })
@ -192,7 +195,7 @@ fn global_passed_down() {
); );
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "global", "--other"]), Opt::try_parse_from(&["test", "global", "--other"]).unwrap(),
Opt { Opt {
other: true, other: true,
sub: Subcommands::Global(GlobalCmd { other: true }) sub: Subcommands::Global(GlobalCmd { other: true })
@ -217,14 +220,14 @@ fn external_subcommand() {
} }
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "add"]), Opt::try_parse_from(&["test", "add"]).unwrap(),
Opt { Opt {
sub: Subcommands::Add sub: Subcommands::Add
} }
); );
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "remove"]), Opt::try_parse_from(&["test", "remove"]).unwrap(),
Opt { Opt {
sub: Subcommands::Remove sub: Subcommands::Remove
} }
@ -310,7 +313,7 @@ fn enum_in_enum_subsubcommand() {
let result = Opt::try_parse_from(&["test", "daemon"]); let result = Opt::try_parse_from(&["test", "daemon"]);
assert!(result.is_err()); assert!(result.is_err());
let result = Opt::parse_from(&["test", "daemon", "start"]); let result = Opt::try_parse_from(&["test", "daemon", "start"]).unwrap();
assert_eq!(Opt::Daemon(DaemonCommand::Start), result); assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
} }
@ -337,17 +340,26 @@ fn update_subcommands() {
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 }); let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
opt.try_update_from(&["test", "command1", "42", "44"]) opt.try_update_from(&["test", "command1", "42", "44"])
.unwrap(); .unwrap();
assert_eq!(Opt::parse_from(&["test", "command1", "42", "44"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "command1", "42", "44"]).unwrap(),
opt
);
// Partial subcommand update // Partial subcommand update
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 }); let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
opt.try_update_from(&["test", "command1", "42"]).unwrap(); opt.try_update_from(&["test", "command1", "42"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "command1", "42", "14"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "command1", "42", "14"]).unwrap(),
opt
);
// Change subcommand // Change subcommand
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 }); let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
opt.try_update_from(&["test", "command2", "43"]).unwrap(); opt.try_update_from(&["test", "command2", "43"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "command2", "43"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "command2", "43"]).unwrap(),
opt
);
} }
#[test] #[test]
@ -388,7 +400,7 @@ fn update_sub_subcommands() {
opt.try_update_from(&["test", "child1", "command1", "42", "44"]) opt.try_update_from(&["test", "child1", "command1", "42", "44"])
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "child1", "command1", "42", "44"]), Opt::try_parse_from(&["test", "child1", "command1", "42", "44"]).unwrap(),
opt opt
); );
@ -397,7 +409,7 @@ fn update_sub_subcommands() {
opt.try_update_from(&["test", "child1", "command1", "42"]) opt.try_update_from(&["test", "child1", "command1", "42"])
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "child1", "command1", "42", "14"]), Opt::try_parse_from(&["test", "child1", "command1", "42", "14"]).unwrap(),
opt opt
); );
@ -405,13 +417,19 @@ fn update_sub_subcommands() {
let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 })); let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 }));
opt.try_update_from(&["test", "child1", "command2", "43"]) opt.try_update_from(&["test", "child1", "command2", "43"])
.unwrap(); .unwrap();
assert_eq!(Opt::parse_from(&["test", "child1", "command2", "43"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "child1", "command2", "43"]).unwrap(),
opt
);
// Change subcommand // Change subcommand
let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 })); let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 }));
opt.try_update_from(&["test", "child2", "command2", "43"]) opt.try_update_from(&["test", "child2", "command2", "43"])
.unwrap(); .unwrap();
assert_eq!(Opt::parse_from(&["test", "child2", "command2", "43"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "child2", "command2", "43"]).unwrap(),
opt
);
} }
#[test] #[test]
@ -438,21 +456,30 @@ fn update_ext_subcommand() {
// Full subcommand update // Full subcommand update
let mut opt = Opt::Ext(vec!["12".into(), "14".into()]); let mut opt = Opt::Ext(vec!["12".into(), "14".into()]);
opt.try_update_from(&["test", "ext", "42", "44"]).unwrap(); opt.try_update_from(&["test", "ext", "42", "44"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "ext", "42", "44"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "ext", "42", "44"]).unwrap(),
opt
);
// No partial subcommand update // No partial subcommand update
let mut opt = Opt::Ext(vec!["12".into(), "14".into()]); let mut opt = Opt::Ext(vec!["12".into(), "14".into()]);
opt.try_update_from(&["test", "ext", "42"]).unwrap(); opt.try_update_from(&["test", "ext", "42"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "ext", "42"]), opt); assert_eq!(Opt::try_parse_from(&["test", "ext", "42"]).unwrap(), opt);
// Change subcommand // Change subcommand
let mut opt = Opt::Ext(vec!["12".into(), "14".into()]); let mut opt = Opt::Ext(vec!["12".into(), "14".into()]);
opt.try_update_from(&["test", "command2", "43"]).unwrap(); opt.try_update_from(&["test", "command2", "43"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "command2", "43"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "command2", "43"]).unwrap(),
opt
);
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 }); let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
opt.try_update_from(&["test", "ext", "42", "44"]).unwrap(); opt.try_update_from(&["test", "ext", "42", "44"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "ext", "42", "44"]), opt); assert_eq!(
Opt::try_parse_from(&["test", "ext", "42", "44"]).unwrap(),
opt
);
} }
#[test] #[test]
fn subcommand_name_not_literal() { fn subcommand_name_not_literal() {
@ -494,14 +521,14 @@ fn skip_subcommand() {
} }
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "add"]), Opt::try_parse_from(&["test", "add"]).unwrap(),
Opt { Opt {
sub: Subcommands::Add sub: Subcommands::Add
} }
); );
assert_eq!( assert_eq!(
Opt::parse_from(&["test", "remove"]), Opt::try_parse_from(&["test", "remove"]).unwrap(),
Opt { Opt {
sub: Subcommands::Remove sub: Subcommands::Remove
} }