-
Notifications
You must be signed in to change notification settings - Fork 152
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
Add raw attributes #26
Conversation
This makes it possible to call clap functions that don't take strings, but any arbitrary value. Even functions that take more than one argument can be called. All attributes that are called `attribute_name_raw` are augmented to `attribute_name(value)` without quoting `value`.
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 job, thanks! It could be great if you also add a few tests in tests/
, nothing complete, but some typical example as an integer parameter, a slice parameter and a 2 parameters method for example.
.about(#about) | ||
.setting(_structopt::clap::AppSettings::SubcommandRequired); | ||
let app = #gen | ||
.setting(_structopt::clap::AppSettings::SubcommandRequiredElseHelp); |
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.
That's OK for me. @williamyaoh OK?
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.
Seems like it makes structopt
more usable. No qualms here.
whoops, work account derp
structopt-derive/src/lib.rs
Outdated
if key.ends_with("_raw") { | ||
let key = Ident::from(&key[..(key.len() - 4)]); | ||
// Call method without quoting the string | ||
let ts = syn::parse_token_trees(val).unwrap(); |
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.
I think something like .expect("bad parameter to {}: the parameter must be valid rust code", key_with_raw)
will help debugging
let version = from_attr_or_env(&struct_attrs, "version", "CARGO_PKG_VERSION"); | ||
let author = format_author(from_attr_or_env(&struct_attrs, "author", "CARGO_PKG_AUTHORS")); | ||
let about = from_attr_or_env(&struct_attrs, "about", "CARGO_PKG_DESCRIPTION"); | ||
fn gen_clap(attrs: &[Attribute]) -> quote::Tokens { |
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 factoring
structopt-derive/src/lib.rs
Outdated
let author = format_author(from_attr_or_env(&attrs, "author", "CARGO_PKG_AUTHORS")); | ||
let about = from_attr_or_env(&attrs, "about", "CARGO_PKG_DESCRIPTION"); | ||
let settings = attrs.iter() | ||
.filter(|&&(ref i, _)| !["name", "version", "auther", "about"].contains(&i.as_ref())) |
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.
s/auther/author/
Thanks for the review, I added some tests and fixed the rest. |
Thanks! I'll submit to crates.io when I'll have the time. I'll post here. If I didn't do it within 48h feel free to remind me here. |
0.1.2 published |
* added fuzzy testing * revert formatting change * removed artifact test * revert metadata changes * added fuzz limits
This makes it possible to call clap functions that don't take strings,
but any arbitrary value. Even functions that take more than one argument
can be called.
All attributes that are called
attribute_name_raw
are augmented toattribute_name(value)
without quotingvalue
.This fixes #4.
For enums, I changed
AppSettings::SubcommandRequired
toAppSettings::SubcommandRequiredElseHelp
if that's ok?I find it much more useful if an application lists the help instead of telling me to get the help manually.
Unfortunately this behaviour of structopt cannot be overwritten by the user (no matter which one is used).