Skip to content

Commit

Permalink
fix(help): Display value terminator and delimiter in help
Browse files Browse the repository at this point in the history
Closes clap-rs#5392 and clap-rs#4812, related to clap-rs#1052
  • Loading branch information
Aethelflaed committed Nov 14, 2024
1 parent b02bc87 commit 68d9934
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
8 changes: 7 additions & 1 deletion clap_builder/src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4647,6 +4647,7 @@ impl Arg {
val_names = vec![val_name; min];
}

let delimiter = self.get_value_delimiter().unwrap_or(' ');
debug_assert!(self.is_takes_value_set());
for (n, val_name) in val_names.iter().enumerate() {
let arg_name = if self.is_positional() && (num_vals.min_values() == 0 || !required) {
Expand All @@ -4656,7 +4657,7 @@ impl Arg {
};

if n != 0 {
rendered.push(' ');
rendered.push(delimiter);
}
rendered.push_str(&arg_name);
}
Expand All @@ -4670,6 +4671,11 @@ impl Arg {
rendered.push_str("...");
}

if let Some(terminator) = self.get_value_terminator() {
rendered.push(' ');
rendered.push_str(terminator);
}

rendered
}

Expand Down
16 changes: 15 additions & 1 deletion clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,10 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
a.default_vals
);

// We might need up to 4 bytes to encode an utf-8 character
let mut buffer = [0u8; 4];
let delimiter = a.get_value_delimiter().unwrap_or(' ').encode_utf8(&mut buffer);

let pvs = a
.default_vals
.iter()
Expand All @@ -778,11 +782,21 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
}
})
.collect::<Vec<_>>()
.join(" ");
.join(delimiter);

spec_vals.push(format!("[default: {pvs}]"));
}

if let Some(delimiter) = a.get_value_delimiter() {
debug!("HelpTemplate::spec_vals: Found delimiter...{delimiter:?}");
spec_vals.push(format!("[value delimiter: {delimiter:?}]"));
}

if let Some(terminator) = a.get_value_terminator() {
debug!("HelpTemplate::spec_vals: Found terminator...{terminator:?}");
spec_vals.push(format!("[value terminator: {terminator:?}]"));
}

let als = a
.aliases
.iter()
Expand Down
18 changes: 9 additions & 9 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,10 +2053,10 @@ fn issue_1052_require_delim_help() {
let expected = str![[r#"
tests stuff
Usage: test --fake <some> <val>
Usage: test --fake <some>:<val>
Options:
-f, --fake <some> <val> some help
-f, --fake <some>:<val> some help [value delimiter: ':']
-h, --help Print help
-V, --version Print version
Expand All @@ -2082,11 +2082,11 @@ fn display_value_terminator() {
let expected = str![[r#"
Likes seeing the value terminator
Usage: test --cmd <cmd>...
Usage: test --cmd <cmd>... ;
Options:
--cmd <cmd>... command to run
-h, --help Print help
--cmd <cmd>... ; command to run [value terminator: ";"]
-h, --help Print help
"#]];
utils::assert_output(cmd, "test --help", expected, false);
Expand Down Expand Up @@ -2118,10 +2118,10 @@ fn custom_headers_headers() {
let expected = str![[r#"
does stuff
Usage: test [OPTIONS] --fake <some> <val>
Usage: test [OPTIONS] --fake <some>:<val>
Options:
-f, --fake <some> <val> some help
-f, --fake <some>:<val> some help [value delimiter: ':']
-h, --help Print help
-V, --version Print version
Expand Down Expand Up @@ -2189,10 +2189,10 @@ fn multiple_custom_help_headers() {
let expected = str![[r#"
does stuff
Usage: test [OPTIONS] --fake <some> <val> --birthday-song <song> --birthday-song-volume <volume>
Usage: test [OPTIONS] --fake <some>:<val> --birthday-song <song> --birthday-song-volume <volume>
Options:
-f, --fake <some> <val> some help
-f, --fake <some>:<val> some help [value delimiter: ':']
--style <style> Choose musical style to play the song
-s, --speed <SPEED> How fast? [possible values: fast, slow]
-h, --help Print help
Expand Down

0 comments on commit 68d9934

Please sign in to comment.