Skip to content

Commit

Permalink
Merge pull request #4340 from epage/doc-comment
Browse files Browse the repository at this point in the history
fix(derive): Apply doc comments for `#[command(subcommand)]`
  • Loading branch information
epage authored Oct 3, 2022
2 parents 7f98947 + 7ceb08a commit a9cc73c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion clap_derive/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Item {
let parsed_attrs = ClapAttr::parse_all(&variant.attrs);
res.infer_kind(&parsed_attrs);
res.push_attrs(&parsed_attrs);
if matches!(&*res.kind, Kind::Command(_)) {
if matches!(&*res.kind, Kind::Command(_) | Kind::Subcommand(_)) {
res.push_doc_comment(&variant.attrs, "about", true);
}

Expand All @@ -155,6 +155,9 @@ impl Item {
"methods are not allowed for flattened entry"
);
}

// ignore doc comments
res.doc_comment = vec![];
}

Kind::Subcommand(_)
Expand Down Expand Up @@ -228,6 +231,9 @@ impl Item {
"methods are not allowed for flattened entry"
);
}

// ignore doc comments
res.doc_comment = vec![];
}

Kind::Subcommand(_) => {
Expand Down
30 changes: 29 additions & 1 deletion tests/derive/doc_comments_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::utils;

use clap::{CommandFactory, Parser, ValueEnum};
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};

#[test]
fn doc_comments() {
Expand Down Expand Up @@ -248,3 +248,31 @@ fn doc_comment_about_handles_both_abouts() {
// comment.
assert_eq!(cmd.get_long_about(), None);
}

#[test]
fn respect_subcommand_doc_comment() {
#[derive(Parser, Debug)]
pub enum Cmd {
/// For child
#[command(subcommand)]
Child(Child),
}

#[derive(Subcommand, Debug)]
pub enum Child {
One,
Twp,
}

const OUTPUT: &str = "\
Usage: cmd <COMMAND>
Commands:
child For child
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help information
";
utils::assert_output::<Cmd>("cmd --help", OUTPUT, false);
}

0 comments on commit a9cc73c

Please sign in to comment.