From 8dc763f05ebe58487c5569545342cc5a8ee19945 Mon Sep 17 00:00:00 2001 From: DTB Date: Sun, 14 Jul 2024 18:34:08 -0600 Subject: [PATCH] mm(1): treats trailing arguments as optargs for the last opt --- src/mm.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/mm.rs b/src/mm.rs index df431db..f39b3b2 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -1,5 +1,6 @@ /* * Copyright (c) 2024 Emma Tebibyte + * Copyright (c) 2024 DTB * SPDX-License-Identifier: AGPL-3.0-or-later * * This program is free software: you can redistribute it and/or modify it under @@ -32,6 +33,8 @@ use getopt::GetOpt; use strerror::StrError; use sysexits::{ EX_IOERR, EX_USAGE }; +enum ArgMode{ In, Out } + fn main() -> ExitCode { let argv = args().collect::>(); let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]); @@ -42,6 +45,8 @@ fn main() -> ExitCode { let mut u = false; /* unbuffer i/o */ let mut ins = Vec::new(); /* initial input file path vector */ let mut outs = Vec::new(); /* initial output file path vector */ + let mut mode: Option = None; /* mode for positional arguments */ + let mut optind = 0; while let Some(opt) = argv.getopt("aei:o:tu") { match opt.opt() { @@ -49,19 +54,33 @@ fn main() -> ExitCode { Ok("e") => e = true, Ok("u") => u = true, Ok("t") => t = false, - Ok("i") => { /* add input */ + Ok("i") => { /* add inputs */ let input = opt.arg().unwrap(); ins.push(input); + mode = Some(ArgMode::In); }, Ok("o") => { /* add output */ let output = opt.arg().unwrap(); outs.push(output); + mode = Some(ArgMode::Out); }, Err(_) | Ok(_) => { eprintln!("{}", usage); return ExitCode::from(EX_USAGE as u8); }, }; + optind = opt.ind(); + } + + let remaining = argv.iter().skip(optind); + + if let Some(m) = mode { + for arg in remaining { + match m { + ArgMode::In => ins.push(arg.to_string()), + ArgMode::Out => outs.push(arg.to_string()), + }; + } } /* use stdin if no inputs are specified */