diff --git a/src/cmd/base32/mod.rs b/src/cmd/base32/mod.rs index e2bd8bf..97d931a 100644 --- a/src/cmd/base32/mod.rs +++ b/src/cmd/base32/mod.rs @@ -24,11 +24,6 @@ impl Cmd for Base32 { let Some(matches) = matches else { return Err(io::Error::new(io::ErrorKind::Other, "No input").into()); }; - let files: Vec<_> = match matches.get_many::("INPUT") { - Some(c) => c.cloned().collect(), - None => vec![String::from("-")], - }; - let len = files.len(); let color = match matches.get_one::("color").map(String::as_str) { Some("always") => ColorChoice::Always, Some("ansi") => ColorChoice::AlwaysAnsi, @@ -41,31 +36,36 @@ impl Cmd for Base32 { } _ => ColorChoice::Never, }; - for (index, file) in files.into_iter().enumerate() { - if { len > 1 || matches.get_flag("verbose") } && !matches.get_flag("QUIET") { - let mut stdout = StandardStream::stdout(color); - stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; - match index { - 0 => writeln!(stdout, "===> {file} <==="), - _ => writeln!(stdout, "\n===> {file} <==="), - }?; - stdout.reset()?; - } else if index > 0 { - println!(); - } - let contents = get_contents(&file)?; - if matches.get_flag("DECODE") { - decode_base32(contents, matches.get_flag("IGNORE"))?; - } else { - encode_base32( - &contents, - match matches.get_one::("WRAP") { - Some(c) => *c, - None => { - return Err(io::Error::new(io::ErrorKind::Other, "Invalid wrap").into()) - } - }, - ); + if let Some(files) = matches.get_many::("files") { + let (len, _) = files.size_hint(); + for (index, file) in files.into_iter().enumerate() { + if { len > 1 || matches.get_flag("verbose") } && !matches.get_flag("QUIET") { + let mut stdout = StandardStream::stdout(color); + stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; + match index { + 0 => writeln!(stdout, "===> {file} <==="), + _ => writeln!(stdout, "\n===> {file} <==="), + }?; + stdout.reset()?; + } else if index > 0 { + println!(); + } + let contents = get_contents(&file)?; + if matches.get_flag("DECODE") { + decode_base32(contents, matches.get_flag("IGNORE"))?; + } else { + encode_base32( + &contents, + match matches.get_one::("WRAP") { + Some(c) => *c, + None => { + return Err( + io::Error::new(io::ErrorKind::Other, "Invalid wrap").into() + ) + } + }, + ); + } } } Ok(()) @@ -80,7 +80,8 @@ pub fn args() -> [Arg; 7] { [ Arg::new("INPUT") .help("The input file to use") - .num_args(1..), + .num_args(1..) + .default_value("-"), Arg::new("DECODE") .help("Decode rather than encode") .short('d') diff --git a/src/cmd/base64/mod.rs b/src/cmd/base64/mod.rs index 38bcb39..b47b963 100644 --- a/src/cmd/base64/mod.rs +++ b/src/cmd/base64/mod.rs @@ -23,11 +23,6 @@ impl Cmd for Base64 { let Some(matches) = matches else { return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No input"))); }; - let files: Vec<_> = match matches.get_many::("INPUT") { - Some(c) => c.cloned().collect(), - None => vec![String::from("-")], - }; - let len = files.len(); let color = match matches.get_one::("color").map(String::as_str) { Some("always") => ColorChoice::Always, Some("ansi") => ColorChoice::AlwaysAnsi, @@ -40,29 +35,32 @@ impl Cmd for Base64 { } _ => ColorChoice::Never, }; - for (index, file) in files.into_iter().enumerate() { - if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") { - let mut stdout = StandardStream::stdout(color); - stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; - match index { - 0 => writeln!(stdout, "===> {file} <==="), - _ => writeln!(stdout, "\n===> {file} <==="), - }?; - stdout.reset()?; - } else if index > 0 { - println!(); - } - let contents = get_contents(&file)?; - if matches.get_flag("DECODE") { - decode_base64(contents, matches.get_flag("IGNORE"))?; - } else { - encode_base64( - &contents, - match matches.get_one("WRAP") { - Some(c) => *c, - None => 76, - }, - ); + if let Some(files) = matches.get_many::("INPUT") { + let (len, _) = files.size_hint(); + for (index, file) in files.enumerate() { + if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") { + let mut stdout = StandardStream::stdout(color); + stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; + match index { + 0 => writeln!(stdout, "===> {file} <==="), + _ => writeln!(stdout, "\n===> {file} <==="), + }?; + stdout.reset()?; + } else if index > 0 { + println!(); + } + let contents = get_contents(&file)?; + if matches.get_flag("DECODE") { + decode_base64(contents, matches.get_flag("IGNORE"))?; + } else { + encode_base64( + &contents, + match matches.get_one("WRAP") { + Some(c) => *c, + None => 76, + }, + ); + } } } Ok(()) diff --git a/src/cmd/head/mod.rs b/src/cmd/head/mod.rs index 8b61a42..24c1b3e 100644 --- a/src/cmd/head/mod.rs +++ b/src/cmd/head/mod.rs @@ -25,6 +25,7 @@ impl Cmd for Head { .args([ Arg::new("FILES") .help("The input file to use") + .default_value("-") .num_args(1..), Arg::new("BYTES") .help("Count bytes instead of lines") @@ -44,7 +45,10 @@ impl Cmd for Head { .allow_negative_numbers(false) .conflicts_with("num") .value_parser(value_parser!(usize)), - args::color(), + Arg::new("color") + .short('C') + .long("color") + .value_parser(["always", "ansi", "auto", "never"]), Arg::new("num") .short('1') .short_aliases(['2', '3', '4', '5', '6', '7', '8', '9']) @@ -82,12 +86,6 @@ impl Cmd for Head { if let Some(l) = matches.get_one("LINES") { lines = *l; } - let files = match matches.get_many::("FILES") { - Some(c) => c.map(std::string::ToString::to_string).collect(), - None => vec!["-".to_string()], - }; - let header = - !matches.get_flag("QUIET") && { files.len() > 1 || matches.get_flag("HEADER") }; let color = match matches.get_one::("color").map(String::as_str) { Some("always") => ColorChoice::Always, Some("ansi") => ColorChoice::AlwaysAnsi, @@ -100,11 +98,15 @@ impl Cmd for Head { } _ => ColorChoice::Never, }; - for (index, file) in files.into_iter().enumerate() { - if index == 1 && header { - println!(); + if let Some(files) = matches.get_many::("FILES") { + let (len, _) = files.size_hint(); + let header = !matches.get_flag("QUIET") && { len > 1 || matches.get_flag("HEADER") }; + for (index, file) in files.enumerate() { + if index == 1 && header { + println!(); + } + head(file, lines, header, matches.get_flag("BYTES"), color)?; } - head(&file, lines, header, matches.get_flag("BYTES"), color)?; } Ok(()) } diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 67e091a..0eb3467 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -16,6 +16,7 @@ mod date; mod dd; mod dirname; mod echo; +mod expand; mod factor; mod r#false; mod fold; diff --git a/src/cmd/rev/mod.rs b/src/cmd/rev/mod.rs index 2b94b7d..b95585c 100644 --- a/src/cmd/rev/mod.rs +++ b/src/cmd/rev/mod.rs @@ -20,7 +20,8 @@ impl Cmd for Rev { args::color(), Arg::new("file") .help("if file is '-' read from stdin") - .num_args(0..), + .num_args(1..) + .default_value("-"), ]) } @@ -28,10 +29,6 @@ impl Cmd for Rev { let Some(matches) = matches else { return Err(Box::new(io::Error::new(ErrorKind::Other, "No input"))); }; - let files: Vec<_> = match matches.get_many::("file") { - Some(c) => c.cloned().collect(), - None => vec![String::from("-")], - }; let color = match matches.get_one::("color").map(String::as_str) { Some("always") => ColorChoice::Always, Some("ansi") => ColorChoice::AlwaysAnsi, @@ -44,17 +41,19 @@ impl Cmd for Rev { } _ => ColorChoice::Never, }; - for (index, file) in files.into_iter().enumerate() { - if matches.get_flag("header") { - let mut stdout = StandardStream::stdout(color); - stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; - match index { - 0 => writeln!(stdout, "===> {file} <==="), - _ => writeln!(stdout, "\n===> {file} <==="), - }?; - stdout.reset()?; + if let Some(files) = matches.get_many::("file") { + for (index, file) in files.enumerate() { + if matches.get_flag("HEADER") { + let mut stdout = StandardStream::stdout(color); + stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; + match index { + 0 => writeln!(stdout, "===> {file} <==="), + _ => writeln!(stdout, "\n===> {file} <==="), + }?; + stdout.reset()?; + } + rev_file(&file)?; } - rev_file(&file)?; } Ok(()) }