Skip to content

Commit

Permalink
fix(complete): Handle help completions
Browse files Browse the repository at this point in the history
The main motivation was to reduce special cases by putting all of the
logic in one place.
  • Loading branch information
epage committed Jan 17, 2022
1 parent e962ad8 commit 4a43b51
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 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
12 changes: 10 additions & 2 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2902,8 +2902,16 @@ impl<'help> App<'help> {
&& !self.subcommands.iter().any(|s| s.id == Id::help_hash())
{
debug!("App::_check_help_and_version: Building help subcommand");
let mut help_subcmd =
App::new("help").about("Print this message or the help of the given subcommand(s)");
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
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 4a43b51

Please sign in to comment.