Change Cmd::Run signature to take &clap::Options instead of

`Option<&clap::Options>` - saves 10k
This commit is contained in:
Nathan Fisher 2023-02-04 08:54:27 -05:00
parent 5fd1bb1220
commit 6b30c356dd
53 changed files with 95 additions and 229 deletions

View File

@ -20,10 +20,7 @@ impl Cmd for Base32 {
.args(args()) .args(args())
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "No input").into());
};
let color = match matches.get_one::<String>("color").map(String::as_str) { let color = match matches.get_one::<String>("color").map(String::as_str) {
Some("always") => ColorChoice::Always, Some("always") => ColorChoice::Always,
Some("ansi") => ColorChoice::AlwaysAnsi, Some("ansi") => ColorChoice::AlwaysAnsi,

View File

@ -19,10 +19,7 @@ impl Cmd for Base64 {
.args(args()) .args(args())
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No input")));
};
let color = match matches.get_one::<String>("color").map(String::as_str) { let color = match matches.get_one::<String>("color").map(String::as_str) {
Some("always") => ColorChoice::Always, Some("always") => ColorChoice::Always,
Some("ansi") => ColorChoice::AlwaysAnsi, Some("ansi") => ColorChoice::AlwaysAnsi,

View File

@ -1,6 +1,5 @@
use super::Cmd; use super::Cmd;
use clap::{Arg, ArgMatches, Command}; use clap::{Arg, ArgMatches, Command};
use std::io;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Basename; pub struct Basename;
@ -26,10 +25,7 @@ impl Cmd for Basename {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
if let Some(mut base) = matches if let Some(mut base) = matches
.get_one::<String>("NAME") .get_one::<String>("NAME")
.and_then(|x| x.split('/').last()) .and_then(|x| x.split('/').last())

View File

@ -5,8 +5,7 @@ use clap_complete_nushell::Nushell;
use clap_mangen::Man; use clap_mangen::Man;
use std::{ use std::{
error::Error, error::Error,
fs, fs, io,
io::{self, ErrorKind},
os::unix::fs::symlink, os::unix::fs::symlink,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -152,10 +151,7 @@ impl Cmd for Bootstrap {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(ErrorKind::Other, "No input").into());
};
if let Some(prefix) = matches.get_one::<String>("prefix") { if let Some(prefix) = matches.get_one::<String>("prefix") {
let usr = matches.get_flag("usr"); let usr = matches.get_flag("usr");
if let Some(progpath) = crate::progpath() { if let Some(progpath) = crate::progpath() {

View File

@ -43,10 +43,7 @@ impl Cmd for Chmod {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let feedback = Feedback::from_matches(matches); let feedback = Feedback::from_matches(matches);
let mode = matches let mode = matches
.get_one::<String>("mode") .get_one::<String>("mode")

View File

@ -36,10 +36,7 @@ impl Cmd for Chgrp {
) )
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let recurse = Recurse::from_matches(matches); let recurse = Recurse::from_matches(matches);
let feedback = Feedback::from_matches(matches); let feedback = Feedback::from_matches(matches);
let group = if let Some(grp) = matches.get_one::<String>("group") { let group = if let Some(grp) = matches.get_one::<String>("group") {

View File

@ -38,10 +38,7 @@ impl Cmd for Chown {
) )
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let recurse = Recurse::from_matches(matches); let recurse = Recurse::from_matches(matches);
let feedback = Feedback::from_matches(matches); let feedback = Feedback::from_matches(matches);
let (user, group) = if let Some(who) = matches.get_one::<String>("user") { let (user, group) = if let Some(who) = matches.get_one::<String>("user") {

View File

@ -38,10 +38,7 @@ impl Cmd for Chroot {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let Some(newroot) = matches.get_one::<String>("newroot") else { let Some(newroot) = matches.get_one::<String>("newroot") else {
return Err(io::Error::new(io::ErrorKind::Other, "no new root given").into()); return Err(io::Error::new(io::ErrorKind::Other, "no new root given").into());
}; };

View File

@ -12,7 +12,7 @@ impl Cmd for Clear {
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
print!("\x1b[2J\x1b[H"); print!("\x1b[2J\x1b[H");
print!("\x1b[3J\x1b[H"); print!("\x1b[3J\x1b[H");
Ok(()) Ok(())

View File

@ -63,10 +63,7 @@ impl Cmd for Cut {
) )
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let files: Vec<String> = match matches.get_many::<String>("file") { let files: Vec<String> = match matches.get_many::<String>("file") {
Some(f) => f.cloned().collect(), Some(f) => f.cloned().collect(),
None => vec!["-".to_string()], None => vec!["-".to_string()],

View File

@ -19,27 +19,25 @@ impl Cmd for Dirname {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
if let Some(matches) = matches { if let Some(names) = matches.get_many::<String>("name") {
if let Some(names) = matches.get_many::<String>("name") { names.for_each(|name| {
names.for_each(|name| { let path = match Path::new(name).parent() {
let path = match Path::new(name).parent() { Some(p) => p,
Some(p) => p, None => Path::new("."),
None => Path::new("."), };
}; let path = path.to_string_lossy();
let path = path.to_string_lossy(); let path = if path.is_empty() {
let path = if path.is_empty() { String::from(".")
String::from(".") } else {
} else { path.to_string()
path.to_string() };
}; if matches.get_flag("zero") {
if matches.get_flag("zero") { print!("{path}\0");
print!("{path}\0"); } else {
} else { println!("{path}");
println!("{path}"); }
} });
});
}
} }
Ok(()) Ok(())
} }

View File

@ -19,7 +19,7 @@ impl Cmd for Echo {
]) ])
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let idx = match crate::progname() { let idx = match crate::progname() {
Some(s) if s.as_str() == "echo" => 1, Some(s) if s.as_str() == "echo" => 1,

View File

@ -1,5 +1,3 @@
use std::io;
use super::Cmd; use super::Cmd;
use clap::{Arg, ArgAction, Command, ValueHint}; use clap::{Arg, ArgAction, Command, ValueHint};
@ -40,10 +38,7 @@ impl Cmd for Expand {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
Ok(()) Ok(())
} }

View File

@ -24,17 +24,15 @@ impl Cmd for Factor {
) )
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
if let Some(matches) = matches { match matches.get_many::<u64>("number") {
match matches.get_many::<u64>("number") { Some(numbers) => {
Some(numbers) => { numbers.for_each(|n| print_factors(*n));
numbers.for_each(|n| print_factors(*n)); }
} None => {
None => { for line in io::stdin().lock().lines() {
for line in io::stdin().lock().lines() { for num in line?.split_whitespace() {
for num in line?.split_whitespace() { print_factors(num.parse()?);
print_factors(num.parse()?);
}
} }
} }
} }

View File

@ -13,7 +13,7 @@ impl Cmd for False {
.author("Nathan Fisher") .author("Nathan Fisher")
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
process::exit(1); process::exit(1);
} }

View File

@ -46,10 +46,7 @@ impl Cmd for Fold {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let files = match matches.get_many::<String>("FILE") { let files = match matches.get_many::<String>("FILE") {
Some(c) => c.map(String::to_string).collect(), Some(c) => c.map(String::to_string).collect(),
None => vec!["-".to_string()], None => vec!["-".to_string()],

View File

@ -1,7 +1,6 @@
use super::Cmd; use super::Cmd;
use crate::pw; use crate::pw;
use clap::{Arg, Command}; use clap::{Arg, Command};
use std::io;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Groups; pub struct Groups;
@ -21,10 +20,7 @@ impl Cmd for Groups {
.arg(Arg::new("user")) .arg(Arg::new("user"))
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")))
};
let groups = match matches.get_one::<String>("user") { let groups = match matches.get_one::<String>("user") {
Some(u) => pw::get_group_names_for_name(u)?, Some(u) => pw::get_group_names_for_name(u)?,
None => pw::get_group_names()?, None => pw::get_group_names()?,

View File

@ -5,7 +5,7 @@ use std::{
env, env,
error::Error, error::Error,
fs, fs,
io::{self, stdin, Read, Write}, io::{stdin, Read, Write},
}; };
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
@ -57,7 +57,7 @@ impl Cmd for Head {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let args: Vec<_> = env::args().collect(); let args: Vec<_> = env::args().collect();
let idx = match crate::progname() { let idx = match crate::progname() {
Some(s) if s.as_str() == "head" => 1, Some(s) if s.as_str() == "head" => 1,
@ -80,9 +80,6 @@ impl Cmd for Head {
} }
} }
} }
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "No input").into());
};
if let Some(l) = matches.get_one("LINES") { if let Some(l) = matches.get_one("LINES") {
lines = *l; lines = *l;
} }

View File

@ -12,7 +12,7 @@ impl Cmd for Hostid {
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let hostid = unsafe { libc::gethostid() }; let hostid = unsafe { libc::gethostid() };
let hostid: String = format!("{hostid:x}").chars().skip(8).collect(); let hostid: String = format!("{hostid:x}").chars().skip(8).collect();
println!("{}", hostid); println!("{}", hostid);

View File

@ -22,8 +22,7 @@ impl Cmd for Hostname {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let matches = matches.unwrap();
if let Some(name) = matches.get_one::<String>("NAME") { if let Some(name) = matches.get_one::<String>("NAME") {
unistd::sethostname(name)?; unistd::sethostname(name)?;
Ok(()) Ok(())

View File

@ -19,10 +19,7 @@ impl Cmd for Link {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let Some(f1) = matches.get_one::<String>("file1") else { let Some(f1) = matches.get_one::<String>("file1") else {
return Err(Box::new(io::Error::new( return Err(Box::new(io::Error::new(
io::ErrorKind::Other, io::ErrorKind::Other,

View File

@ -14,7 +14,7 @@ impl Cmd for Logname {
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let logname = unsafe { CStr::from_ptr(libc::getlogin()) }; let logname = unsafe { CStr::from_ptr(libc::getlogin()) };
let logname = logname.to_str()?; let logname = logname.to_str()?;
println!("{logname}"); println!("{logname}");

View File

@ -19,10 +19,7 @@ impl Cmd for Md5sum {
.args([args::check(), args::file()]) .args([args::check(), args::file()])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(files) = matches.get_many::<String>("file") { if let Some(files) = matches.get_many::<String>("file") {
let mut erred = 0; let mut erred = 0;
for f in files { for f in files {

View File

@ -1,7 +1,7 @@
use super::Cmd; use super::Cmd;
use crate::{args, mode::Parser, stat}; use crate::{args, mode::Parser, stat};
use clap::{Arg, ArgMatches, Command}; use clap::{Arg, ArgMatches, Command};
use std::{error::Error, io}; use std::error::Error;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct MkFifo; pub struct MkFifo;
@ -35,10 +35,7 @@ impl Cmd for MkFifo {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let mode = if let Some(m) = matches.get_one::<String>("mode") { let mode = if let Some(m) = matches.get_one::<String>("mode") {
Parser::new(0o666).parse(m)? Parser::new(0o666).parse(m)?
} else { } else {

View File

@ -40,10 +40,7 @@ impl Cmd for MkNod {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let Some(file) = matches.get_one::<String>("file") else { let Some(file) = matches.get_one::<String>("file") else {
return Err(io::Error::new(io::ErrorKind::Other, "no file given").into()); return Err(io::Error::new(io::ErrorKind::Other, "no file given").into());
}; };

View File

@ -36,10 +36,7 @@ impl Cmd for MkTemp {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let mut path = if let Some(t) = matches.get_one::<String>("tmpdir") { let mut path = if let Some(t) = matches.get_one::<String>("tmpdir") {
PathBuf::from(t) PathBuf::from(t)
} else { } else {

View File

@ -67,7 +67,7 @@ pub trait Cmd: fmt::Debug + Sync {
/// Runs the applet /// Runs the applet
/// # Errors /// # Errors
/// Bubbles up any errors to the caller /// Bubbles up any errors to the caller
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>>; fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>>;
/// Returns the path relative to the binary where the link to this applet /// Returns the path relative to the binary where the link to this applet
/// will be installed /// will be installed
fn path(&self) -> Option<crate::Path>; fn path(&self) -> Option<crate::Path>;

View File

@ -55,10 +55,7 @@ impl Cmd for Mountpoint {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(ErrorKind::Other, "no input").into());
};
let file = matches.get_one::<String>("file").unwrap(); let file = matches.get_one::<String>("file").unwrap();
if matches.get_flag("fs-devno") { if matches.get_flag("fs-devno") {
if let Some(maj_min) = fs_devno(file)? { if let Some(maj_min) = fs_devno(file)? {

View File

@ -12,7 +12,7 @@ impl Cmd for Nologin {
.about("Denies a user account login ability") .about("Denies a user account login ability")
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
eprintln!("I'm sorry, I can't let you do that, Dave"); eprintln!("I'm sorry, I can't let you do that, Dave");
process::exit(42); process::exit(42);
} }

View File

@ -1,6 +1,5 @@
use super::Cmd; use super::Cmd;
use clap::{Arg, ArgAction, Command}; use clap::{Arg, ArgAction, Command};
use std::io;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Nproc; pub struct Nproc;
@ -19,10 +18,7 @@ impl Cmd for Nproc {
) )
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
if matches.get_flag("ALL") { if matches.get_flag("ALL") {
println!("{}", unsafe { get_nprocs_conf() }); println!("{}", unsafe { get_nprocs_conf() });
} else { } else {

View File

@ -1,4 +1,4 @@
use std::{env, io}; use std::env;
use super::Cmd; use super::Cmd;
use clap::{Arg, ArgAction, Command}; use clap::{Arg, ArgAction, Command};
@ -25,10 +25,7 @@ impl Cmd for Printenv {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(vars) = matches.get_many::<String>("var") { if let Some(vars) = matches.get_many::<String>("var") {
for var in vars { for var in vars {
let val = env::var(var)?; let val = env::var(var)?;

View File

@ -1,6 +1,6 @@
use super::Cmd; use super::Cmd;
use clap::{Arg, ArgAction, Command}; use clap::{Arg, ArgAction, Command};
use std::{env, io, path::PathBuf}; use std::{env, path::PathBuf};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Pwd; pub struct Pwd;
@ -26,10 +26,7 @@ impl Cmd for Pwd {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let cwd = if matches.get_flag("logical") { let cwd = if matches.get_flag("logical") {
PathBuf::from(env::var("PWD")?) PathBuf::from(env::var("PWD")?)
} else if matches.get_flag("physical") { } else if matches.get_flag("physical") {

View File

@ -1,6 +1,6 @@
use crate::Cmd; use crate::Cmd;
use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint}; use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint};
use std::{fs, io, path::PathBuf}; use std::{fs, path::PathBuf};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Readlink; pub struct Readlink;
@ -31,10 +31,7 @@ impl Cmd for Readlink {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(paths) = matches.get_many::<String>("path") { if let Some(paths) = matches.get_many::<String>("path") {
let paths: Vec<_> = paths.collect(); let paths: Vec<_> = paths.collect();
let len = paths.len(); let len = paths.len();

View File

@ -1,6 +1,6 @@
use super::Cmd; use super::Cmd;
use clap::{Arg, ArgAction, Command, ValueHint}; use clap::{Arg, ArgAction, Command, ValueHint};
use std::{env, fs, io, process}; use std::{env, fs, process};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Realpath; pub struct Realpath;
@ -24,10 +24,7 @@ impl Cmd for Realpath {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(files) = matches.get_many::<String>("path") { if let Some(files) = matches.get_many::<String>("path") {
let mut erred = false; let mut erred = false;
for f in files { for f in files {

View File

@ -3,7 +3,7 @@ use crate::args;
use clap::{Arg, Command}; use clap::{Arg, Command};
use std::{ use std::{
fs::File, fs::File,
io::{self, BufRead, BufReader, ErrorKind, Write}, io::{self, BufRead, BufReader, Write},
}; };
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
@ -25,10 +25,7 @@ impl Cmd for Rev {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(ErrorKind::Other, "No input")));
};
let color = match matches.get_one::<String>("color").map(String::as_str) { let color = match matches.get_one::<String>("color").map(String::as_str) {
Some("always") => ColorChoice::Always, Some("always") => ColorChoice::Always,
Some("ansi") => ColorChoice::AlwaysAnsi, Some("ansi") => ColorChoice::AlwaysAnsi,

View File

@ -62,10 +62,7 @@ impl Cmd for Rm {
) )
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let mut actions = AllActions::from(matches); let mut actions = AllActions::from(matches);
let proceed = match actions.prompt { let proceed = match actions.prompt {
When::Always => true, When::Always => true,

View File

@ -1,7 +1,7 @@
use super::Cmd; use super::Cmd;
use crate::args; use crate::args;
use clap::{Arg, ArgAction, Command, ValueHint}; use clap::{Arg, ArgAction, Command, ValueHint};
use std::{error::Error, fs, io, path::Path}; use std::{error::Error, fs, path::Path};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Rmdir; pub struct Rmdir;
@ -27,10 +27,7 @@ impl Cmd for Rmdir {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
if let Some(directories) = matches.get_many::<String>("dir") { if let Some(directories) = matches.get_many::<String>("dir") {
for dir in directories { for dir in directories {
if matches.get_flag("parents") { if matches.get_flag("parents") {

View File

@ -19,10 +19,7 @@ impl Cmd for Sha1sum {
.args([args::check(), args::file()]) .args([args::check(), args::file()])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(files) = matches.get_many::<String>("file") { if let Some(files) = matches.get_many::<String>("file") {
let mut erred = 0; let mut erred = 0;
for f in files { for f in files {

View File

@ -19,10 +19,7 @@ impl Cmd for Sha224sum {
.args([args::check(), args::file()]) .args([args::check(), args::file()])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(files) = matches.get_many::<String>("file") { if let Some(files) = matches.get_many::<String>("file") {
let mut erred = 0; let mut erred = 0;
for f in files { for f in files {

View File

@ -19,10 +19,7 @@ impl Cmd for Sha256sum {
.args([args::check(), args::file()]) .args([args::check(), args::file()])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(files) = matches.get_many::<String>("file") { if let Some(files) = matches.get_many::<String>("file") {
let mut erred = 0; let mut erred = 0;
for f in files { for f in files {

View File

@ -19,10 +19,7 @@ impl Cmd for Sha384sum {
.args([args::check(), args::file()]) .args([args::check(), args::file()])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(files) = matches.get_many::<String>("file") { if let Some(files) = matches.get_many::<String>("file") {
let mut erred = 0; let mut erred = 0;
for f in files { for f in files {

View File

@ -19,10 +19,7 @@ impl Cmd for Sha512sum {
.args([args::check(), args::file()]) .args([args::check(), args::file()])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
if let Some(files) = matches.get_many::<String>("file") { if let Some(files) = matches.get_many::<String>("file") {
let mut erred = 0; let mut erred = 0;
for f in files { for f in files {

View File

@ -1,10 +1,6 @@
use super::{Cmd, COMMANDS}; use super::{Cmd, COMMANDS};
use clap::Command; use clap::Command;
use std::{ use std::{error::Error, process};
error::Error,
io::{self, ErrorKind},
process,
};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Shitbox; pub struct Shitbox;
@ -33,13 +29,10 @@ impl Cmd for Shitbox {
.subcommands(&subcommands) .subcommands(&subcommands)
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(ErrorKind::Other, "No input")));
};
if let Some((name, matches)) = matches.subcommand() { if let Some((name, matches)) = matches.subcommand() {
if let Some(command) = crate::cmd::get(name) { if let Some(command) = crate::cmd::get(name) {
if let Err(e) = command.run(Some(matches)) { if let Err(e) = command.run(matches) {
eprintln!("Error: {name}: {e}"); eprintln!("Error: {name}: {e}");
process::exit(1); process::exit(1);
} }

View File

@ -27,8 +27,8 @@ impl Cmd for Sleep {
} }
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
if let Some(raw) = matches.unwrap().get_one::<f64>("seconds") { if let Some(raw) = matches.get_one::<f64>("seconds") {
let seconds = *raw as u64; let seconds = *raw as u64;
let nanos = ((raw % 1.0) * 10e-9) as u32; let nanos = ((raw % 1.0) * 10e-9) as u32;
let s = Duration::new(seconds, nanos); let s = Duration::new(seconds, nanos);

View File

@ -1,8 +1,7 @@
use crate::unistd;
use super::Cmd; use super::Cmd;
use crate::unistd;
use clap::{Arg, ArgAction, Command}; use clap::{Arg, ArgAction, Command};
use std::{error::Error, fs::OpenOptions, io}; use std::{error::Error, fs::OpenOptions};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Sync; pub struct Sync;
@ -34,10 +33,7 @@ impl Cmd for Sync {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
if let Some(files) = matches.get_many::<String>("FILE") { if let Some(files) = matches.get_many::<String>("FILE") {
for f in files { for f in files {
let mut opts = OpenOptions::new(); let mut opts = OpenOptions::new();

View File

@ -13,7 +13,7 @@ impl Cmd for True {
.author("Nathan Fisher") .author("Nathan Fisher")
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
process::exit(0); process::exit(0);
} }

View File

@ -1,7 +1,6 @@
use super::Cmd; use super::Cmd;
use crate::{args, unistd}; use crate::{args, unistd};
use clap::{Arg, Command}; use clap::{Arg, Command};
use std::io;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Unlink; pub struct Unlink;
@ -21,10 +20,7 @@ impl Cmd for Unlink {
]) ])
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
if let Some(files) = matches.get_many::<String>("file") { if let Some(files) = matches.get_many::<String>("file") {
for f in files { for f in files {
unistd::unlink(f)?; unistd::unlink(f)?;

View File

@ -6,7 +6,7 @@ use std::{
error::Error, error::Error,
fmt::{self, Write}, fmt::{self, Write},
fs, fs,
io::{self, stdin, Read}, io::{stdin, Read},
ops::{AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign}, ops::{AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign},
str::FromStr, str::FromStr,
}; };
@ -52,10 +52,7 @@ impl Cmd for Wc {
]) ])
} }
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> { fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let mut flags = 0; let mut flags = 0;
for arg in &["LINES", "WORDS", "CHARS", "BYTES", "MAX"] { for arg in &["LINES", "WORDS", "CHARS", "BYTES", "MAX"] {
if matches.get_flag(arg) { if matches.get_flag(arg) {

View File

@ -1,7 +1,7 @@
use super::Cmd; use super::Cmd;
use crate::{bitflags::BitFlags, mode::Bit}; use crate::{bitflags::BitFlags, mode::Bit};
use clap::{Arg, Command}; use clap::{Arg, Command};
use std::{env, fs::File, io, os::unix::prelude::MetadataExt, path::PathBuf, process}; use std::{env, fs::File, os::unix::prelude::MetadataExt, path::PathBuf, process};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Which; pub struct Which;
@ -15,10 +15,7 @@ impl Cmd for Which {
.arg(Arg::new("COMMAND").num_args(1..)) .arg(Arg::new("COMMAND").num_args(1..))
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let rawpath = if let Ok(p) = env::var("PATH") { let rawpath = if let Ok(p) = env::var("PATH") {
p p
} else { } else {
@ -31,7 +28,7 @@ impl Cmd for Which {
if let Some(p) = which(command, &path) { if let Some(p) = which(command, &path) {
println!("{p}"); println!("{p}");
} else { } else {
println!("which: no {} in ({})", command, &rawpath); println!("which: no {command} in ({})", &rawpath);
failures += 1; failures += 1;
} }
} }

View File

@ -15,7 +15,7 @@ impl Cmd for Whoami {
.author("Nathan Fisher") .author("Nathan Fisher")
} }
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let pwnam = unsafe { let pwnam = unsafe {
let pw = *getpwuid(geteuid()); let pw = *getpwuid(geteuid());
CStr::from_ptr((pw).pw_name) CStr::from_ptr((pw).pw_name)

View File

@ -1,6 +1,5 @@
use super::Cmd; use super::Cmd;
use clap::{Arg, Command}; use clap::{Arg, Command};
use std::io;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Yes; pub struct Yes;
@ -13,10 +12,7 @@ impl Cmd for Yes {
.arg(Arg::new("msg").num_args(1).default_value("y")) .arg(Arg::new("msg").num_args(1).default_value("y"))
} }
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> { fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
};
let msg = matches.get_one::<String>("msg").unwrap(); let msg = matches.get_one::<String>("msg").unwrap();
loop { loop {
println!("{msg}"); println!("{msg}");

View File

@ -12,7 +12,6 @@ use {
}; };
pub enum HashType { pub enum HashType {
Blake2b,
Md5, Md5,
Sha1, Sha1,
Sha224, Sha224,
@ -54,7 +53,6 @@ pub fn check_sums(file: &str, hashtype: HashType, erred: &mut usize) -> Result<(
io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(), io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(),
)?; )?;
let s = match hashtype { let s = match hashtype {
HashType::Blake2b => unimplemented!(),
HashType::Md5 => compute_hash(file, Md5::new())?, HashType::Md5 => compute_hash(file, Md5::new())?,
HashType::Sha1 => compute_hash(file, Sha1::new())?, HashType::Sha1 => compute_hash(file, Sha1::new())?,
HashType::Sha224 => compute_hash(file, Sha224::new())?, HashType::Sha224 => compute_hash(file, Sha224::new())?,

View File

@ -68,7 +68,7 @@ pub fn run() {
if let Some(progname) = progname() { if let Some(progname) = progname() {
if let Some(command) = cmd::get(&progname) { if let Some(command) = cmd::get(&progname) {
let cli = command.cli(); let cli = command.cli();
if let Err(e) = command.run(Some(&cli.get_matches())) { if let Err(e) = command.run(&cli.get_matches()) {
eprintln!("{progname}: Error: {e}"); eprintln!("{progname}: Error: {e}");
process::exit(1); process::exit(1);
} }