1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

uucore: indent multiline usage info

This commit is contained in:
Daniel Hofstetter 2023-02-20 09:03:57 +01:00 committed by Sylvestre Ledru
parent 3ed26dc454
commit b35c415578

View file

@ -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"
);
}
}