mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
Derived ArgEnum::from_str returns Err instead of panicking
ArgEnum is public so it is possible for users to call it with a non-variant input string.
This commit is contained in:
parent
e3bfa50e8f
commit
c20acaf2d4
2 changed files with 12 additions and 2 deletions
|
@ -118,7 +118,7 @@ fn gen_from_str(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
|||
|
||||
match input {
|
||||
#(val if func(val, #lit) => Ok(Self::#variant),)*
|
||||
e => unreachable!("The impossible variant have been spotted: {}", e),
|
||||
e => Err(format!("Invalid variant: {}", e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
use clap::Clap;
|
||||
use clap::{ArgEnum, Clap};
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
|
@ -309,3 +309,13 @@ fn vector() {
|
|||
);
|
||||
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_str_invalid() {
|
||||
#[derive(Clap, PartialEq, Debug)]
|
||||
enum ArgChoice {
|
||||
Foo,
|
||||
}
|
||||
|
||||
assert!(ArgChoice::from_str("bar", true).is_err());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue