Command line argument parsing library. It generates the parser at compile time so that parsed options have a well-defined type.
After defining your expected arguments with newParser(...), use:
run(...)to parse and execute anyrun:blocks you've defined. This will automatically display help text when-h/--helpis used.parse(...)to parse without executing.
Both procs will parse the process' command line if no arguments are given.
import argparse
var p = newParser("My Program"):
flag("-a", "--apple")
flag("-b", help="Show a banana")
option("-o", "--output", help="Output to this file")
command("somecommand"):
arg("name")
arg("others", nargs = -1)
run:
echo opts.name
echo opts.others
echo opts.parentOpts.apple
echo opts.parentOpts.b
echo opts.parentOpts.output
p.run(@["--apple", "-o=foo", "somecommand", "myname", "thing1", "thing2"])import argparse
var p = newParser("My Program"):
flag("-a", "--apple")
flag("-b", help="Show a banana")
option("-o", "--output", help="Output to this file")
arg("name")
arg("others", nargs = -1)
var opts = p.parse(@["--apple", "-o=foo", "hi"])
assert opts.apple == true
assert opts.b == false
assert opts.output == "foo"
assert opts.name == "hi"
assert opts.others == @[]- --long-flags
- --arguments=withvalues
- help argument
- arguments
- variable args
- sub commands (git-style)
- sub commands access parent opts
- render docs
- --help special case
- --version
- default values
- raise exception on invalid args
- Handle
--arg val --nother-arg val2(spaces instead of=or:between key and value) - Access to object type names (so you can do
handleOpts(opts: TheGeneratedType) = ...) - Make it so you don't have to use a wrapping macro
- parse strings into sequences (shlex-like)
- fail on unknown arguments
- let options have a list of acceptable values (choices)
Run tests with:
nimble test
Run a single test with:
nim c -r tests/test1.nim -g "somepatternmatchingthetestname"
When you publish a new version, publish the docs by running ./builddocs.sh then pushing master.