Merge pull request #2606 from epage/subcommand

fix(derive): Allow partial update of Subcommand arguments
This commit is contained in:
Pavan Kumar Sunkara 2021-07-25 14:34:37 +01:00 committed by GitHub
commit 077d43beeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 3 deletions

View file

@ -187,9 +187,17 @@ fn gen_augment(
Unit => quote!( #app_var ),
Unnamed(FieldsUnnamed { ref unnamed, .. }) if unnamed.len() == 1 => {
let ty = &unnamed[0];
quote_spanned! { ty.span()=>
{
<#ty as clap::Args>::augment_args(#app_var)
if override_required {
quote_spanned! { ty.span()=>
{
<#ty as clap::Args>::augment_args_for_update(#app_var)
}
}
} else {
quote_spanned! { ty.span()=>
{
<#ty as clap::Args>::augment_args(#app_var)
}
}
}
}

View file

@ -313,3 +313,39 @@ fn enum_in_enum_subsubcommand() {
let result = Opt::parse_from(&["test", "daemon", "start"]);
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
}
#[test]
fn update_subcommands() {
#[derive(Clap, PartialEq, Debug)]
enum Opt {
Command1(Command1),
Command2(Command2),
}
#[derive(Clap, PartialEq, Debug)]
struct Command1 {
arg1: i32,
arg2: i32,
}
#[derive(Clap, PartialEq, Debug)]
struct Command2 {
arg2: i32,
}
// Full subcommand update
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
opt.try_update_from(&["test", "command1", "42", "44"])
.unwrap();
assert_eq!(Opt::parse_from(&["test", "command1", "42", "44"]), opt);
// Partial subcommand update
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
opt.try_update_from(&["test", "command1", "42"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "command1", "42", "14"]), opt);
// Change subcommand
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
opt.try_update_from(&["test", "command2", "43"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "command2", "43"]), opt);
}