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

Add raw attributes #26

Merged
merged 2 commits into from
Nov 1, 2017
Merged

Add raw attributes #26

merged 2 commits into from
Nov 1, 2017

Conversation

Flakebi
Copy link
Contributor

@Flakebi Flakebi commented Oct 30, 2017

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.

This fixes #4.

For enums, I changed AppSettings::SubcommandRequired to AppSettings::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).

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`.
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 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);
Copy link
Owner

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?

Copy link

@williamyaoh-ah williamyaoh-ah Oct 31, 2017

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

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

@TeXitoi TeXitoi Oct 31, 2017

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 {
Copy link
Owner

Choose a reason for hiding this comment

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

👍 great factoring

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

Choose a reason for hiding this comment

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

s/auther/author/

@Flakebi
Copy link
Contributor Author

Flakebi commented Nov 1, 2017

Thanks for the review, I added some tests and fixed the rest.

@TeXitoi
Copy link
Owner

TeXitoi commented Nov 1, 2017

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.

@TeXitoi TeXitoi merged commit ebde32a into TeXitoi:master Nov 1, 2017
@TeXitoi
Copy link
Owner

TeXitoi commented Nov 1, 2017

0.1.2 published

@CAD97 CAD97 mentioned this pull request Feb 17, 2018
11 tasks
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
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.

Support clap::Arg method not taking just one &str
3 participants