Merge pull request #3305 from epage/help

fix(help): Always respect DisableColoredHelp
This commit is contained in:
Ed Page 2022-01-17 09:35:18 -06:00 committed by GitHub
commit ffe6bff8ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 43 deletions

View file

@ -72,7 +72,7 @@ static BASH: &str = r#"_myapp() {
return 0
;;
myapp__help)
opts=""
opts="<SUBCOMMAND>..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
@ -175,7 +175,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
return 0
;;
my_app__help)
opts=""
opts="<SUBCOMMAND>..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0

View file

@ -72,6 +72,7 @@ _arguments "${_arguments_options[@]}" \
;;
(help)
_arguments "${_arguments_options[@]}" \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
esac
@ -191,6 +192,7 @@ _arguments "${_arguments_options[@]}" \
;;
(help)
_arguments "${_arguments_options[@]}" \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
esac
@ -382,6 +384,7 @@ _arguments "${_arguments_options[@]}" \
'--version[Print version information]' \
'-h[Print help information]' \
'--help[Print help information]' \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
esac
@ -390,6 +393,7 @@ esac
;;
(help)
_arguments "${_arguments_options[@]}" \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
esac

View file

@ -62,6 +62,10 @@ static FIG: &str = r#"const completion: Fig.Spec = {
description: "Print this message or the help of the given subcommand(s)",
options: [
],
args: {
name: "subcommand",
isOptional: true,
},
},
],
options: [
@ -169,6 +173,10 @@ static FIG_SPECIAL_CMDS: &str = r#"const completion: Fig.Spec = {
description: "Print this message or the help of the given subcommand(s)",
options: [
],
args: {
name: "subcommand",
isOptional: true,
},
},
],
options: [
@ -420,6 +428,10 @@ static FIG_SUB_SUBCMDS: &str = r#"const completion: Fig.Spec = {
description: "Print version information",
},
],
args: {
name: "subcommand",
isOptional: true,
},
},
],
options: [
@ -438,6 +450,10 @@ static FIG_SUB_SUBCMDS: &str = r#"const completion: Fig.Spec = {
description: "Print this message or the help of the given subcommand(s)",
options: [
],
args: {
name: "subcommand",
isOptional: true,
},
},
],
options: [

View file

@ -2775,32 +2775,31 @@ impl<'help> App<'help> {
/// Propagate settings
pub(crate) fn _propagate(&mut self) {
macro_rules! propagate_subcmd {
($_self:expr, $sc:expr) => {{
// We have to create a new scope in order to tell rustc the borrow of `sc` is
// done and to recursively call this method
{
if $_self.settings.is_set(AppSettings::PropagateVersion) {
if $sc.version.is_none() && $_self.version.is_some() {
$sc.version = Some($_self.version.unwrap());
}
if $sc.long_version.is_none() && $_self.long_version.is_some() {
$sc.long_version = Some($_self.long_version.unwrap());
}
}
$sc.settings = $sc.settings | $_self.g_settings;
$sc.g_settings = $sc.g_settings | $_self.g_settings;
$sc.term_w = $_self.term_w;
$sc.max_w = $_self.max_w;
}
}};
}
debug!("App::_propagate:{}", self.name);
let mut subcommands = std::mem::take(&mut self.subcommands);
for sc in &mut subcommands {
self._propagate_subcommand(sc);
}
self.subcommands = subcommands;
}
for sc in &mut self.subcommands {
propagate_subcmd!(self, sc);
fn _propagate_subcommand(&self, sc: &mut Self) {
// We have to create a new scope in order to tell rustc the borrow of `sc` is
// done and to recursively call this method
{
if self.settings.is_set(AppSettings::PropagateVersion) {
if sc.version.is_none() && self.version.is_some() {
sc.version = Some(self.version.unwrap());
}
if sc.long_version.is_none() && self.long_version.is_some() {
sc.long_version = Some(self.long_version.unwrap());
}
}
sc.settings = sc.settings | self.g_settings;
sc.g_settings = sc.g_settings | self.g_settings;
sc.term_w = self.term_w;
sc.max_w = self.max_w;
}
}
@ -2903,13 +2902,27 @@ impl<'help> App<'help> {
&& !self.subcommands.iter().any(|s| s.id == Id::help_hash())
{
debug!("App::_check_help_and_version: Building help subcommand");
self.subcommands.push(
App::new("help")
.about("Print this message or the help of the given subcommand(s)")
// The parser acts like this is set, so let's set it so we don't falsely
// advertise it to the user
.setting(AppSettings::DisableHelpFlag),
);
let mut help_subcmd = App::new("help")
.about("Print this message or the help of the given subcommand(s)")
.arg(
Arg::new("subcommand")
.index(1)
.takes_value(true)
.multiple_occurrences(true)
.value_name("SUBCOMMAND")
.help("The subcommand whose help message to display"),
);
self._propagate_subcommand(&mut help_subcmd);
// The parser acts like this is set, so let's set it so we don't falsely
// advertise it to the user
help_subcmd.version = None;
help_subcmd.long_version = None;
help_subcmd = help_subcmd
.setting(AppSettings::DisableHelpFlag)
.unset_setting(AppSettings::PropagateVersion);
self.subcommands.push(help_subcmd);
}
}

View file

@ -665,16 +665,6 @@ impl<'help, 'app> Parser<'help, 'app> {
}
.clone();
if cmd == OsStr::new("help") {
let pb = Arg::new("subcommand")
.index(1)
.takes_value(true)
.multiple_occurrences(true)
.value_name("SUBCOMMAND")
.help("The subcommand whose help message to display");
sc = sc.arg(pb);
}
sc._build();
bin_name.push(' ');
bin_name.push_str(&sc.name);