stris(1): modernize code

This commit is contained in:
dtb 2024-08-28 18:50:21 -06:00
parent 1796e772fb
commit 72c3ac5df0
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -23,9 +23,9 @@ use std::{
}; };
extern crate getopt; extern crate getopt;
use getopt::{ Opt, Parser };
extern crate sysexits; extern crate sysexits;
use getopt::GetOpt;
use sysexits::EX_USAGE; use sysexits::EX_USAGE;
struct Reqs { struct Reqs {
@ -34,45 +34,39 @@ struct Reqs {
} }
fn usage(s: &str) -> ExitCode { fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} (-7bcdlu) (-i [inclusions]) [strings...]", s); eprintln!("Usage: {} [-7bcdlu] [-i inclusions] [strings...]", s);
ExitCode::from(EX_USAGE as u8) ExitCode::from(EX_USAGE as u8)
} }
fn main() -> ExitCode { fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
let mut opts = Parser::new(&argv, "7bcdi:lu"); let mut optind = 1;
let mut reqs = Reqs { let mut reqs = Reqs {
ascii: false, blank: false, cntrl: false, digit: false, lower: false, ascii: false, blank: false, cntrl: false, digit: false, lower: false,
upper: false, inuse: false, extra: String::new() upper: false, inuse: false, extra: String::new()
}; };
loop { while let Some(opt) = argv.getopt("7bcdi:lu") {
match opts.next() { match opt.opt() {
None => break, Ok("7") => reqs.ascii = true,
Some(opt) => { Ok("b") => reqs.blank = true,
match opt { Ok("c") => reqs.cntrl = true,
Ok(Opt('7', None)) => reqs.ascii = true, Ok("d") => reqs.digit = true,
Ok(Opt('b', None)) => reqs.blank = true, Ok("i") => reqs.extra = opt.arg().unwrap(),
Ok(Opt('c', None)) => reqs.cntrl = true, Ok("l") => reqs.lower = true,
Ok(Opt('d', None)) => reqs.digit = true, Ok("u") => reqs.upper = true,
Ok(Opt('i', Some(arg))) => reqs.extra = arg, _ => { return usage(&argv[0]); }
Ok(Opt('l', None)) => reqs.lower = true,
Ok(Opt('u', None)) => reqs.upper = true,
_ => { return usage(&argv[0]); }
}
reqs.inuse = true;
}
} }
optind = opt.ind();
reqs.inuse = true;
} }
if argv.len() == opts.index() { if argv.len() == optind { return usage(&argv[0]); }
return usage(&argv[0]);
}
drop(argv); drop(argv);
if reqs.inuse { if reqs.inuse {
for arg in args().skip(opts.index()) { for arg in args().skip(optind) {
for c in arg.chars() { for c in arg.chars() {
if (reqs.ascii && c.is_ascii()) if (reqs.ascii && c.is_ascii())
|| (reqs.blank && c.is_whitespace()) || (reqs.blank && c.is_whitespace())