Skip to content
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

Merged
merged 2 commits into from
Apr 12, 2018

Conversation

birkenfeld
Copy link
Contributor

@birkenfeld birkenfeld commented Apr 9, 2018

Fix #21

if attrs.should_flatten() {
let ty = &field.ty;
return Some(quote!{
let #app_var = #ty ::augment_clap(#app_var);
Copy link
Contributor Author

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?

Copy link
Owner

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.

Copy link
Contributor Author

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().

Copy link
Owner

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.

@birkenfeld
Copy link
Contributor Author

I'll add documentation and an example if you're okay with the idea and the implementation.

Copy link
Owner

@TeXitoi TeXitoi left a 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.

@@ -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))
Copy link
Owner

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.

@birkenfeld
Copy link
Contributor Author

Updated!

Copy link
Owner

@TeXitoi TeXitoi left a 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 !

panic!("subcommand is only allowed on fields");
} else if let Kind::FlattenStruct = res.kind {
panic!("flatten is only allowed on fields");
Copy link
Owner

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.

if let Kind::Subcommand(_) = self.kind {
panic!("subcommands cannot be flattened");
}
self.kind = Kind::FlattenStruct;
Copy link
Owner

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.

#required
})
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange indentation.

@TeXitoi
Copy link
Owner

TeXitoi commented Apr 11, 2018

No more remarks after reading on a computer.

@birkenfeld
Copy link
Contributor Author

Ok, (hopefully) last round :)

@TeXitoi TeXitoi merged commit a914117 into TeXitoi:master Apr 12, 2018
@TeXitoi
Copy link
Owner

TeXitoi commented Apr 12, 2018

Thanks for your contribution and patience.

@TeXitoi
Copy link
Owner

TeXitoi commented Apr 12, 2018

v0.2.7 published

@birkenfeld birkenfeld deleted the flatten branch April 12, 2018 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants