• Stars
    star
    117
  • Rank 293,439 (Top 6 %)
  • Language
    Nim
  • License
    MIT License
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Argument parsing for Nim

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:

More Repositories

1

electron-updater-example

A complete example showing how to use `electron-updater`
JavaScript
596
star
2

wiish

Why Is It So Hard (to make a cross-platform app)?
Nim
110
star
3

lhtml

An app for working with local HTML files.
JavaScript
85
star
4

install-nim

Github Action to install Nim
Shell
29
star
5

changer

A small command-line utility for updating changelogs easily.
Nim
26
star
6

grace

Graceful server restart
Python
24
star
7

nimxc

Nim cross-compiler tool
C
17
star
8

nim-embedfs

Nim library to embed directories in your executable
Nim
17
star
9

nim-keyring

Cross-platform access to OS keyring
Nim
16
star
10

nim-intl

Nim library for i18n and l10n
Nim
8
star
11

electron-autoupdate-example

A complete example showing how to use electron's autoUpdater
JavaScript
7
star
12

norm

Asynchronous, cross-database library
Python
6
star
13

scriptfs

Python
5
star
14

lin

Linear build system (in Nim)
Nim
5
star
15

termtools

Nim terminal tools
Nim
5
star
16

nim-stripe

Nim library for using the Stripe API
Nim
4
star
17

parsefin

Tools for parsing OFX/QFX/OFC/csv financial files.
Python
4
star
18

nim-protocols

A protocol library for Nim
Nim
3
star
19

generalconference

Programming-friendly General Conference
Python
3
star
20

nim-totp

TOTP command line tool
Nim
3
star
21

humancrypto

Cryptography for humans
Python
2
star
22

campus

A terminal MUDish server
Python
2
star
23

txcas

CAS server implemented with Twisted
Python
2
star
24

simplebb

A dumb-server, smart-client automated build system
Python
2
star
25

inliner

Make standalone HTML pages
Python
2
star
26

iffy.github.com

HTML
1
star
27

gdax-ts

TypeScript promise-returning interface to GDAX
TypeScript
1
star
28

unidb

Unified DB API based on Twisted and web.py
Python
1
star
29

blog

Script to render code snippets and publish to Blogger
Python
1
star
30

moldspec

Mold specification
Python
1
star
31

eventsts

Simple typed event emitter
TypeScript
1
star
32

boom

Bomberman clone used as tutorial for learning Twisted
Python
1
star
33

sqldiff

Makes SQL needed to turn one schema dump into another.
Python
1
star
34

nim-ohm

A Nim implementation of the Ohm grammar/parser
Nim
1
star
35

nim-sentry

Nim client for Sentry.io
Nim
1
star
36

twistedftw

Twisted tutorials for beginners
Python
1
star
37

gocatgo

cat, written in go
Go
1
star
38

chalkboard

A simple online chalkboard
Python
1
star
39

nim-memtools

Nim
1
star