docopt-ng

0.9.0 · active · verified Sun Apr 12

docopt-ng is a Jazzband-maintained fork of the original docopt library, providing a humane command-line arguments parser for Python 3.7+. It generates command-line interfaces by parsing a script's docstring, which defines usage patterns and option descriptions. The library aims to offer maintenance, type hints, and complete test coverage. While actively maintained, its release cadence can be irregular, with the latest major release (0.9.0) occurring almost a year ago as of verification.

Warnings

Install

Imports

Quickstart

Define your command-line interface directly in a docstring, then pass it to the `docopt` function. The function returns a dictionary of parsed arguments. This example demonstrates a simulated command-line input for a 'Naval Fate' application, parsing commands, arguments, and options.

import os
from docopt import docopt

DOC = """Naval Fate.

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

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

# Simulate command line arguments for demonstration
# Replace with sys.argv[1:] in a real application
argv_simulate = os.environ.get('DOCOPT_NG_ARGV_SIMULATE', 'ship Guardian move 100 150 --speed=15').split(' ')

if __name__ == '__main__':
    arguments = docopt(DOC, argv=argv_simulate, version='Naval Fate 2.0')
    print(arguments)

view raw JSON →