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 * Add `StructOpt::from_iter_safe()`, which returns an `Error` instead of
killing the program when it fails to parse, or parses one of the killing the program when it fails to parse, or parses one of the
short-circuiting flags. ([#98](https://github.com/TeXitoi/structopt/pull/98) short-circuiting flags. ([#98](https://github.com/TeXitoi/structopt/pull/98)
by [@quodlibetor](https://github.com/quodlibetor)) by [@quodlibetor](https://github.com/quodlibetor))
* Allow users to enable `clap` features independently by
* Allow users to enable `clap` features independently. [@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) # 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) * Add flattening, the insertion of options of another StructOpt struct
* Fail compilation when using `default_value` or `required` with `Option` ([#88](https://github.com/TeXitoi/structopt/pull/88)) by [@Kerollmops](https://github.com/Kerollmops) 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) # v0.2.6 (2018-03-31)

View file

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

View file

@ -1,6 +1,6 @@
[package] [package]
name = "structopt-derive" name = "structopt-derive"
version = "0.2.7" version = "0.2.8"
authors = ["Guillaume Pinot <texitoi@texitoi.eu>"] authors = ["Guillaume Pinot <texitoi@texitoi.eu>"]
description = "Parse command line argument by defining a struct, derive crate." description = "Parse command line argument by defining a struct, derive crate."
documentation = "https://docs.rs/structopt-derive" 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::Subcommand(_) => None,
Kind::FlattenStruct => { Kind::FlattenStruct => {
let ty = &field.ty; 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) => { Kind::Arg(ty) => {
let convert_type = match 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"]); let result = Opt::from_iter(&["test", "daemon", "start"]);
assert_eq!(Opt::Daemon(DaemonCommand::Start), result); 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 });
}