clap/tests/derive/value_enum.rs

629 lines
14 KiB
Rust
Raw Normal View History

2020-04-22 07:25:41 +00:00
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
2022-01-04 20:10:35 +00:00
// Ana Hobden (@hoverbear) <operator@hoverbear.org>
2020-04-22 07:25:41 +00:00
//
// 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.
use clap::Parser;
2020-04-22 07:25:41 +00:00
#[test]
fn basic() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
2020-04-22 07:25:41 +00:00
enum ArgChoice {
Foo,
Bar,
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 07:25:41 +00:00
struct Opt {
#[arg(value_enum)]
2020-04-22 07:25:41 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 07:25:41 +00:00
assert_eq!(
Opt {
arg: ArgChoice::Foo
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo"]).unwrap()
2020-04-22 07:25:41 +00:00
);
assert_eq!(
Opt {
arg: ArgChoice::Bar
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "bar"]).unwrap()
2020-04-22 07:25:41 +00:00
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "fOo"]).is_err());
2020-04-22 07:25:41 +00:00
}
#[test]
fn default_value() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
}
impl Default for ArgChoice {
fn default() -> Self {
Self::Bar
}
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_enum, default_value_t)]
arg: ArgChoice,
}
assert_eq!(
Opt {
arg: ArgChoice::Foo
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo"]).unwrap()
);
assert_eq!(
Opt {
arg: ArgChoice::Bar
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "bar"]).unwrap()
);
assert_eq!(
Opt {
arg: ArgChoice::Bar
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from([""]).unwrap()
);
}
#[test]
fn vec_for_default_values_t() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_enum, default_values_t = vec![ArgChoice::Foo, ArgChoice::Bar])]
arg1: Vec<ArgChoice>,
#[arg(
long,
value_enum,
default_values_t = clap::ValueEnum::value_variants()
)]
arg2: Vec<ArgChoice>,
}
assert_eq!(
Opt {
arg1: vec![ArgChoice::Foo],
arg2: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo"]).unwrap()
);
assert_eq!(
Opt {
arg1: vec![ArgChoice::Bar],
arg2: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "bar"]).unwrap()
);
assert_eq!(
Opt {
arg1: vec![ArgChoice::Foo, ArgChoice::Bar],
arg2: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from([""]).unwrap()
);
assert_eq!(
Opt {
arg1: vec![ArgChoice::Foo, ArgChoice::Bar],
arg2: vec![ArgChoice::Foo]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "--arg2", "foo"]).unwrap()
);
}
#[test]
fn vec_for_default_values_os_t() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_enum, default_values_os_t = vec![ArgChoice::Foo, ArgChoice::Bar])]
arg: Vec<ArgChoice>,
#[arg(
long,
value_enum,
default_values_os_t = clap::ValueEnum::value_variants()
)]
arg2: Vec<ArgChoice>,
}
assert_eq!(
Opt {
arg: vec![ArgChoice::Foo],
arg2: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo"]).unwrap()
);
assert_eq!(
Opt {
arg: vec![ArgChoice::Bar],
arg2: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "bar"]).unwrap()
);
assert_eq!(
Opt {
arg: vec![ArgChoice::Foo, ArgChoice::Bar],
arg2: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from([""]).unwrap()
);
assert_eq!(
Opt {
arg: vec![ArgChoice::Foo, ArgChoice::Bar],
arg2: vec![ArgChoice::Foo]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "--arg2", "foo"]).unwrap()
);
}
2020-04-22 07:25:41 +00:00
#[test]
fn multi_word_is_renamed_kebab() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
2020-04-22 07:25:41 +00:00
#[allow(non_camel_case_types)]
enum ArgChoice {
FooBar,
BAR_BAZ,
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 07:25:41 +00:00
struct Opt {
#[arg(value_enum)]
2020-04-22 07:25:41 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 07:25:41 +00:00
assert_eq!(
Opt {
arg: ArgChoice::FooBar
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo-bar"]).unwrap()
2020-04-22 07:25:41 +00:00
);
assert_eq!(
Opt {
arg: ArgChoice::BAR_BAZ
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "bar-baz"]).unwrap()
2020-04-22 07:25:41 +00:00
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "FooBar"]).is_err());
2020-04-22 07:25:41 +00:00
}
#[test]
fn variant_with_defined_casing() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
2020-04-22 07:25:41 +00:00
enum ArgChoice {
#[value(rename_all = "screaming_snake")]
2020-04-22 07:25:41 +00:00
FooBar,
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 07:25:41 +00:00
struct Opt {
#[arg(value_enum)]
2020-04-22 07:25:41 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 07:25:41 +00:00
assert_eq!(
Opt {
arg: ArgChoice::FooBar
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "FOO_BAR"]).unwrap()
2020-04-22 07:25:41 +00:00
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "FooBar"]).is_err());
2020-04-22 07:25:41 +00:00
}
#[test]
fn casing_is_propagated_from_parent() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
#[value(rename_all = "screaming_snake")]
2020-04-22 07:25:41 +00:00
enum ArgChoice {
FooBar,
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 07:25:41 +00:00
struct Opt {
#[arg(value_enum)]
2020-04-22 07:25:41 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 07:25:41 +00:00
assert_eq!(
Opt {
arg: ArgChoice::FooBar
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "FOO_BAR"]).unwrap()
2020-04-22 07:25:41 +00:00
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "FooBar"]).is_err());
2020-04-22 07:25:41 +00:00
}
#[test]
fn casing_propagation_is_overridden() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
#[value(rename_all = "screaming_snake")]
2020-04-22 07:25:41 +00:00
enum ArgChoice {
#[value(rename_all = "camel")]
2020-04-22 07:25:41 +00:00
FooBar,
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 07:25:41 +00:00
struct Opt {
#[arg(value_enum)]
2020-04-22 07:25:41 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 07:25:41 +00:00
assert_eq!(
Opt {
arg: ArgChoice::FooBar
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "fooBar"]).unwrap()
2020-04-22 07:25:41 +00:00
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "FooBar"]).is_err());
assert!(Opt::try_parse_from(["", "FOO_BAR"]).is_err());
2020-04-22 07:25:41 +00:00
}
#[test]
fn ignore_case() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
2020-04-22 07:25:41 +00:00
enum ArgChoice {
Foo,
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 07:25:41 +00:00
struct Opt {
#[arg(value_enum, ignore_case(true))]
2020-04-22 07:25:41 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 07:25:41 +00:00
assert_eq!(
Opt {
arg: ArgChoice::Foo
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo"]).unwrap()
2020-04-22 07:25:41 +00:00
);
assert_eq!(
Opt {
arg: ArgChoice::Foo
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "fOo"]).unwrap()
2020-04-22 07:25:41 +00:00
);
}
#[test]
fn ignore_case_set_to_false() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
2020-04-22 07:25:41 +00:00
enum ArgChoice {
Foo,
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 07:25:41 +00:00
struct Opt {
#[arg(value_enum, ignore_case(false))]
2020-04-22 07:25:41 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 07:25:41 +00:00
assert_eq!(
Opt {
arg: ArgChoice::Foo
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo"]).unwrap()
2020-04-22 07:25:41 +00:00
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "fOo"]).is_err());
2020-04-22 07:25:41 +00:00
}
2020-04-22 10:16:48 +00:00
#[test]
fn alias() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
2020-04-22 10:16:48 +00:00
enum ArgChoice {
#[value(alias = "TOTP")]
2021-11-17 20:23:22 +00:00
Totp,
2020-04-22 10:16:48 +00:00
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 10:16:48 +00:00
struct Opt {
#[arg(value_enum, ignore_case(false))]
2020-04-22 10:16:48 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 10:16:48 +00:00
assert_eq!(
Opt {
2021-11-17 20:23:22 +00:00
arg: ArgChoice::Totp
2020-04-22 10:16:48 +00:00
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "totp"]).unwrap()
2020-04-22 10:16:48 +00:00
);
assert_eq!(
Opt {
2021-11-17 20:23:22 +00:00
arg: ArgChoice::Totp
2020-04-22 10:16:48 +00:00
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "TOTP"]).unwrap()
2020-04-22 10:16:48 +00:00
);
}
#[test]
fn multiple_alias() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
2020-04-22 10:16:48 +00:00
enum ArgChoice {
#[value(alias = "TOTP", alias = "t")]
2021-11-17 20:23:22 +00:00
Totp,
2020-04-22 10:16:48 +00:00
}
#[derive(Parser, PartialEq, Debug)]
2020-04-22 10:16:48 +00:00
struct Opt {
#[arg(value_enum, ignore_case(false))]
2020-04-22 10:16:48 +00:00
arg: ArgChoice,
2021-01-18 18:12:28 +00:00
}
2020-04-22 10:16:48 +00:00
assert_eq!(
Opt {
2021-11-17 20:23:22 +00:00
arg: ArgChoice::Totp
2020-04-22 10:16:48 +00:00
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "totp"]).unwrap()
2020-04-22 10:16:48 +00:00
);
assert_eq!(
Opt {
2021-11-17 20:23:22 +00:00
arg: ArgChoice::Totp
2020-04-22 10:16:48 +00:00
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "TOTP"]).unwrap()
2020-04-22 10:16:48 +00:00
);
assert_eq!(
Opt {
2021-11-17 20:23:22 +00:00
arg: ArgChoice::Totp
2020-04-22 10:16:48 +00:00
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "t"]).unwrap()
2020-04-22 10:16:48 +00:00
);
}
#[test]
fn skip_variant() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
#[allow(dead_code)] // silence warning about `Baz` being unused
enum ArgChoice {
Foo,
Bar,
#[value(skip)]
Baz,
}
assert_eq!(
<ArgChoice as clap::ValueEnum>::value_variants()
.iter()
.map(clap::ValueEnum::to_possible_value)
.map(Option::unwrap)
.collect::<Vec<_>>(),
vec![
clap::builder::PossibleValue::new("foo"),
clap::builder::PossibleValue::new("bar")
]
);
{
use clap::ValueEnum;
assert!(ArgChoice::from_str("foo", true).is_ok());
assert!(ArgChoice::from_str("bar", true).is_ok());
assert!(ArgChoice::from_str("baz", true).is_err());
}
}
#[test]
fn skip_non_unit_variant() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
#[allow(dead_code)] // silence warning about `Baz` being unused
enum ArgChoice {
Foo,
Bar,
#[value(skip)]
Baz(usize),
}
assert_eq!(
<ArgChoice as clap::ValueEnum>::value_variants()
.iter()
.map(clap::ValueEnum::to_possible_value)
.map(Option::unwrap)
.collect::<Vec<_>>(),
vec![
clap::builder::PossibleValue::new("foo"),
clap::builder::PossibleValue::new("bar")
]
);
{
use clap::ValueEnum;
assert!(ArgChoice::from_str("foo", true).is_ok());
assert!(ArgChoice::from_str("bar", true).is_ok());
assert!(ArgChoice::from_str("baz", true).is_err());
}
}
#[test]
fn from_str_invalid() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
}
{
use clap::ValueEnum;
assert!(ArgChoice::from_str("bar", true).is_err());
}
}
#[test]
fn option_type() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_enum)]
arg: Option<ArgChoice>,
2021-01-18 18:12:28 +00:00
}
2022-11-24 13:54:25 +00:00
assert_eq!(Opt { arg: None }, Opt::try_parse_from([""]).unwrap());
assert_eq!(
Opt {
arg: Some(ArgChoice::Foo)
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "foo"]).unwrap()
);
assert_eq!(
Opt {
arg: Some(ArgChoice::Bar)
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "bar"]).unwrap()
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "fOo"]).is_err());
}
#[test]
fn option_option_type() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_enum, long)]
arg: Option<Option<ArgChoice>>,
}
2022-11-24 13:54:25 +00:00
assert_eq!(Opt { arg: None }, Opt::try_parse_from([""]).unwrap());
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' ```
2021-10-30 14:55:50 +00:00
assert_eq!(
Opt { arg: Some(None) },
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "--arg"]).unwrap()
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' ```
2021-10-30 14:55:50 +00:00
);
assert_eq!(
Opt {
arg: Some(Some(ArgChoice::Foo))
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "--arg", "foo"]).unwrap()
);
assert_eq!(
Opt {
arg: Some(Some(ArgChoice::Bar))
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "--arg", "bar"]).unwrap()
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "--arg", "fOo"]).is_err());
}
#[test]
fn vec_type() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_enum, short, long)]
arg: Vec<ArgChoice>,
2021-01-18 18:12:28 +00:00
}
2022-11-24 13:54:25 +00:00
assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from([""]).unwrap());
assert_eq!(
Opt {
arg: vec![ArgChoice::Foo]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "-a", "foo"]).unwrap()
);
assert_eq!(
Opt {
arg: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "-a", "foo", "-a", "bar"]).unwrap()
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "-a", "fOo"]).is_err());
}
#[test]
fn option_vec_type() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_enum, short, long)]
arg: Option<Vec<ArgChoice>>,
}
2022-11-24 13:54:25 +00:00
assert_eq!(Opt { arg: None }, Opt::try_parse_from([""]).unwrap());
assert_eq!(
Opt {
arg: Some(vec![ArgChoice::Foo])
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "-a", "foo"]).unwrap()
);
assert_eq!(
Opt {
arg: Some(vec![ArgChoice::Foo, ArgChoice::Bar])
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "-a", "foo", "-a", "bar"]).unwrap()
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["", "-a", "fOo"]).is_err());
}
#[test]
fn vec_type_default_value() {
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
enum ArgChoice {
Foo,
Bar,
Baz,
}
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(
value_enum,
short,
long,
default_value = "foo,bar",
value_delimiter = ','
)]
arg: Vec<ArgChoice>,
}
assert_eq!(
Opt {
arg: vec![ArgChoice::Foo, ArgChoice::Bar]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from([""]).unwrap()
);
assert_eq!(
Opt {
arg: vec![ArgChoice::Foo, ArgChoice::Baz]
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["", "-a", "foo,baz"]).unwrap()
);
}