2878: fix: Give Arg::help_heading priority over App::help_heading r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
This commit is contained in:
bors[bot] 2021-10-15 07:36:19 +00:00 committed by GitHub
commit cd00a9408a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 26 deletions

View file

@ -1086,23 +1086,20 @@ impl<'help> App<'help> {
/// [argument]: Arg /// [argument]: Arg
pub fn arg<A: Into<Arg<'help>>>(mut self, a: A) -> Self { pub fn arg<A: Into<Arg<'help>>>(mut self, a: A) -> Self {
let mut arg = a.into(); let mut arg = a.into();
if let Some(help_heading) = self.current_help_heading { arg.help_heading.get_or_insert(self.current_help_heading);
arg = arg.help_heading(Some(help_heading));
}
self.args.push(arg); self.args.push(arg);
self self
} }
/// Set a custom section heading for future args. Every call to [`App::arg`] /// Set the default section heading for future args.
/// (and its related methods) will use this header (instead of the default ///
/// header for the specified argument type) until a subsequent call to /// This will be used for any arg that hasn't had [`Arg::help_heading`] called.
/// [`App::help_heading`].
/// ///
/// This is useful if the default `OPTIONS` or `ARGS` headings are /// This is useful if the default `OPTIONS` or `ARGS` headings are
/// not specific enough for one's use case. /// not specific enough for one's use case.
/// ///
/// [`App::arg`]: App::arg() /// [`App::arg`]: App::arg()
/// [`App::help_heading`]: App::help_heading() /// [`Arg::help_heading`]: crate::Arg::help_heading()
#[inline] #[inline]
pub fn help_heading<O>(mut self, heading: O) -> Self pub fn help_heading<O>(mut self, heading: O) -> Self
where where

View file

@ -118,7 +118,7 @@ pub struct Arg<'help> {
pub(crate) env: Option<(&'help OsStr, Option<OsString>)>, pub(crate) env: Option<(&'help OsStr, Option<OsString>)>,
pub(crate) terminator: Option<&'help str>, pub(crate) terminator: Option<&'help str>,
pub(crate) index: Option<usize>, pub(crate) index: Option<usize>,
pub(crate) help_heading: Option<&'help str>, pub(crate) help_heading: Option<Option<&'help str>>,
pub(crate) global: bool, pub(crate) global: bool,
pub(crate) exclusive: bool, pub(crate) exclusive: bool,
pub(crate) value_hint: ValueHint, pub(crate) value_hint: ValueHint,
@ -156,7 +156,7 @@ impl<'help> Arg<'help> {
/// Get the help heading specified for this argument, if any /// Get the help heading specified for this argument, if any
#[inline] #[inline]
pub fn get_help_heading(&self) -> Option<&str> { pub fn get_help_heading(&self) -> Option<&str> {
self.help_heading self.help_heading.unwrap_or_default()
} }
/// Get the short option name for this argument, if any /// Get the short option name for this argument, if any
@ -4699,7 +4699,7 @@ impl<'help> Arg<'help> {
where where
O: Into<Option<&'help str>>, O: Into<Option<&'help str>>,
{ {
self.help_heading = heading.into(); self.help_heading = Some(heading.into());
self self
} }

View file

@ -755,7 +755,7 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> {
.app .app
.args .args
.args() .args()
.filter_map(|arg| arg.help_heading) .filter_map(|arg| arg.get_help_heading())
.collect::<IndexSet<_>>(); .collect::<IndexSet<_>>();
let mut first = if !pos.is_empty() { let mut first = if !pos.is_empty() {
@ -783,7 +783,7 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> {
.args .args
.args() .args()
.filter(|a| { .filter(|a| {
if let Some(help_heading) = a.help_heading { if let Some(help_heading) = a.get_help_heading() {
return help_heading == heading; return help_heading == heading;
} }
false false

View file

@ -1788,14 +1788,17 @@ OPTIONS:
-f, --fake <some>:<val> some help -f, --fake <some>:<val> some help
-h, --help Print help information -h, --help Print help information
-s, --speed <SPEED> How fast? [possible values: fast, slow] -s, --speed <SPEED> How fast? [possible values: fast, slow]
--style <style> Choose musical style to play the song
-V, --version Print version information -V, --version Print version information
NETWORKING: NETWORKING:
-a, --server-addr Set server address -a, --server-addr Set server address
-n, --no-proxy Do not use system proxy settings -n, --no-proxy Do not use system proxy settings
OVERRIDE SPECIAL:
-b, --birthday-song <song> Change which song is played for birthdays
SPECIAL: SPECIAL:
-b, --birthday-song <song> Change which song is played for birthdays
-v, --birthday-song-volume <volume> Change the volume of the birthday song -v, --birthday-song-volume <volume> Change the volume of the birthday song
"; ";
@ -1822,15 +1825,17 @@ fn multiple_custom_help_headers() {
.help_heading(Some("SPECIAL")) .help_heading(Some("SPECIAL"))
.arg( .arg(
Arg::from("-b, --birthday-song <song> 'Change which song is played for birthdays'") Arg::from("-b, --birthday-song <song> 'Change which song is played for birthdays'")
.help_heading(Some("IGNORE THIS")), .help_heading(Some("OVERRIDE SPECIAL")),
) )
.help_heading(None)
.arg( .arg(
Arg::from( Arg::from("--style <style> 'Choose musical style to play the song'")
"-v --birthday-song-volume <volume> 'Change the volume of the birthday song'", .required(false)
) .help_heading(None),
.help_heading(Some("SPECIAL")),
) )
.arg(Arg::from(
"-v --birthday-song-volume <volume> 'Change the volume of the birthday song'",
))
.help_heading(None)
.arg( .arg(
Arg::new("server-addr") Arg::new("server-addr")
.short('a') .short('a')
@ -1869,8 +1874,10 @@ OPTIONS:
-h, --help Print help information -h, --help Print help information
-V, --version Print version information -V, --version Print version information
OVERRIDE SPECIAL:
-b, --song <song> Change which song is played for birthdays
SPECIAL: SPECIAL:
-b, --song <song> Change which song is played for birthdays
-v, --song-volume <volume> Change the volume of the birthday song -v, --song-volume <volume> Change the volume of the birthday song
"; ";
@ -1891,13 +1898,12 @@ fn custom_help_headers_hidden_args() {
.help_heading(Some("SPECIAL")) .help_heading(Some("SPECIAL"))
.arg( .arg(
Arg::from("-b, --song <song> 'Change which song is played for birthdays'") Arg::from("-b, --song <song> 'Change which song is played for birthdays'")
.help_heading(Some("IGNORE THIS")), .help_heading(Some("OVERRIDE SPECIAL")),
) )
.arg(Arg::from(
"-v --song-volume <volume> 'Change the volume of the birthday song'",
))
.help_heading(None) .help_heading(None)
.arg(
Arg::from("-v --song-volume <volume> 'Change the volume of the birthday song'")
.help_heading(Some("SPECIAL")),
)
.arg( .arg(
Arg::new("server-addr") Arg::new("server-addr")
.short('a') .short('a')