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.
|
2022-01-10 21:41:58 +00:00
|
|
|
use clap::Parser;
|
2020-04-22 07:25:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, 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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2021-07-21 17:46:24 +00:00
|
|
|
#[test]
|
|
|
|
fn default_value() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2021-07-21 17:46:24 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ArgChoice {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-21 17:46:24 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum, default_value_t)]
|
2021-07-21 17:46:24 +00:00
|
|
|
arg: ArgChoice,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Foo
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["", "foo"]).unwrap()
|
2021-07-21 17:46:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Bar
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["", "bar"]).unwrap()
|
2021-07-21 17:46:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: ArgChoice::Bar
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from([""]).unwrap()
|
2021-07-21 17:46:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:10:31 +00:00
|
|
|
#[test]
|
|
|
|
fn vec_for_default_values_t() {
|
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum, default_values_t = vec![ArgChoice::Foo, ArgChoice::Bar])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg1: Vec<ArgChoice>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(
|
2022-07-24 23:10:31 +00:00
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_for_default_values_os_t() {
|
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum, default_values_os_t = vec![ArgChoice::Foo, ArgChoice::Bar])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg: Vec<ArgChoice>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(
|
2022-07-24 23:10:31 +00:00
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
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()
|
2022-07-24 23:10:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-22 07:25:41 +00:00
|
|
|
#[test]
|
|
|
|
fn multi_word_is_renamed_kebab() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, 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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2020-04-22 07:25:41 +00:00
|
|
|
enum ArgChoice {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(rename_all = "screaming_snake")]
|
2020-04-22 07:25:41 +00:00
|
|
|
FooBar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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]
|
2021-10-19 01:38:22 +00:00
|
|
|
fn casing_is_propagated_from_parent() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(rename_all = "screaming_snake")]
|
2020-04-22 07:25:41 +00:00
|
|
|
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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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]
|
2021-10-19 01:38:22 +00:00
|
|
|
fn casing_propagation_is_overridden() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(rename_all = "screaming_snake")]
|
2020-04-22 07:25:41 +00:00
|
|
|
enum ArgChoice {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(rename_all = "camel")]
|
2020-04-22 07:25:41 +00:00
|
|
|
FooBar,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 07:25:41 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn ignore_case() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, 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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn ignore_case_set_to_false() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, 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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2020-04-22 10:16:48 +00:00
|
|
|
enum ArgChoice {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(alias = "TOTP")]
|
2021-11-17 20:23:22 +00:00
|
|
|
Totp,
|
2020-04-22 10:16:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 10:16:48 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2020-04-22 10:16:48 +00:00
|
|
|
enum ArgChoice {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(alias = "TOTP", alias = "t")]
|
2021-11-17 20:23:22 +00:00
|
|
|
Totp,
|
2020-04-22 10:16:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-04-22 10:16:48 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[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
|
|
|
);
|
|
|
|
}
|
2020-04-28 09:11:00 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-11-04 15:15:04 +00:00
|
|
|
fn skip_variant() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2021-11-04 15:15:04 +00:00
|
|
|
#[allow(dead_code)] // silence warning about `Baz` being unused
|
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(skip)]
|
2021-11-04 15:15:04 +00:00
|
|
|
Baz,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-06-08 15:56:57 +00:00
|
|
|
<ArgChoice as clap::ValueEnum>::value_variants()
|
2021-11-04 15:15:04 +00:00
|
|
|
.iter()
|
2022-06-08 15:56:57 +00:00
|
|
|
.map(clap::ValueEnum::to_possible_value)
|
2021-11-04 15:15:04 +00:00
|
|
|
.map(Option::unwrap)
|
|
|
|
.collect::<Vec<_>>(),
|
2022-01-10 21:41:58 +00:00
|
|
|
vec![
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::builder::PossibleValue::new("foo"),
|
|
|
|
clap::builder::PossibleValue::new("bar")
|
2022-01-10 21:41:58 +00:00
|
|
|
]
|
2021-11-04 15:15:04 +00:00
|
|
|
);
|
2022-01-10 21:41:58 +00:00
|
|
|
|
|
|
|
{
|
2022-06-08 15:56:57 +00:00
|
|
|
use clap::ValueEnum;
|
2022-01-10 21:41:58 +00:00
|
|
|
assert!(ArgChoice::from_str("foo", true).is_ok());
|
2022-03-30 01:16:11 +00:00
|
|
|
assert!(ArgChoice::from_str("bar", true).is_ok());
|
|
|
|
assert!(ArgChoice::from_str("baz", true).is_err());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_non_unit_variant() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2022-03-30 01:16:11 +00:00
|
|
|
#[allow(dead_code)] // silence warning about `Baz` being unused
|
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[value(skip)]
|
2022-03-30 01:16:11 +00:00
|
|
|
Baz(usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-06-08 15:56:57 +00:00
|
|
|
<ArgChoice as clap::ValueEnum>::value_variants()
|
2022-03-30 01:16:11 +00:00
|
|
|
.iter()
|
2022-06-08 15:56:57 +00:00
|
|
|
.map(clap::ValueEnum::to_possible_value)
|
2022-03-30 01:16:11 +00:00
|
|
|
.map(Option::unwrap)
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec![
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::builder::PossibleValue::new("foo"),
|
|
|
|
clap::builder::PossibleValue::new("bar")
|
2022-03-30 01:16:11 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
{
|
2022-06-08 15:56:57 +00:00
|
|
|
use clap::ValueEnum;
|
2022-03-30 01:16:11 +00:00
|
|
|
assert!(ArgChoice::from_str("foo", true).is_ok());
|
2022-01-10 21:41:58 +00:00
|
|
|
assert!(ArgChoice::from_str("bar", true).is_ok());
|
|
|
|
assert!(ArgChoice::from_str("baz", true).is_err());
|
|
|
|
}
|
2021-11-04 15:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_str_invalid() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2021-11-04 15:15:04 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
}
|
|
|
|
|
2022-01-10 21:41:58 +00:00
|
|
|
{
|
2022-06-08 15:56:57 +00:00
|
|
|
use clap::ValueEnum;
|
2022-01-10 21:41:58 +00:00
|
|
|
assert!(ArgChoice::from_str("bar", true).is_err());
|
|
|
|
}
|
2021-11-04 15:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_type() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, 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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum)]
|
2020-04-28 09:11:00 +00:00
|
|
|
arg: Option<ArgChoice>,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-28 09:11:00 +00:00
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt { arg: None }, Opt::try_parse_from([""]).unwrap());
|
2020-04-28 09:11:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: Some(ArgChoice::Foo)
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["", "foo"]).unwrap()
|
2020-04-28 09:11:00 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: Some(ArgChoice::Bar)
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["", "bar"]).unwrap()
|
2020-04-28 09:11:00 +00:00
|
|
|
);
|
2022-11-24 13:54:25 +00:00
|
|
|
assert!(Opt::try_parse_from(["", "fOo"]).is_err());
|
2020-04-28 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:12:05 +00:00
|
|
|
#[test]
|
2021-11-04 15:15:04 +00:00
|
|
|
fn option_option_type() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2021-10-25 19:12:05 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum, long)]
|
2021-10-25 19:12:05 +00:00
|
|
|
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
|
|
|
);
|
2021-10-25 19:12:05 +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()
|
2021-10-25 19:12:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: Some(Some(ArgChoice::Bar))
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["", "--arg", "bar"]).unwrap()
|
2021-10-25 19:12:05 +00:00
|
|
|
);
|
2022-11-24 13:54:25 +00:00
|
|
|
assert!(Opt::try_parse_from(["", "--arg", "fOo"]).is_err());
|
2021-10-25 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:11:00 +00:00
|
|
|
#[test]
|
2021-11-04 15:15:04 +00:00
|
|
|
fn vec_type() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, 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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum, short, long)]
|
2020-04-28 09:11:00 +00:00
|
|
|
arg: Vec<ArgChoice>,
|
2021-01-18 18:12:28 +00:00
|
|
|
}
|
2020-04-28 09:11:00 +00:00
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from([""]).unwrap());
|
2020-04-28 09:11:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: vec![ArgChoice::Foo]
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["", "-a", "foo"]).unwrap()
|
2020-04-28 09:11:00 +00:00
|
|
|
);
|
|
|
|
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()
|
2020-04-28 09:11:00 +00:00
|
|
|
);
|
2022-11-24 13:54:25 +00:00
|
|
|
assert!(Opt::try_parse_from(["", "-a", "fOo"]).is_err());
|
2020-04-28 09:11:00 +00:00
|
|
|
}
|
2021-06-08 17:52:09 +00:00
|
|
|
|
2021-10-25 19:12:05 +00:00
|
|
|
#[test]
|
2021-11-04 15:15:04 +00:00
|
|
|
fn option_vec_type() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2021-10-25 19:12:05 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum, short, long)]
|
2021-10-25 19:12:05 +00:00
|
|
|
arg: Option<Vec<ArgChoice>>,
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt { arg: None }, Opt::try_parse_from([""]).unwrap());
|
2021-10-25 19:12:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: Some(vec![ArgChoice::Foo])
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["", "-a", "foo"]).unwrap()
|
2021-10-25 19:12:05 +00:00
|
|
|
);
|
|
|
|
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()
|
2021-10-25 19:12:05 +00:00
|
|
|
);
|
2022-11-24 13:54:25 +00:00
|
|
|
assert!(Opt::try_parse_from(["", "-a", "fOo"]).is_err());
|
2021-10-25 19:12:05 +00:00
|
|
|
}
|
2022-03-07 03:18:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_type_default_value() {
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2022-03-07 03:18:55 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
Baz,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(
|
2022-06-08 15:56:57 +00:00
|
|
|
value_enum,
|
2022-03-07 03:18:55 +00:00
|
|
|
short,
|
|
|
|
long,
|
|
|
|
default_value = "foo,bar",
|
2022-07-22 14:19:14 +00:00
|
|
|
value_delimiter = ','
|
2022-03-07 03:18:55 +00:00
|
|
|
)]
|
|
|
|
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()
|
2022-03-07 03:18:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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()
|
2022-03-07 03:18:55 +00:00
|
|
|
);
|
|
|
|
}
|