When I first came to `nim` I was looking for a faster, type-safe alternative to
python and it was love at first sight. But then I rapidly came unstuck trying
to write anything in it, since I found `parseopt` lead to spaghetti code and
there was no `argparse` available. Years later (after I left nim behind for a
while), there's [nim-argparse](https://github.com/iffy/nim-argparse) and
[docopt.nim](https://github.com/docopt/docopt.nim) but they weren't quite was I
was looking for, and I still had a soft spot for parsing as a project. So,
without further ado, I bring you
[therapist](https://maxgrenderjones.bitbucket.io/therapist/therapist.html), a
new command line parser.
A simple hello world example:
import therapist
# The parser is specified as a tuple
let spec = (
# Name is a positional argument, by virtue of being surrounded by < and
>
name: newStringArg(@["<name>"], help="Person to greet"),
# --times is an optional argument, by virtue of starting with - and/or
--
times: newIntArg(@["-t", "--times"], default=1, help="How many times to
greet"),
# --version will cause 0.1.0 to be printed
version: newMessageArg(@["--version"], "0.1.0", help="Prints version"),
# --help will cause a help message to be printed
help: newHelpArg(@["-h", "--help"], help="Show help message"),
)
# `args` and `command` would normally be picked up from the commandline
spec.parseOrQuit(prolog="Greeter", args="-t 2 World", command="hello")
# If a help message or version was requested or a parse error generated it
would be printed
# and then the parser would call `quit`. Getting past `parseOrQuit` implies
we're ok.
for i in 1..spec.times.value:
echo "Hello " & spec.name.value
doAssert spec.name.seen
doAssert spec.name.value == "World"
doAssert spec.times.seen
doAssert spec.times.value == 2
Run
Since this is my first library for nim, feedback (and _that 's not how you do
it, try this_) welcome!