mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
aa6594b334
This is a port of clap-rs/clap#3023
147 lines
2.7 KiB
Rust
147 lines
2.7 KiB
Rust
mod utils;
|
|
|
|
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> {
|
|
#[clap(flatten)]
|
|
pub inner: T,
|
|
}
|
|
|
|
assert_eq!(
|
|
Outer {
|
|
inner: Inner { answer: 42 }
|
|
},
|
|
Outer::parse_from(&["--answer", "42"])
|
|
)
|
|
}
|
|
|
|
#[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,
|
|
{
|
|
#[clap(flatten)]
|
|
pub inner: T,
|
|
}
|
|
|
|
assert_eq!(
|
|
Outer {
|
|
inner: Inner { answer: 42 }
|
|
},
|
|
Outer::parse_from(&["--answer", "42"])
|
|
)
|
|
}
|
|
|
|
#[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 }),
|
|
GenericEnum::parse_from(&["test", "start", "42"])
|
|
)
|
|
}
|
|
|
|
#[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 }),
|
|
GenericEnum::parse_from(&["test", "start", "42"])
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn generic_w_fromstr_trait_bound() {
|
|
use std::str::FromStr;
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
struct Opt<T>
|
|
where
|
|
T: FromStr,
|
|
<T as FromStr>::Err: std::error::Error + Sync + Send + 'static,
|
|
{
|
|
answer: T,
|
|
}
|
|
|
|
assert_eq!(
|
|
Opt::<isize> { answer: 42 },
|
|
Opt::<isize>::parse_from(&["--answer", "42"])
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn generic_wo_trait_bound() {
|
|
use std::time::Duration;
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
struct Opt<T> {
|
|
answer: isize,
|
|
#[clap(skip)]
|
|
took: Option<T>,
|
|
}
|
|
|
|
assert_eq!(
|
|
Opt::<Duration> {
|
|
answer: 42,
|
|
took: None
|
|
},
|
|
Opt::<Duration>::parse_from(&["--answer", "42"])
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn generic_where_clause_w_trailing_comma() {
|
|
use std::str::FromStr;
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
struct Opt<T>
|
|
where
|
|
T: FromStr,
|
|
<T as FromStr>::Err: std::error::Error + Sync + Send + 'static,
|
|
{
|
|
pub answer: T,
|
|
}
|
|
|
|
assert_eq!(
|
|
Opt::<isize> { answer: 42 },
|
|
Opt::<isize>::parse_from(&["--answer", "42"])
|
|
)
|
|
}
|