Skip to content

Add raw attributes#26

Merged
TeXitoi merged 2 commits into
TeXitoi:masterfrom
Flakebi:master
Nov 1, 2017
Merged

Add raw attributes#26
TeXitoi merged 2 commits into
TeXitoi:masterfrom
Flakebi:master

Conversation

@Flakebi

@Flakebi Flakebi commented Oct 30, 2017

Copy link
Copy Markdown
Contributor

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`.

@TeXitoi TeXitoi left a comment

Copy link
Copy Markdown
Owner

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
Copy Markdown
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?

@williamyaoh-ah williamyaoh-ah Oct 31, 2017

Copy link
Copy Markdown

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

Comment thread 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();

@TeXitoi TeXitoi Oct 31, 2017

Copy link
Copy Markdown
Owner

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
Copy Markdown
Owner

Choose a reason for hiding this comment

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

👍 great factoring

Comment thread 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()))

Copy link
Copy Markdown
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

Flakebi commented Nov 1, 2017

Copy link
Copy Markdown
Contributor Author

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

@TeXitoi

TeXitoi commented Nov 1, 2017

Copy link
Copy Markdown
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.

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

TeXitoi commented Nov 1, 2017

Copy link
Copy Markdown
Owner

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