misc

Misc scripts and tools
git clone git://git.alexwennerberg.com/misc
Log | Files | Refs | README | LICENSE

commit 5b67e0fa53c80df0d274624cc34338b687b5ad3b
parent 9ae89c001120d34ac53409315f7a4576c321bec2
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Fri, 24 Dec 2021 10:09:02 -0800

add parsenext closure

Diffstat:
Marg.rs | 32+++++++++++---------------------
1 file changed, 11 insertions(+), 21 deletions(-)

diff --git a/arg.rs b/arg.rs @@ -29,34 +29,24 @@ fn usage() -> ! { fn main() { // Initialize your variables here; - let mut a_enabled = false; - let mut b_enabled = false; + let mut flags = String::new(); let mut positional: Option<String> = None; let mut var: Option<i32> = None; let mut args = env::args().skip(1); + let parsenext = + |a: Option<String>| Some(a.and_then(|a| a.parse().ok()).unwrap_or_else(|| usage())); + while let Some(arg) = args.next() { - let mut iter = arg.chars(); - if iter.next() != Some('-') { + let mut chars = arg.chars(); + if chars.next() != Some('-') { positional = Some(arg); continue; } - iter.for_each(|m| match m { - 'c' => { - // I think this is the least worst way to do this - var = Some( - args.next() - .and_then(|a| a.parse().ok()) - .unwrap_or_else(|| usage()), - ); - } - 'a' => { - a_enabled = true; - } - 'b' => { - b_enabled = true; - } + chars.for_each(|m| match m { + 'c' => var = parsenext(args.next()), + 'a' | 'b' => flags.push(m), _ => { usage(); } @@ -66,10 +56,10 @@ fn main() { // Check mandatory arguments // Check values of variables, use to direct behavior - if a_enabled { + if flags.contains('a') { println!("a enabled"); } - if b_enabled { + if flags.contains('b') { println!("b enabled"); } if let Some(v) = var {