Add raw attributes#26
Merged
Merged
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`.
TeXitoi
requested changes
Oct 31, 2017
TeXitoi
left a comment
Owner
There was a problem hiding this comment.
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.
Seems like it makes structopt more usable. No qualms here.
whoops, work account derp
| 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(); |
Owner
There was a problem hiding this comment.
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 { |
| 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())) |
Contributor
Author
|
Thanks for the review, I added some tests and fixed the rest. |
Owner
|
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. |
Owner
|
0.1.2 published |
Eijebong
pushed a commit
to Eijebong/structopt
that referenced
this pull request
Jan 2, 2019
* added fuzzy testing * revert formatting change * removed artifact test * revert metadata changes * added fuzz limits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_raware augmented toattribute_name(value)without quotingvalue.This fixes #4.
For enums, I changed
AppSettings::SubcommandRequiredtoAppSettings::SubcommandRequiredElseHelpif 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).