diff --git a/src/uu/echo/Cargo.toml b/src/uu/echo/Cargo.toml index a8742b68f..7b831fcb8 100644 --- a/src/uu/echo/Cargo.toml +++ b/src/uu/echo/Cargo.toml @@ -15,6 +15,7 @@ edition = "2018" path = "src/echo.rs" [dependencies] +clap = "2.33" uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 93c395391..7dfdf3113 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -9,10 +9,13 @@ #[macro_use] extern crate uucore; +use clap::{App, Arg}; use std::io::{self, Write}; use std::iter::Peekable; use std::str::Chars; +static VERSION: &str = env!("CARGO_PKG_VERSION"); +const NAME: &str = "echo"; const SYNTAX: &str = "[OPTIONS]... [STRING]..."; const SUMMARY: &str = "display a line of text"; const HELP: &str = r#" @@ -33,6 +36,13 @@ const HELP: &str = r#" \\xHH byte with hexadecimal value HH (1 to 2 digits) "#; +mod options { + pub const STRING: &str = "string"; + pub const NEWLINE: &str = "n"; + pub const ENABLE_ESCAPE: &str = "e"; + pub const DISABLE_ESCAPE: &str = "E"; +} + fn parse_code( input: &mut Peekable, base: u32, @@ -105,20 +115,38 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result { pub fn uumain(args: impl uucore::Args) -> i32 { let args = args.collect_str(); - let matches = app!(SYNTAX, SUMMARY, HELP) - .optflag("n", "", "do not output the trailing newline") - .optflag("e", "", "enable interpretation of backslash escapes") - .optflag( - "E", - "", - "disable interpretation of backslash escapes (default)", + let matches = App::new(executable!()) + .name(NAME) + .version(VERSION) + .usage(SYNTAX) + .about(SUMMARY) + .help(HELP) + .arg(Arg::with_name(options::STRING).hidden(true).multiple(true)) + .arg( + Arg::with_name(options::NEWLINE) + .short("n") + .help("do not output the trailing newline"), ) - .parse(args); + .arg( + Arg::with_name(options::ENABLE_ESCAPE) + .short("e") + .help("enable interpretation of backslash escapes"), + ) + .arg( + Arg::with_name(options::DISABLE_ESCAPE) + .short("E") + .help("disable interpretation of backslash escapes (default)"), + ) + .get_matches_from(args); - let no_newline = matches.opt_present("n"); - let escaped = matches.opt_present("e"); + let no_newline = matches.is_present("n"); + let escaped = matches.is_present("e"); + let values: Vec = match matches.values_of(options::STRING) { + Some(v) => v.map(|v| v.to_string()).collect(), + None => vec!["".to_string()], + }; - match execute(no_newline, escaped, matches.free) { + match execute(no_newline, escaped, values) { Ok(_) => 0, Err(f) => { show_error!("{}", f);