use std::{ error, env, };
use arg_kit::*;

static HELP_TEXT: &str = "\
Usage:
    -h  --help          Print this message and exit
    -s  --set INT       Set variable to INT
    -d  --diff A B      Set variable to A - B
    -p  --print         Print the variable
";


fn main() -> Result<(), Box<dyn error::Error>> {
    let mut argv = env::args();
    argv.next();  // Skip argv[0]

    let mut var: isize = 0;
    let mut to_print: Vec<isize> = Vec::new();

    // Following few lines are equivalent to:
    /*
    while let Some(args) = argv.next() {
        for arg in args.as_arg() {
            match arg {
                Argument::Short("h") | Argument::Long("help") => ...
    */
    for_args!(argv; {
        arg!(-h | --help) => {
            eprintln!("{HELP_TEXT}");
            return Ok(());
        },
        arg!(-s | --set) => {
            var = argv.next().ok_or("Expected positional argument INT")?.parse()?;
        },
        arg!(-d | --diff) => {
            let a: isize = argv.next().ok_or("Expected positional argument A")?.parse()?;
            let b: isize = argv.next().ok_or("Expected positional argument B")?.parse()?;
            var = a - b;
        },
        arg!(-p | --print) => {
            to_print.push(var);
        }
        unknown => panic!("Unknown argument {unknown}"),
    });

    to_print.into_iter().for_each(|v| println!("{v}"));

    Ok(())
}
