From b35c415578895d0c60692fcb3340db1aa79f4dde Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Mon, 20 Feb 2023 09:03:57 +0100 Subject: [PATCH] uucore: indent multiline usage info --- src/uucore/src/lib/lib.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 5cbf58faa..38540600d 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -99,10 +99,13 @@ macro_rules! bin { /// Generate the usage string for clap. /// -/// This function replaces all occurrences of `{}` with the execution phrase -/// and returns the resulting `String`. It does **not** support -/// more advanced formatting features such as `{0}`. +/// This function does two things. It indents all but the first line to align +/// the lines because clap adds "Usage: " to the first line. And it replaces +/// all occurrences of `{}` with the execution phrase and returns the resulting +/// `String`. It does **not** support more advanced formatting features such +/// as `{0}`. pub fn format_usage(s: &str) -> String { + let s = s.replace('\n', &format!("\n{}", " ".repeat(7))); s.replace("{}", crate::execution_phrase()) } @@ -273,4 +276,13 @@ mod tests { test_invalid_utf8_args_lossy(os_str); test_invalid_utf8_args_ignore(os_str); } + + #[test] + fn test_format_usage() { + assert_eq!(format_usage("expr EXPRESSION"), "expr EXPRESSION"); + assert_eq!( + format_usage("expr EXPRESSION\nexpr OPTION"), + "expr EXPRESSION\n expr OPTION" + ); + } }