2021-11-14 08:55:24 +00:00
|
|
|
use clap::{Args, Parser};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_struct_flatten() {
|
|
|
|
#[derive(Args, PartialEq, Debug)]
|
|
|
|
struct Inner {
|
|
|
|
pub answer: isize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Outer<T: Args> {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2021-11-14 08:55:24 +00:00
|
|
|
pub inner: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Outer {
|
|
|
|
inner: Inner { answer: 42 }
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Outer::parse_from(["--answer", "42"])
|
2021-11-14 08:55:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_struct_flatten_w_where_clause() {
|
|
|
|
#[derive(Args, PartialEq, Debug)]
|
|
|
|
struct Inner {
|
|
|
|
pub answer: isize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Outer<T>
|
|
|
|
where
|
|
|
|
T: Args,
|
|
|
|
{
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2021-11-14 08:55:24 +00:00
|
|
|
pub inner: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Outer {
|
|
|
|
inner: Inner { answer: 42 }
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Outer::parse_from(["--answer", "42"])
|
2021-11-14 08:55:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_enum() {
|
|
|
|
#[derive(Args, PartialEq, Debug)]
|
|
|
|
struct Inner {
|
|
|
|
pub answer: isize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
enum GenericEnum<T: Args> {
|
|
|
|
Start(T),
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
GenericEnum::Start(Inner { answer: 42 }),
|
2022-11-24 13:54:25 +00:00
|
|
|
GenericEnum::parse_from(["test", "start", "42"])
|
2021-11-14 08:55:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_enum_w_where_clause() {
|
|
|
|
#[derive(Args, PartialEq, Debug)]
|
|
|
|
struct Inner {
|
|
|
|
pub answer: isize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
enum GenericEnum<T>
|
|
|
|
where
|
|
|
|
T: Args,
|
|
|
|
{
|
|
|
|
Start(T),
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
GenericEnum::Start(Inner { answer: 42 }),
|
2022-11-24 13:54:25 +00:00
|
|
|
GenericEnum::parse_from(["test", "start", "42"])
|
2021-11-14 08:55:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_w_fromstr_trait_bound() {
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt<T>
|
|
|
|
where
|
2022-05-23 20:42:34 +00:00
|
|
|
T: FromStr + Send + Sync + Clone + 'static,
|
2021-11-14 08:55:24 +00:00
|
|
|
<T as FromStr>::Err: std::error::Error + Sync + Send + 'static,
|
|
|
|
{
|
|
|
|
answer: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt::<isize> { answer: 42 },
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::<isize>::parse_from(["--answer", "42"])
|
2021-11-14 08:55:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_wo_trait_bound() {
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt<T> {
|
|
|
|
answer: isize,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(skip)]
|
2021-11-14 08:55:24 +00:00
|
|
|
took: Option<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt::<Duration> {
|
|
|
|
answer: 42,
|
|
|
|
took: None
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::<Duration>::parse_from(["--answer", "42"])
|
2021-11-14 08:55:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_where_clause_w_trailing_comma() {
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt<T>
|
|
|
|
where
|
2022-05-23 20:42:34 +00:00
|
|
|
T: FromStr + Send + Sync + Clone + 'static,
|
2021-11-14 08:55:24 +00:00
|
|
|
<T as FromStr>::Err: std::error::Error + Sync + Send + 'static,
|
|
|
|
{
|
|
|
|
pub answer: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt::<isize> { answer: 42 },
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::<isize>::parse_from(["--answer", "42"])
|
2021-11-14 08:55:24 +00:00
|
|
|
)
|
|
|
|
}
|