Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42a11ca

Browse files
authored
Merge pull request #5084 from epage/newline
fix(help): Ensure padding isn't stripped
2 parents 0a87008 + bf3f25e commit 42a11ca

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

clap_builder/src/builder/styled_str.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(not(feature = "usage"), allow(dead_code))]
2+
13
/// Terminal-styling container
24
///
35
/// Styling may be encoded as [ANSI Escape Code](https://en.wikipedia.org/wiki/ANSI_escape_code)
@@ -47,6 +49,19 @@ impl StyledStr {
4749
self.0 = self.0.trim().to_owned()
4850
}
4951

52+
pub(crate) fn trim_start_lines(&mut self) {
53+
if let Some(pos) = self.0.find('\n') {
54+
let (leading, help) = self.0.split_at(pos + 1);
55+
if leading.trim().is_empty() {
56+
self.0 = help.to_owned()
57+
}
58+
}
59+
}
60+
61+
pub(crate) fn trim_end(&mut self) {
62+
self.0 = self.0.trim_end().to_owned()
63+
}
64+
5065
#[cfg(feature = "help")]
5166
pub(crate) fn replace_newline_var(&mut self) {
5267
self.0 = self.0.replace("{n}", "\n");

clap_builder/src/output/help.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ pub(crate) fn write_help(writer: &mut StyledStr, cmd: &Command, usage: &Usage<'_
3030
}
3131
}
3232

33-
// Remove any extra lines caused by book keeping
34-
writer.trim();
33+
// Remove any lines from unused sections
34+
writer.trim_start_lines();
35+
// Remove any whitespace caused by book keeping
36+
writer.trim_end();
3537
// Ensure there is still a trailing newline
3638
writer.push_str("\n");
3739
}

tests/builder/help.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,23 @@ Options:
13331333
utils::assert_output(cmd, "ctest --help", ISSUE_777, false);
13341334
}
13351335

1336+
#[test]
1337+
fn dont_strip_padding_issue_5083() {
1338+
let cmd = Command::new("test")
1339+
.help_template("{subcommands}")
1340+
.subcommands([
1341+
Command::new("one"),
1342+
Command::new("two"),
1343+
Command::new("three"),
1344+
]);
1345+
static EXPECTED: &str = " one
1346+
two
1347+
three
1348+
help Print this message or the help of the given subcommand(s)
1349+
";
1350+
utils::assert_output(cmd, "test --help", EXPECTED, false);
1351+
}
1352+
13361353
static OVERRIDE_HELP_SHORT: &str = "\
13371354
Usage: test
13381355

0 commit comments

Comments
 (0)