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

fix(derive): Apply doc comments for #[command(subcommand)] #4340

Merged
merged 2 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion clap_derive/src/item.rs
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
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);
}