Skip to content

Commit

Permalink
fix: Don't suggest values for boolean options
Browse files Browse the repository at this point in the history
The man generation would generate suggestions for all options of
values they should take, even if the argument did not take values.
This adds a simple check to make sure only options that take values
offer a suggestion

fixes: clap-rs#4443
  • Loading branch information
Calder-Ty committed Nov 23, 2022
1 parent f5dcfc5 commit 65e6f54
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
8 changes: 5 additions & 3 deletions clap_mangen/src/render.rs
Expand Up @@ -99,9 +99,11 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
(None, None) => vec![],
};

if let Some(value) = &opt.get_value_names() {
header.push(roman("="));
header.push(italic(&value.join(" ")));
if opt.get_action().takes_values() {
if let Some(value) = &opt.get_value_names() {
header.push(roman("="));
header.push(italic(&value.join(" ")));
}
}

if let Some(defs) = option_default_values(opt) {
Expand Down
17 changes: 17 additions & 0 deletions clap_mangen/tests/common.rs
Expand Up @@ -312,3 +312,20 @@ pub fn possible_values_command(name: &'static str) -> clap::Command {
]),
)
}


/// Checks to make sure boolean valued "Flag options" do not generate
/// suggestions for a parameter. i.e:
/// --boolean_flag=BOOLEAN_FLAG
///
/// This is both confusing and suggest erroneous behavior as clap will fail if you
/// pass a value to a boolean flag
pub fn flag_without_value(name: &'static str) -> clap::Command {
clap::Command::new(name)
.arg(
clap::Arg::new("is_bool")
.long("is_bool")
.action(clap::ArgAction::SetTrue)
)
}

7 changes: 7 additions & 0 deletions clap_mangen/tests/roff.rs
Expand Up @@ -70,6 +70,13 @@ fn possible_values() {
common::assert_matches_path("tests/snapshots/possible_values.bash.roff", cmd);
}

#[test]
fn flag_without_value() {
let name = "my-app";
let cmd = common::flag_without_value(name);
common::assert_matches_path("tests/snapshots/flag_without_value.bash.roff", cmd);
}

#[test]
fn sub_subcommands_help() {
let name = "my-app";
Expand Down
15 changes: 15 additions & 0 deletions clap_mangen/tests/snapshots/flag_without_value.bash.roff
@@ -0,0 +1,15 @@
.ie /n(.g .ds Aq /(aq
.el .ds Aq '
.TH my-app 1 "my-app "
.SH NAME
my/-app
.SH SYNOPSIS
/fBmy/-app/fR [/fB/-/-is_bool/fR] [/fB/-h/fR|/fB/-/-help/fR]
.SH DESCRIPTION
.SH OPTIONS
.TP
/fB/-/-is_bool/fR

.TP
/fB/-h/fR, /fB/-/-help/fR
Print help information

0 comments on commit 65e6f54

Please sign in to comment.