feat: move echo to clap (#1884)

This commit is contained in:
Yagiz Degirmenci 2021-03-23 00:42:14 +03:00 committed by GitHub
parent a1b50ae0f4
commit e5ef7486d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 11 deletions

View file

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

View file

@ -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<Chars>,
base: u32,
@ -105,20 +115,38 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> {
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<String> = 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);