diff --git a/src/intcmp.rs b/src/intcmp.rs index 88be72c..f504b50 100644 --- a/src/intcmp.rs +++ b/src/intcmp.rs @@ -34,45 +34,45 @@ fn usage(s: &str) -> ExitCode { fn main() -> ExitCode { let argv = args().collect::>(); - let mut can_eq = false; - let mut can_gt = false; - let mut can_lt = false; + let mut e = false; /* args can be == */ + let mut g = false; /* args can be > */ + let mut l = false; /* args can be < */ let mut optind = 0; if argv.len() < 3 { return usage(&argv[0]); } while let Some(opt) = argv.getopt("egl") { match opt.opt() { - Ok("e") => can_eq = true, - Ok("g") => can_gt = true, - Ok("l") => can_lt = true, + Ok("e") => e = true, + Ok("g") => g = true, + Ok("l") => l = true, _ => { return usage(&argv[0]); }, } optind = opt.ind(); } - if argv.len() - optind < 2 /* ref cmp */ { return usage(&argv[0]); } + if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); } - let mut reference: Option = None; - let mut cmpn: usize; + let mut prev: Option = None; /* no previous operand */ + let mut currn: usize; - for arg in argv.iter().skip(optind) { - match arg.parse::() { - Ok(n) => cmpn = n, + for arg in argv.iter().skip(optind) { /* iterate operands */ + match arg.parse::() { /* parse current operand */ + Ok(n) => currn = n, _ => { eprintln!("{}: {}: Invalid integer", &argv[0], arg); return ExitCode::from(EX_USAGE as u8); } } - if let Some(refn) = reference { - if (!can_eq && refn == cmpn) - || (!can_gt && refn > cmpn) - || (!can_lt && refn < cmpn) + if let Some(prevn) = prev { /* if there was a previous opr., test */ + if (!e && prevn == currn) + || (!g && prevn > currn) + || (!l && prevn < currn) { return ExitCode::FAILURE; } } - reference = Some(cmpn); + prev = Some(currn); /* there is a previous operand */ } ExitCode::SUCCESS