From ad92fe27d4eb2a6731182844918713ee93acc292 Mon Sep 17 00:00:00 2001 From: emma Date: Thu, 11 Apr 2024 23:37:04 -0600 Subject: [PATCH] fop(1): switch to getopt.rs(3) --- src/fop.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index b829602..a56a0a6 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -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::>(); 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::>(); 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); });