fop(1): switch to getopt.rs(3)

This commit is contained in:
Emma Tebibyte 2024-04-11 23:37:04 -06:00
parent bb2c63bfaf
commit ad92fe27d4
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -26,33 +26,43 @@ extern crate getopt;
extern crate strerror;
extern crate sysexits;
use getopt::{ Opt, Parser };
use getopt::GetOpt;
use strerror::StrError;
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
fn main() {
let argv = args().collect::<Vec<String>>();
let mut d = '␞';
let mut arg_parser = Parser::new(&argv, "d:");
let usage = format!(
"Usage: {} [-d delimiter] index command [args...]",
argv[0],
);
let mut index_arg = 0;
while let Some(opt) = arg_parser.next() {
while let Some(opt) = argv.getopt("d:") {
match opt {
Ok(Opt('d', Some(arg))) => {
Ok(o) => {
/* unwrap because Err(OptError::MissingArg) will be returned if
* o.arg is None */
let arg = o.arg.unwrap();
let arg_char = arg.chars().collect::<Vec<char>>();
if arg_char.len() > 1 {
eprintln!("{}: {}: Not a character.", argv[0], arg);
exit(EX_USAGE);
} else { d = arg_char[0]; }
index_arg = o.ind as usize;
},
_ => {},
Err(_) => {
eprintln!("{}", usage);
exit(EX_USAGE);
}
};
}
let index_arg = arg_parser.index();
let command_arg = arg_parser.index() + 1;
let command_arg = index_arg as usize + 1;
argv.get(command_arg).unwrap_or_else(|| {
eprintln!("Usage: {} [-d delimiter] index command [args...]", argv[0]);
eprintln!("{}", usage);
exit(EX_USAGE);
});