-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support flattening common options from a struct (#21) #92
Conversation
structopt-derive/src/lib.rs
Outdated
if attrs.should_flatten() { | ||
let ty = &field.ty; | ||
return Some(quote!{ | ||
let #app_var = #ty ::augment_clap(#app_var); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this errors out with a somewhat strange message if #ty
is something like Option<Foo>
. Is there a good way to check for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error is something like "no augment_clap method on Option"? I don't have any idea, but I suppose you have a similar error with an enum Cmd { Foo(Foo) }
with no derive(StructOpt)
on Foo
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's some syntax error because you can't call Option<Foo>::method()
, you'd have to do Option::<Foo>::method()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let #app_var = <#ty>::augment_clap(#app_var);
Maybe you can update other similar code gen.
I'll add documentation and an example if you're okay with the idea and the implementation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Great work! Only minor remarks.
Please also update the CHANGELOG.md.
structopt-derive/src/lib.rs
Outdated
@@ -141,6 +151,8 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>) -> quote::Tokens { | |||
_ => quote!( .unwrap() ) | |||
}; | |||
quote!(#field_name: #subcmd_type::from_subcommand(matches.subcommand())#unwrapper) | |||
} else if attrs.should_flatten() { | |||
quote!(#field_name: StructOpt::from_clap(matches)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usage seems always to be if attrs.is_subcommand() { ... } else if attrs.should_flatten() { ... } else { ... }
.
Maybe an enum Mode { SubCommand, Flatten, Arg }
(or other naming if you're more inspired than me) would be better?
Also, checking that we don't subcommand
and flatten
on the same field seems reasonable.
Updated! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On mobile and difficult to follow with the reindentations. Will try to review this evening on a real computer. But it seems great !
structopt-derive/src/attrs.rs
Outdated
panic!("subcommand is only allowed on fields"); | ||
} else if let Kind::FlattenStruct = res.kind { | ||
panic!("flatten is only allowed on fields"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A match would be better looking here.
structopt-derive/src/attrs.rs
Outdated
if let Kind::Subcommand(_) = self.kind { | ||
panic!("subcommands cannot be flattened"); | ||
} | ||
self.kind = Kind::FlattenStruct; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A setter with check can mutualize this code.
structopt-derive/src/lib.rs
Outdated
#required | ||
}) | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange indentation.
No more remarks after reading on a computer. |
Ok, (hopefully) last round :) |
Thanks for your contribution and patience. |
v0.2.7 published |
Fix #21