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

Merge pull request #4393 from cakebaker/uucore_indent_usage_info

uucore: indent multiline usage info
This commit is contained in:
Sylvestre Ledru 2023-03-24 23:13:11 +01:00 committed by GitHub
commit 8e1082458f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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())
}
@ -271,4 +274,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"
);
}
}