mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
feat: move echo to clap (#1884)
This commit is contained in:
parent
a1b50ae0f4
commit
e5ef7486d5
2 changed files with 40 additions and 11 deletions
|
@ -15,6 +15,7 @@ edition = "2018"
|
||||||
path = "src/echo.rs"
|
path = "src/echo.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = "2.33"
|
||||||
uucore = { version=">=0.0.7", package="uucore", path="../../uucore" }
|
uucore = { version=">=0.0.7", package="uucore", path="../../uucore" }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
||||||
|
|
|
@ -9,10 +9,13 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{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");
|
||||||
|
const NAME: &str = "echo";
|
||||||
const SYNTAX: &str = "[OPTIONS]... [STRING]...";
|
const SYNTAX: &str = "[OPTIONS]... [STRING]...";
|
||||||
const SUMMARY: &str = "display a line of text";
|
const SUMMARY: &str = "display a line of text";
|
||||||
const HELP: &str = r#"
|
const HELP: &str = r#"
|
||||||
|
@ -33,6 +36,13 @@ const HELP: &str = r#"
|
||||||
\\xHH byte with hexadecimal value HH (1 to 2 digits)
|
\\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(
|
fn parse_code(
|
||||||
input: &mut Peekable<Chars>,
|
input: &mut Peekable<Chars>,
|
||||||
base: u32,
|
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 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let args = args.collect_str();
|
||||||
|
|
||||||
let matches = app!(SYNTAX, SUMMARY, HELP)
|
let matches = App::new(executable!())
|
||||||
.optflag("n", "", "do not output the trailing newline")
|
.name(NAME)
|
||||||
.optflag("e", "", "enable interpretation of backslash escapes")
|
.version(VERSION)
|
||||||
.optflag(
|
.usage(SYNTAX)
|
||||||
"E",
|
.about(SUMMARY)
|
||||||
"",
|
.help(HELP)
|
||||||
"disable interpretation of backslash escapes (default)",
|
.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 no_newline = matches.is_present("n");
|
||||||
let escaped = matches.opt_present("e");
|
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,
|
Ok(_) => 0,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!("{}", f);
|
show_error!("{}", f);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue