No description
Find a file
2024-08-16 14:56:45 -04:00
.github/workflows Use macos-13 2024-04-29 10:08:39 -04:00
changes Bump to v4.0.2 2024-08-16 14:56:45 -04:00
docs Rebuild docs 2023-03-15 09:20:46 -04:00
src casting to nimcall function types 2024-08-16 17:10:04 +08:00
tests You can now use hyphens in both command("...") and arg("...") names. (#83) 2022-11-17 14:03:12 -05:00
.gitignore Initial broken commit 2018-10-09 09:48:50 -06:00
argparse.nimble Bump to v4.0.2 2024-08-16 14:56:45 -04:00
builddocs.sh Update docs 2020-11-23 10:06:58 -05:00
CHANGELOG.md Bump to v4.0.2 2024-08-16 14:56:45 -04:00
LICENSE.md Rename LICENSE 2018-10-30 16:30:05 -06:00
README.md Update README 2022-11-11 08:38:56 -05:00

argparse

tests

Docs

Command line argument parsing library. It generates the parser at compile time so that parsed options have a well-defined type.

Example

After defining your expected arguments with newParser(...), use:

  1. run(...) to parse and execute any run: blocks you've defined. This will automatically display help text when -h/--help is used.
  2. parse(...) to parse without executing, giving you more control over what happens.

Both procs will parse the process' command line if no arguments are given.

See the docs for more info

run()

import argparse

var p = newParser:
  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
      echo opts.parentOpts.output_opt.get()

try:
  p.run(@["--apple", "-o=foo", "somecommand", "myname", "thing1", "thing2"])
except UsageError as e:
  stderr.writeLine getCurrentExceptionMsg()
  quit(1)

parse()

import argparse

var p = newParser:
  flag("-a", "--apple")
  flag("-b", help="Show a banana")
  option("-o", "--output", help="Output to this file")
  arg("name")
  arg("others", nargs = -1)

try:
  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 == @[]
except ShortCircuit as err:
  if err.flag == "argparse_help":
    echo err.help
    quit(1)
except UsageError:
  stderr.writeLine getCurrentExceptionMsg()
  quit(1)

Alternatives

If argparse doesn't suit your needs, consider these alternatives: