2021-10-11 19:48:13 +00:00
|
|
|
use clap::{Args, Parser, Subcommand};
|
2020-02-13 15:21:01 +00:00
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-07-07 11:17:36 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2020-07-07 11:17:36 +00:00
|
|
|
sub: Box<Sub>,
|
|
|
|
}
|
2020-02-13 15:21:01 +00:00
|
|
|
|
2021-10-11 19:48:13 +00:00
|
|
|
#[derive(Subcommand, PartialEq, Debug)]
|
2020-07-07 11:17:36 +00:00
|
|
|
enum Sub {
|
|
|
|
Flame {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2020-07-07 11:17:36 +00:00
|
|
|
arg: Box<Ext>,
|
|
|
|
},
|
|
|
|
}
|
2020-02-13 15:21:01 +00:00
|
|
|
|
2021-10-11 19:48:13 +00:00
|
|
|
#[derive(Args, PartialEq, Debug)]
|
2020-07-07 11:17:36 +00:00
|
|
|
struct Ext {
|
|
|
|
arg: u32,
|
|
|
|
}
|
2020-02-13 15:21:01 +00:00
|
|
|
|
2020-07-07 11:17:36 +00:00
|
|
|
#[test]
|
|
|
|
fn boxed_flatten_subcommand() {
|
2020-02-13 15:21:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
sub: Box::new(Sub::Flame {
|
|
|
|
arg: Box::new(Ext { arg: 1 })
|
|
|
|
})
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "flame", "1"]).unwrap()
|
2020-02-13 15:21:01 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-07 11:17:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_boxed_flatten_subcommand() {
|
2022-11-24 13:54:25 +00:00
|
|
|
let mut opt = Opt::try_parse_from(["test", "flame", "1"]).unwrap();
|
2020-07-07 11:17:36 +00:00
|
|
|
|
2023-01-10 02:52:06 +00:00
|
|
|
opt.try_update_from(["test", "flame", "42"]).unwrap();
|
2020-07-07 11:17:36 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
sub: Box::new(Sub::Flame {
|
|
|
|
arg: Box::new(Ext { arg: 42 })
|
|
|
|
})
|
|
|
|
},
|
|
|
|
opt
|
|
|
|
);
|
|
|
|
}
|