2022-12-20 12:05:21 -05:00
|
|
|
use crate::Path;
|
|
|
|
use clap::{Arg, Command};
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
pub const PATH: Path = Path::Bin;
|
|
|
|
|
2022-12-20 18:35:45 -05:00
|
|
|
#[must_use]
|
2022-12-20 12:05:21 -05:00
|
|
|
pub fn cli() -> Command {
|
|
|
|
Command::new("echo")
|
|
|
|
.about("Display a line of text")
|
|
|
|
.long_about("Echo the STRING(s) to standard output")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.args([
|
|
|
|
Arg::new("inline")
|
|
|
|
.short('n')
|
|
|
|
.help("Do not output a trailing newline"),
|
|
|
|
Arg::new("STRING").num_args(1..),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run() {
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let idx = match crate::progname() {
|
|
|
|
Some(s) if s.as_str() == "echo" => 1,
|
|
|
|
Some(_) => 2,
|
|
|
|
None => unreachable!(),
|
|
|
|
};
|
|
|
|
let len = args.len();
|
|
|
|
let n = len > idx && args[idx] == "-n";
|
|
|
|
let i = if n { idx + 1 } else { idx };
|
|
|
|
for (index, arg) in args.iter().enumerate().skip(i) {
|
|
|
|
if index < len - 1 {
|
2022-12-20 18:35:45 -05:00
|
|
|
print!("{arg} ");
|
2022-12-20 12:05:21 -05:00
|
|
|
} else {
|
2022-12-20 18:35:45 -05:00
|
|
|
print!("{arg}");
|
2022-12-20 12:05:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !n {
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
}
|