Fix custom help headings

This commit is contained in:
Artem Vorotnikov 2020-06-01 07:42:07 +03:00
parent 2da492e4dc
commit 2de5c0e44c
No known key found for this signature in database
GPG key ID: E0148C3F2FBB7A20
2 changed files with 25 additions and 20 deletions

View file

@ -695,7 +695,10 @@ impl<'b> App<'b> {
} else { } else {
None None
}; };
let arg = a.into().help_heading(help_heading); let mut arg = a.into();
if arg.get_help_heading().is_none() {
arg = arg.help_heading(help_heading);
}
self.args.push(arg); self.args.push(arg);
self self
} }

View file

@ -11,13 +11,16 @@ use std::{
use crate::{ use crate::{
build::{App, AppSettings, Arg, ArgSettings}, build::{App, AppSettings, Arg, ArgSettings},
output::{fmt::Colorizer, Usage}, output::{fmt::Colorizer, Usage},
parse::errors::{Error, Result as ClapResult}, parse::{
parse::Parser, errors::{Error, Result as ClapResult},
Parser,
},
util::VecMap, util::VecMap,
INTERNAL_ERROR_MSG, INTERNAL_ERROR_MSG,
}; };
// Third party // Third party
use indexmap::IndexSet;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
#[cfg(not(feature = "wrap_help"))] #[cfg(not(feature = "wrap_help"))]
@ -666,13 +669,14 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let subcmds = self.parser.has_visible_subcommands(); let subcmds = self.parser.has_visible_subcommands();
let custom_headings = self.parser.app.args.args.iter().fold(0, |acc, arg| { let custom_headings = self
if arg.help_heading.is_some() { .parser
acc + 1 .app
} else { .args
acc .args
} .iter()
}) > 0; .filter_map(|arg| arg.help_heading)
.collect::<IndexSet<_>>();
let mut first = if pos { let mut first = if pos {
self.warning("ARGS:\n")?; self.warning("ARGS:\n")?;
@ -717,15 +721,8 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
self.write_args(&opts)?; self.write_args(&opts)?;
first = false; first = false;
} }
if custom_headings { if !custom_headings.is_empty() {
for heading in self for heading in custom_headings {
.parser
.app
.help_headings
.iter()
.filter(|heading| heading.is_some())
.map(|heading| heading.unwrap())
{
if !first { if !first {
self.none("\n\n")?; self.none("\n\n")?;
} }
@ -736,7 +733,12 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
.args .args
.args .args
.iter() .iter()
.filter(|a| a.help_heading.is_some() && a.help_heading.unwrap() == heading) .filter(|a| {
if let Some(help_heading) = a.help_heading {
return help_heading == heading;
}
false
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
self.write_args(&*args)?; self.write_args(&*args)?;
first = false first = false