Skip to content

Commit a9cc73c

Browse files
authored
Merge pull request #4340 from epage/doc-comment
fix(derive): Apply doc comments for `#[command(subcommand)]`
2 parents 7f98947 + 7ceb08a commit a9cc73c

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

clap_derive/src/item.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Item {
143143
let parsed_attrs = ClapAttr::parse_all(&variant.attrs);
144144
res.infer_kind(&parsed_attrs);
145145
res.push_attrs(&parsed_attrs);
146-
if matches!(&*res.kind, Kind::Command(_)) {
146+
if matches!(&*res.kind, Kind::Command(_) | Kind::Subcommand(_)) {
147147
res.push_doc_comment(&variant.attrs, "about", true);
148148
}
149149

@@ -155,6 +155,9 @@ impl Item {
155155
"methods are not allowed for flattened entry"
156156
);
157157
}
158+
159+
// ignore doc comments
160+
res.doc_comment = vec![];
158161
}
159162

160163
Kind::Subcommand(_)
@@ -228,6 +231,9 @@ impl Item {
228231
"methods are not allowed for flattened entry"
229232
);
230233
}
234+
235+
// ignore doc comments
236+
res.doc_comment = vec![];
231237
}
232238

233239
Kind::Subcommand(_) => {

tests/derive/doc_comments_help.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use crate::utils;
1616

17-
use clap::{CommandFactory, Parser, ValueEnum};
17+
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
1818

1919
#[test]
2020
fn doc_comments() {
@@ -248,3 +248,31 @@ fn doc_comment_about_handles_both_abouts() {
248248
// comment.
249249
assert_eq!(cmd.get_long_about(), None);
250250
}
251+
252+
#[test]
253+
fn respect_subcommand_doc_comment() {
254+
#[derive(Parser, Debug)]
255+
pub enum Cmd {
256+
/// For child
257+
#[command(subcommand)]
258+
Child(Child),
259+
}
260+
261+
#[derive(Subcommand, Debug)]
262+
pub enum Child {
263+
One,
264+
Twp,
265+
}
266+
267+
const OUTPUT: &str = "\
268+
Usage: cmd <COMMAND>
269+
270+
Commands:
271+
child For child
272+
help Print this message or the help of the given subcommand(s)
273+
274+
Options:
275+
-h, --help Print help information
276+
";
277+
utils::assert_output::<Cmd>("cmd --help", OUTPUT, false);
278+
}

0 commit comments

Comments
 (0)