A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://github.com/docopt/docopt.nim below:

docopt/docopt.nim: Command line arguments parser that will make you smile (port of docopt to Nim)

docopt creates beautiful command-line interfaces

This is a port of docopt to Nim. Visit docopt.org for more information.

let doc = """
Naval Fate.

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored | --drifting]
  naval_fate (-h | --help)
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.
"""

import strutils
import docopt

let args = docopt(doc, version = "Naval Fate 2.0")

if args["move"]:
  echo "Moving ship $# to ($#, $#) at $# kn".format(
    args["<name>"], args["<x>"], args["<y>"], args["--speed"])
  ships[$args["<name>"]].move(
    parseFloat($args["<x>"]), parseFloat($args["<y>"]),
    speed = parseFloat($args["--speed"]))

if args["new"]: 
  for name in @(args["<name>"]): 
    echo "Creating ship $#" % name 

The option parser is generated based on the docstring above that is passed to docopt function. docopt parses the usage pattern ("Usage: ...") and option descriptions (lines starting with dash "-") and ensures that the program invocation matches the usage pattern; it parses options, arguments and commands based on that. The basic idea is that a good help message has all necessary information in it to make a parser.

proc docopt(doc: string, argv: seq[string] = nil,
            help = true, version: string = nil,
            optionsFirst = false, quit = true): Table[string, Value]

docopt takes 1 required and 5 optional arguments:

If the doc string is invalid, DocoptLanguageError will be raised.

The return value is a Table with options, arguments and commands as keys, spelled exactly like in your help message. Long versions of options are given priority. For example, if you invoke the top example as:

naval_fate ship Guardian move 100 150 --speed=15

the result will be:

{"--drifting": false,     "mine": false,
 "--help": false,         "move": true,
 "--moored": false,       "new": false,
 "--speed": "15",         "remove": false,
 "--version": false,      "set": false,
 "<name>": @["Guardian"], "ship": true,
 "<x>": "100",            "shoot": false,
 "<y>": "150"}

Note that this is not how the values are actually stored, because a Table can hold values of only one type. For that reason, a variant Value type is needed. Value's only accessible member is kind: ValueKind (which shouldn't be needed anyway, because it is known beforehand). ValueKind is one of:

Note that you can use any kind of value in a boolean context and convert any value to string.

Look in the source code to find out more about these conversions.

As of version 0.7.0 docopt also includes a dispatch mechanism for automatically running procedures and converting arguments. This works by a simple macro that inspects the signature of the given procedure. The macro then returns code that will inspect the parsed arguments and if a list of supplied conditions are true the matched arguments from the signature will be extracted from the arguments and converted to the correct type before the procedure is called. A simple example would be something like this (a longer example can be found in the examples folder):

let doc = """
Naval Fate Lite

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate (-h | --help)
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
"""

import strutils
import docopt
import docopt/dispatch
import sequtils

let args = docopt(doc, version = "Naval Fate Lite")

# Define procedures with parameters named the same as the arguments
proc newShip(name: seq[string]) =
  for ship in name:
    echo "Creating ship $#" % ship

proc moveShip(name: string, x, y: int, speed: int) =
  echo "Moving ship $# to ($#, $#) at $# kn".format(
    name, x, y, speed)

if args.dispatchProc(newShip, "ship", "new") or # Runs newShip if "ship" and "new" is set
  args.dispatchProc(moveShip, "ship", "move"): # Runs newShip if "ship" and "move" is set
  echo "Ran something"
else:
  echo doc

See examples folder.

For more examples of docopt language see docopt.py examples.

This library has no dependencies outside the standard library. An impure re library is used.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4