Skip to content

Commit b1bb82c

Browse files
committed
fix issue with about text for subcommands
1 parent 2dc095a commit b1bb82c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

conf_derive/src/subcommand_proc_macro_options/variant_item.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ impl VariantItem {
324324
let command_name = &self.command_name;
325325
let aliases = &self.aliases;
326326

327+
// Generate .about() call if we have a doc string
328+
let about_call = self.doc_string.as_ref().map(|doc| {
329+
quote! { .about(#doc) }
330+
});
331+
327332
if let Some(ty) = self.variant_type.as_ref() {
328333
// Single unnamed field variant
329334
let inner_type = self.is_optional_type.as_ref().unwrap_or(ty);
@@ -334,6 +339,7 @@ impl VariantItem {
334339
#parsers_ident.push(
335340
<#inner_type as ::conf::Conf>::get_parser(#parsed_env_ident, program_options)?
336341
.rename(#command_name)
342+
#about_call
337343
#(.add_alias(#aliases))*
338344
);
339345
}
@@ -347,6 +353,7 @@ impl VariantItem {
347353
#parsers_ident.push(
348354
<#struct_name as ::conf::Conf>::get_parser(#parsed_env_ident, program_options)?
349355
.rename(#command_name)
356+
#about_call
350357
#(.add_alias(#aliases))*
351358
);
352359
}
@@ -357,6 +364,7 @@ impl VariantItem {
357364
#parsers_ident.push(
358365
::conf::Parser::new(::conf::ParserConfig::default(), vec![], &[], #parsed_env_ident)?
359366
.rename(#command_name)
367+
#about_call
360368
#(.add_alias(#aliases))*
361369
);
362370
})

src/parser.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ impl Parser {
231231
command = command.after_help(after_help_text);
232232
}
233233

234-
command.build();
234+
// Note: We intentionally do NOT call command.build() here.
235+
// build() configures help args based on whether long_about is set.
236+
// For subcommand parsers, .about() is called AFTER Parser::new() returns,
237+
// so we must defer build() until all configuration is complete.
238+
// clap will call build() automatically during try_get_matches_from().
235239

236240
Ok(Self {
237241
options,
@@ -258,6 +262,17 @@ impl Parser {
258262
self
259263
}
260264

265+
/// Set the about text for a parser. (This is used by subcommands)
266+
///
267+
/// The first line becomes the short `about` (shown in command listings),
268+
/// and the full text becomes `long_about` (shown in subcommand --help).
269+
pub fn about(mut self, about: impl Into<String>) -> Self {
270+
let full_text = about.into();
271+
let first_line = full_text.lines().next().unwrap_or("").to_string();
272+
self.command = self.command.about(first_line).long_about(full_text);
273+
self
274+
}
275+
261276
/// Get command associated to this parser
262277
pub fn get_command(&self) -> &Command {
263278
&self.command

0 commit comments

Comments
 (0)