Skip to content

Allow doc comment to set help text#14

Merged
TeXitoi merged 3 commits into
TeXitoi:masterfrom
killercup:feature/doc-comments-help
Jun 16, 2017
Merged

Allow doc comment to set help text#14
TeXitoi merged 3 commits into
TeXitoi:masterfrom
killercup:feature/doc-comments-help

Conversation

@killercup

@killercup killercup commented Jun 15, 2017

Copy link
Copy Markdown
Contributor

This treats doc comments as help/about attributes, which may lead to concatenating the original help text with the doc comment.

I didn't add any tests except for changing the 'basic' example and checking the CLI output myself. If there is anything you want me to add in that regard, let me know :)

fix #13

This treats doc comments as `help`/`about` attributes, which may lead to
concatenating the original help text with the doc comment.
#[derive(Debug, Clone, Copy)]
enum AttrSource { Struct, Field, }

fn extract_attrs<'a>(attrs: &'a [Attribute], attr_source: AttrSource) -> Box<Iterator<Item = (Ident, Lit)> + 'a> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was pretty close to making this a Box<Iterator<Item = (Cow<'a, Ident>, Cow<'a, Lit>)> but luckily I noticed early enough that this was derive macro whose performance will probably never matter 😅

@TeXitoi

TeXitoi commented Jun 15, 2017

Copy link
Copy Markdown
Owner

Great work, thanks!

I'd prefer that if help is provided, help is used, else the docstring is used. But it looks like the last seen will be used, thus better to revert the chain. To be sure of the result, can you add an example in tests/ with about and a docstring on the struct, and with help and a docstring on a field, and testing with ArgsMatches::usage() ?

Thanks.

This changes the behavior of `from_attr_or_env` (used for clap
application settings) to favor the last occurrence of the requested
attribute. This is what repeatedly calling the same methods to set field
properties does implicitly.
@killercup killercup changed the title WIP: Allow doc comment to set help text Allow doc comment to set help text Jun 15, 2017
@killercup

Copy link
Copy Markdown
Contributor Author

Alright, help/about is now preferred if the doc string is also set, and I added some tests comparing the help output that clap's write_long_help gives me.

@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!

Comment thread structopt-derive/src/lib.rs Outdated
}

fn from_attr_or_env(attrs: &[(&Ident, &Lit)], key: &str, env: &str) -> Lit {
fn from_attr_or_env<'a>(attrs: &[(Ident, Lit)], key: &str, env: &str) -> Lit {

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.

Useless lifetime parameter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@TeXitoi TeXitoi merged commit 483396d into TeXitoi:master Jun 16, 2017
@TeXitoi

TeXitoi commented Jun 16, 2017

Copy link
Copy Markdown
Owner

Thanks! I'll update crates.io after eating.

@killercup

Copy link
Copy Markdown
Contributor Author

Thanks! Bon appétit!

@TeXitoi

TeXitoi commented Jun 16, 2017

Copy link
Copy Markdown
Owner

Published

@killercup

Copy link
Copy Markdown
Contributor Author

I think you just published a new version of structopt but not structopt-derive?

@TeXitoi

TeXitoi commented Jun 16, 2017

Copy link
Copy Markdown
Owner

oups, should be fixed

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

Consider using field doc comment as help text by default

2 participants