1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

echo: Refactored help message. (#1886)

This commit is contained in:
Alessandro Stoltenberg 2021-03-23 11:55:18 +01:00 committed by GitHub
parent 545fe7d887
commit b54f0b1ff2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,17 +9,17 @@
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use clap::{App, Arg}; use clap::{crate_version, App, Arg};
use std::io::{self, Write}; use std::io::{self, Write};
use std::iter::Peekable; use std::iter::Peekable;
use std::str::Chars; use std::str::Chars;
static VERSION: &str = env!("CARGO_PKG_VERSION"); static NAME: &str = "echo";
const NAME: &str = "echo"; static USAGE: &str = "[OPTIONS]... [STRING]...";
const SYNTAX: &str = "[OPTIONS]... [STRING]..."; static SUMMARY: &str = "display a line of text";
const SUMMARY: &str = "display a line of text"; static AFTER_HELP: &str = r#"
const HELP: &str = r#"
Echo the STRING(s) to standard output. Echo the STRING(s) to standard output.
If -e is in effect, the following sequences are recognized: If -e is in effect, the following sequences are recognized:
\\\\ backslash \\\\ backslash
@ -37,10 +37,10 @@ const HELP: &str = r#"
"#; "#;
mod options { mod options {
pub const STRING: &str = "string"; pub static STRING: &str = "STRING";
pub const NEWLINE: &str = "n"; pub static NO_NEWLINE: &str = "no_newline";
pub const ENABLE_ESCAPE: &str = "e"; pub static ENABLE_BACKSLASH_ESCAPE: &str = "enable_backslash_escape";
pub const DISABLE_ESCAPE: &str = "E"; pub static DISABLE_BACKSLASH_ESCAPE: &str = "disable_backslash_escape";
} }
fn parse_code( fn parse_code(
@ -122,32 +122,43 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
// Final argument must have multiple(true) or the usage string equivalent. // Final argument must have multiple(true) or the usage string equivalent.
.setting(clap::AppSettings::TrailingVarArg) .setting(clap::AppSettings::TrailingVarArg)
.setting(clap::AppSettings::AllowLeadingHyphen) .setting(clap::AppSettings::AllowLeadingHyphen)
.version(VERSION) .version(crate_version!())
.usage(SYNTAX) .usage(USAGE)
.about(SUMMARY) .about(SUMMARY)
.help(HELP) .after_help(AFTER_HELP)
.arg(Arg::with_name(options::STRING).hidden(true).multiple(true))
.arg( .arg(
Arg::with_name(options::NEWLINE) Arg::with_name(options::NO_NEWLINE)
.short("n") .short("n")
.help("do not output the trailing newline"), .help("do not output the trailing newline")
.takes_value(false)
.display_order(1),
) )
.arg( .arg(
Arg::with_name(options::ENABLE_ESCAPE) Arg::with_name(options::ENABLE_BACKSLASH_ESCAPE)
.short("e") .short("e")
.help("enable interpretation of backslash escapes"), .help("enable interpretation of backslash escapes")
.takes_value(false)
.display_order(2),
) )
.arg( .arg(
Arg::with_name(options::DISABLE_ESCAPE) Arg::with_name(options::DISABLE_BACKSLASH_ESCAPE)
.short("E") .short("E")
.help("disable interpretation of backslash escapes (default)"), .help("disable interpretation of backslash escapes (default)")
.takes_value(false)
.display_order(3),
)
.arg(
Arg::with_name(options::STRING)
.hidden(true)
.multiple(true)
.allow_hyphen_values(true),
) )
.get_matches_from(args); .get_matches_from(args);
let no_newline = matches.is_present("n"); let no_newline = matches.is_present(options::NO_NEWLINE);
let escaped = matches.is_present("e"); let escaped = matches.is_present(options::ENABLE_BACKSLASH_ESCAPE);
let values: Vec<String> = match matches.values_of(options::STRING) { let values: Vec<String> = match matches.values_of(options::STRING) {
Some(v) => v.map(|v| v.to_string()).collect(), Some(s) => s.map(|s| s.to_string()).collect(),
None => vec!["".to_string()], None => vec!["".to_string()],
}; };