From f89a686d3c8b0bcad120e22f1534083ce43ecc0f Mon Sep 17 00:00:00 2001 From: emma Date: Thu, 28 Dec 2023 22:42:05 -0700 Subject: [PATCH] fop(1): made checking argv better --- src/fop.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 68cc06b..1ed31fd 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -28,30 +28,20 @@ use sysexits::{ EX_DATAERR, EX_USAGE }; fn main() { let argv = args().collect::>(); - let usage = format!("Usage: {} index command [args...]", argv[0]); + argv.get(2).unwrap_or_else(|| { + eprintln!("Usage: {} index command [args...]", argv[0]); + exit(EX_USAGE); + }); - let index = match argv.get(1) { - Some(i) => { - i.parse::().unwrap_or_else(|_| { - eprintln!("{}: {}: Not an integer.", argv[0], i); - exit(1); - }) - }, - None => { - eprintln!("{}", usage); - exit(EX_USAGE); - }, - }; + let index = argv[1].parse::().unwrap_or_else(|_| { + eprintln!("{}: {}: Not an integer.", argv[0], argv[1]); + exit(EX_DATAERR); + }); let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); let mut fields = buf.split('␞').collect::>(); - argv.get(2).unwrap_or_else(|| { - eprintln!("{}", usage); - exit(EX_USAGE); - }); - let opts = argv.iter().clone().skip(3).collect::>(); let mut spawned = Command::new(argv.get(2).unwrap())