Fix a bug when flattening an enum

Fix #103
This commit is contained in:
Guillaume Pinot 2018-04-28 19:03:19 +02:00
parent 4111f9c6f8
commit 70fbd88ae7
5 changed files with 40 additions and 9 deletions

View file

@ -1,16 +1,23 @@
# NEXT
# v0.2.8 (2018-04-28)
* Add `StructOpt::from_iter_safe()`, which returns an `Error` instead of
killing the program when it fails to parse, or parses one of the
short-circuiting flags. ([#98](https://github.com/TeXitoi/structopt/pull/98)
by [@quodlibetor](https://github.com/quodlibetor))
* Allow users to enable `clap` features independently.
* Allow users to enable `clap` features independently by
[@Kerollmops](https://github.com/Kerollmops)
* Fix a bug when flattening an enum
([#103](https://github.com/TeXitoi/structopt/pull/103) by
[@TeXitoi](https://github.com/TeXitoi)
# v0.2.7 (2018-04-12)
* Add flattening, the insertion of options of another StructOpt struct into another ([#92](https://github.com/TeXitoi/structopt/pull/92)) by [@birkenfeld](https://github.com/birkenfeld)
* Fail compilation when using `default_value` or `required` with `Option` ([#88](https://github.com/TeXitoi/structopt/pull/88)) by [@Kerollmops](https://github.com/Kerollmops)
* Add flattening, the insertion of options of another StructOpt struct
into another ([#92](https://github.com/TeXitoi/structopt/pull/92))
by [@birkenfeld](https://github.com/birkenfeld)
* Fail compilation when using `default_value` or `required` with
`Option` ([#88](https://github.com/TeXitoi/structopt/pull/88)) by
[@Kerollmops](https://github.com/Kerollmops)
# v0.2.6 (2018-03-31)

View file

@ -1,6 +1,6 @@
[package]
name = "structopt"
version = "0.2.7"
version = "0.2.8"
authors = ["Guillaume Pinot <texitoi@texitoi.eu>"]
description = "Parse command line argument by defining a struct."
documentation = "https://docs.rs/structopt"
@ -27,6 +27,6 @@ travis-ci = { repository = "TeXitoi/structopt" }
[dependencies]
clap = { version = "2.20", default-features = false }
structopt-derive = { path = "structopt-derive", version = "0.2.7" }
structopt-derive = { path = "structopt-derive", version = "0.2.8" }
[workspace]

View file

@ -1,6 +1,6 @@
[package]
name = "structopt-derive"
version = "0.2.7"
version = "0.2.8"
authors = ["Guillaume Pinot <texitoi@texitoi.eu>"]
description = "Parse command line argument by defining a struct, derive crate."
documentation = "https://docs.rs/structopt-derive"

View file

@ -90,7 +90,14 @@ fn gen_augmentation(fields: &Punctuated<Field, Comma>, app_var: &Ident) -> quote
Kind::Subcommand(_) => None,
Kind::FlattenStruct => {
let ty = &field.ty;
Some(quote! { let #app_var = <#ty>::augment_clap(#app_var); })
Some(quote! {
let #app_var = <#ty>::augment_clap(#app_var);
let #app_var = if <#ty>::is_subcommand() {
#app_var.setting(::structopt::clap::AppSettings::SubcommandRequiredElseHelp)
} else {
#app_var
};
})
}
Kind::Arg(ty) => {
let convert_type = match ty {

View file

@ -160,3 +160,20 @@ fn enum_in_enum_subsubcommand() {
let result = Opt::from_iter(&["test", "daemon", "start"]);
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
}
#[test]
fn flatten_enum() {
#[derive(StructOpt, Debug, PartialEq)]
struct Opt {
#[structopt(flatten)]
sub_cmd: SubCmd,
}
#[derive(StructOpt, Debug, PartialEq)]
enum SubCmd {
Foo,
Bar,
}
assert!(Opt::from_iter_safe(&["test"]).is_err());
assert_eq!(Opt::from_iter(&["test", "Foo"]), Opt { sub_cmd: SubCmd::Foo });
}