From 39b1ef076e6e1200f0c249359de894cae9a0f334 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 3 Oct 2022 15:40:59 -0500 Subject: [PATCH 1/2] test(derive): Show derive bug --- tests/derive/doc_comments_help.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/derive/doc_comments_help.rs b/tests/derive/doc_comments_help.rs index d90a04bc90c..f655859e31e 100644 --- a/tests/derive/doc_comments_help.rs +++ b/tests/derive/doc_comments_help.rs @@ -14,7 +14,7 @@ use crate::utils; -use clap::{CommandFactory, Parser, ValueEnum}; +use clap::{CommandFactory, Parser, Subcommand, ValueEnum}; #[test] fn doc_comments() { @@ -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 + +Commands: + child + help Print this message or the help of the given subcommand(s) + +Options: + -h, --help Print help information +"; + utils::assert_output::("cmd --help", OUTPUT, false); +} From 7ceb08ac70fa6ebaab6dc60415cc8542da8387ce Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 3 Oct 2022 15:42:26 -0500 Subject: [PATCH 2/2] fix(derive): Apply doc comments for `#[command(subcommand)]` This was broken in 8a5a9ba931dd45a4976a5600eb24a91b202472f3. I had missed this case for parsing doc comments. --- clap_derive/src/item.rs | 8 +++++++- tests/derive/doc_comments_help.rs | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/clap_derive/src/item.rs b/clap_derive/src/item.rs index 8b60b1f8428..68af9d62836 100644 --- a/clap_derive/src/item.rs +++ b/clap_derive/src/item.rs @@ -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); } @@ -155,6 +155,9 @@ impl Item { "methods are not allowed for flattened entry" ); } + + // ignore doc comments + res.doc_comment = vec![]; } Kind::Subcommand(_) @@ -228,6 +231,9 @@ impl Item { "methods are not allowed for flattened entry" ); } + + // ignore doc comments + res.doc_comment = vec![]; } Kind::Subcommand(_) => { diff --git a/tests/derive/doc_comments_help.rs b/tests/derive/doc_comments_help.rs index f655859e31e..d6d3db3d0a9 100644 --- a/tests/derive/doc_comments_help.rs +++ b/tests/derive/doc_comments_help.rs @@ -268,7 +268,7 @@ fn respect_subcommand_doc_comment() { Usage: cmd Commands: - child + child For child help Print this message or the help of the given subcommand(s) Options: