2020-04-22 07:25:41 +00:00
|
|
|
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
|
|
|
|
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
|
|
|
|
// Andrew Hobden (@hoverbear) <andrew@hoverbear.org>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <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.
|
2021-07-13 17:50:55 +00:00
|
|
|
use clap::{ArgEnum, ArgValue, Parser};
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum)]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Foo
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "foo"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Bar
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "bar"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "fOo"]).is_err());
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:46:24 +00:00
|
|
|
#[test]
|
|
|
|
fn default_value() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2021-07-21 17:46:24 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ArgChoice {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for ArgChoice {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
2021-10-01 14:13:17 +00:00
|
|
|
std::fmt::Display::fmt(self.to_arg_value().unwrap().get_name(), f)
|
2021-07-21 17:46:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-21 17:46:24 +00:00
|
|
|
struct Opt {
|
2021-07-28 13:53:32 +00:00
|
|
|
#[clap(arg_enum, default_value_t)]
|
2021-07-21 17:46:24 +00:00
|
|
|
arg: ArgChoice,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Foo
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "foo"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Bar
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "bar"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Bar
|
|
|
|
},
|
|
|
|
Opt::parse_from(&[""])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-22 07:25:41 +00:00
|
|
|
#[test]
|
|
|
|
fn multi_word_is_renamed_kebab() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
enum ArgChoice {
|
|
|
|
FooBar,
|
|
|
|
BAR_BAZ,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum)]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::FooBar
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "foo-bar"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::BAR_BAZ
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "bar-baz"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn variant_with_defined_casing() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
#[clap(rename_all = "screaming_snake")]
|
|
|
|
FooBar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum)]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::FooBar
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "FOO_BAR"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-19 01:38:22 +00:00
|
|
|
fn casing_is_propagated_from_parent() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
#[clap(rename_all = "screaming_snake")]
|
|
|
|
enum ArgChoice {
|
|
|
|
FooBar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum)]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::FooBar
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "FOO_BAR"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-19 01:38:22 +00:00
|
|
|
fn casing_propagation_is_overridden() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
#[clap(rename_all = "screaming_snake")]
|
|
|
|
enum ArgChoice {
|
|
|
|
#[clap(rename_all = "camel")]
|
|
|
|
FooBar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum)]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::FooBar
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "fooBar"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "FooBar"]).is_err());
|
|
|
|
assert!(Opt::try_parse_from(&["", "FOO_BAR"]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum, case_insensitive(true))]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Foo
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "foo"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Foo
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "fOo"])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_set_to_false() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum, case_insensitive(false))]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Foo
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "foo"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "fOo"]).is_err());
|
|
|
|
}
|
2020-04-22 10:16:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 10:16:48 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
#[clap(alias = "TOTP")]
|
|
|
|
TOTP,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 10:16:48 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum, case_insensitive(false))]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 10:16:48 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::TOTP
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "totp"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::TOTP
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "TOTP"])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_alias() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-22 10:16:48 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
#[clap(alias = "TOTP", alias = "t")]
|
|
|
|
TOTP,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 10:16:48 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum, case_insensitive(false))]
|
|
|
|
arg: ArgChoice,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-22 10:16:48 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::TOTP
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "totp"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::TOTP
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "TOTP"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::TOTP
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "t"])
|
|
|
|
);
|
|
|
|
}
|
2020-04-28 09:11:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-28 09:11:00 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-28 09:11:00 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum)]
|
|
|
|
arg: Option<ArgChoice>,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-28 09:11:00 +00:00
|
|
|
|
|
|
|
assert_eq!(Opt { arg: None }, Opt::parse_from(&[""]));
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: Some(ArgChoice::Foo)
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "foo"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: Some(ArgChoice::Bar)
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "bar"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "fOo"]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vector() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2020-04-28 09:11:00 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-28 09:11:00 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(arg_enum, short, long)]
|
|
|
|
arg: Vec<ArgChoice>,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-28 09:11:00 +00:00
|
|
|
|
|
|
|
assert_eq!(Opt { arg: vec![] }, Opt::parse_from(&[""]));
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: vec![ArgChoice::Foo]
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "-a", "foo"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: vec![ArgChoice::Foo, ArgChoice::Bar]
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["", "-a", "foo", "bar"])
|
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
|
|
|
|
}
|
2021-06-08 17:52:09 +00:00
|
|
|
|
2021-07-14 20:30:16 +00:00
|
|
|
#[test]
|
|
|
|
fn skip_variant() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2021-07-14 20:30:16 +00:00
|
|
|
#[allow(dead_code)] // silence warning about `Baz` being unused
|
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
#[clap(skip)]
|
|
|
|
Baz,
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:50:43 +00:00
|
|
|
assert_eq!(
|
2021-09-29 16:05:19 +00:00
|
|
|
ArgChoice::value_variants()
|
|
|
|
.iter()
|
2021-10-01 14:13:17 +00:00
|
|
|
.map(ArgEnum::to_arg_value)
|
2021-09-29 16:05:19 +00:00
|
|
|
.map(Option::unwrap)
|
|
|
|
.collect::<Vec<_>>(),
|
2021-09-26 23:18:47 +00:00
|
|
|
vec![ArgValue::new("foo"), ArgValue::new("bar")]
|
2021-09-09 12:50:43 +00:00
|
|
|
);
|
2021-07-14 20:30:16 +00:00
|
|
|
assert!(ArgChoice::from_str("foo", true).is_ok());
|
|
|
|
assert!(ArgChoice::from_str("bar", true).is_ok());
|
|
|
|
assert!(ArgChoice::from_str("baz", true).is_err());
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:52:09 +00:00
|
|
|
#[test]
|
|
|
|
fn from_str_invalid() {
|
2021-09-29 16:05:19 +00:00
|
|
|
#[derive(ArgEnum, PartialEq, Debug, Clone)]
|
2021-06-08 17:52:09 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(ArgChoice::from_str("bar", true).is_err());
|
|
|
|
}
|