Skip to content

Commit

Permalink
Merge pull request #3305 from epage/help
Browse files Browse the repository at this point in the history
fix(help): Always respect DisableColoredHelp
  • Loading branch information
epage authored Jan 17, 2022
2 parents 3620cad + 4a43b51 commit ffe6bff
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 42 deletions.
4 changes: 2 additions & 2 deletions clap_complete/tests/completions/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clap_complete/tests/completions/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ _arguments "${_arguments_options[@]}" \
;;
(help)
_arguments "${_arguments_options[@]}" \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
esac
Expand Down Expand Up @@ -191,6 +192,7 @@ _arguments "${_arguments_options[@]}" \
;;
(help)
_arguments "${_arguments_options[@]}" \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
esac
Expand Down Expand Up @@ -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
Expand All @@ -390,6 +393,7 @@ esac
;;
(help)
_arguments "${_arguments_options[@]}" \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
esac
Expand Down
16 changes: 16 additions & 0 deletions clap_complete_fig/tests/completions/fig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -420,6 +428,10 @@ static FIG_SUB_SUBCMDS: &str = r#"const completion: Fig.Spec = {
description: "Print version information",
},
],
args: {
name: "subcommand",
isOptional: true,
},
},
],
options: [
Expand All @@ -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: [
Expand Down
73 changes: 43 additions & 30 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

debug!("App::_propagate:{}", self.name);
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());
}
}

for sc in &mut self.subcommands {
propagate_subcmd!(self, sc);
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;
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ffe6bff

Please sign in to comment.