#decided to skip using clap

21 messages · Page 1 of 1 (latest)

viscid lagoon
#

what do yall think of this?

use std::path::PathBuf;
use std::process::exit as rexit;

pub fn main() {
    let mut args = std::env::args().peekable();
     if let Some(path) = args.peek() {
        if PathBuf::from(path).exists() {
            let _ = args.next();
        }
    }

    while let Some(mut arg) = args.next() {
        match (arg.chars().nth(0), arg.chars().nth(1)) {
            (Some('-'), Some('-')) => {
                let _ = arg.remove(0);
                let _ = arg.remove(0);
                match arg.as_str() {
                    "help" => {
                        print_help();
                        if args.peek() == Some(&String::from("more_help")) {
                            print_more_help();
                        }
                        rexit(0);
                    }
                    arg => {
                        eprintln!("Invalid long argument: {arg}");
                        rexit(1);
                    }
                }
            }
            (Some('-'), Some(_)) => {
                let _ = arg.remove(0);
                for char in arg.chars() {
                    match char {
                        'h' => {
                            print_help();
                            if args.peek() == Some(&String::from("more_help")) {
                                print_more_help();
                            }
                            rexit(0);
                        }
                        arg => {
                            eprintln!("Invalid short argument: {arg}");
                            rexit(1);
                        }
                    }
                }
            }
            _ => {
                // handle anything not consumed by a flag
                eprintln!("Invalid argument: {arg}");
                rexit(1);
            }
        }
    }
}
fleet musk
#

Imagine you had 10 projects for 10 clients and they all had about cli 20 flags, this would make you very tired very quickly

viscid lagoon
#

yea but what i write in clap is more complicated and restrictive

#

especially args that take multiple values of different types

#

plus thats without any builders/macros im going to end up making

fleet musk
#

I agree clap (there must be other popular alternative) is opinionated but really imagine having 10 different clients and they all want a cli app. To keep it DRY you'd end up with a custom cli framework that maybe would look like clap.

viscid lagoon
#

dry?

fleet musk
#

DRY (Don't repeat yourself) another way of saying reusable code

viscid lagoon
#

oh

#

yea i dont really go overboard with that stuff

#

i copy it from an old project and upgrade/fix it as needed

#

plus, if you could see the mess i had to make to get clap working you would agree this way is better

#

clap works well for simple cli setups, but anything even the slightest bit unconventional and it fails to do much of anything without significant tweaks or drawbacks

fleet musk
#

well the biggest concern really is scalability as the code grows. because eventually if it gets to 1000 lines of code - at that point copy/paste won't be feasible. but before that sure it is more than doable

viscid lagoon
#

think about how clap would be crippled by something of that scale, the code would be so much worse with it

fleet musk
viscid lagoon
#

oh you thought i was talking about derive?

#

derive is even more restrictive in what types can be used

#

but i was comparing it directly to clap builder

hasty grove
#

or, you could ask how to solve a problem u have with clap

heavy merlin