I'd argue that the 80% use cases for CLI parsing are covered by just a few 
items. We want to know:

  * If our command or subcommand was a given thing.
  * What a positional argument was.
  * What an option with input was, with a default fallback.
  * If a flag was present.



Most of Nim's CLI parsing libraries are gigantic macro-heavy opinionated things 
that are overkill, and don't let you write your own docstrings. The gift that 
is `std/parseopt` allowed me to vastly simplify the basic use cases into 7 tiny 
procs that are barely 50 lines of code.

<https://gist.github.com/nervecenter/9fc10e3ef521cbc296ac8b4c45e6635d>

With this, one can write a simple cascading argument parser and dispatch, like 
so:
    
    
    proc main =
        var cli = init_opt_parser()
        
        if cli.command_was("ship"):
            if cli.subcommand_was("move"):
                ship_move(
                    x = cli.get_option_short("x", "-1").parse_int(),
                    y = cli.get_option_short("y", "-1").parse_int()
                )
            elif cli.subcommand_was("turn"):
                ship_turn(
                    angle =             cli.get_option("angle", "-1", short = 
"a").parse_int(),
                    counterclockwise =  cli.get_flag("counterclockwise", short 
= "c")
                )
        elif cli.command_was("new"):
            make_ship(
                name = cli.get_option("name", "Boaty McBoatface", short = "n")
            )
        elif cli.get_flag("help", short = "h"):
            echo help_string
        elif cli.get_flag("version", short = "v"):
            echo version_string
        else:
            echo usage_string
    
    
    Run

Just thought some people might appreciate it. If it's useful enough to be a 
package, let me know, but I figured it's fine as a code snippet you can just 
download for your own projects.

Reply via email to