Skip to content

Commit 4c00d3d

Browse files
committed
Quote empty default values in help output
Fixes #4976
1 parent 68b5ff9 commit 4c00d3d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

clap_builder/src/output/help_template.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl HelpTemplate<'_, '_> {
796796
.iter()
797797
.map(|dv| dv.to_string_lossy())
798798
.map(|dv| {
799-
if dv.contains(char::is_whitespace) {
799+
if dv.is_empty() || dv.contains(char::is_whitespace) {
800800
Cow::from(format!("{dv:?}"))
801801
} else {
802802
dv

tests/derive/default_value.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@ Options:
8282
"#]].raw());
8383
}
8484

85+
#[test]
86+
fn auto_default_value_t_string() {
87+
#[derive(Parser, PartialEq, Debug)]
88+
struct Opt {
89+
#[arg(long, default_value_t)]
90+
arg: String,
91+
}
92+
assert_eq!(
93+
Opt {
94+
arg: String::default()
95+
},
96+
Opt::try_parse_from(["test"]).unwrap()
97+
);
98+
assert_eq!(
99+
Opt {
100+
arg: "value".to_string()
101+
},
102+
Opt::try_parse_from(["test", "--arg", "value"]).unwrap()
103+
);
104+
105+
let help = utils::get_long_help::<Opt>();
106+
assert_data_eq!(help, str![[r#"
107+
Usage: clap [OPTIONS]
108+
109+
Options:
110+
--arg <ARG>
111+
[default: ""]
112+
113+
-h, --help
114+
Print help
115+
116+
"#]].raw());
117+
}
118+
85119
#[test]
86120
fn default_values_t() {
87121
#[derive(Parser, PartialEq, Debug)]

0 commit comments

Comments
 (0)