Registry / serialization / docopt-ng

docopt-ng

JSON →
library0.9.0pypypi✓ verified 28d ago

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.

pip install docopt-ng
INSTALL
IMPORT
SIG · DOCOPT-NG
D
docopt-ng
serializationpythonv0.9.0
Install
1.6s avg
Import
16ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.016s · 17.9MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.012s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

docopt
✓ from docopt import docopt

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)
Debug
Known issues
breakingThe 0.9.0 release introduced breaking changes by removing some 'magic' or implicit behaviors to align more strictly with the original `docopt`'s core parsing scope. Users who relied on non-standard docstring patterns or extensions that were previously handled more permissively might experience unexpected parsing or errors. A fix in this release restored compatibility with `docopt` 0.6.2 docstrings, indicating prior parsing discrepancies.
fix
Review your docstring definitions against the strict `docopt` specification, especially regarding implicit behaviors or non-standard syntax that may have been previously tolerated. Ensure adherence to `docopt`'s explicit usage patterns and option descriptions.
affects: 0.9.0+
gotchaIf both the original `docopt` library and `docopt-ng` are installed in the same Python environment, the original `docopt` package might take precedence during import due to Python's import resolution order. This can lead to unexpected behavior if `docopt-ng`'s specific features or bug fixes are expected.
fix
Ensure that only `docopt-ng` is installed in your environment, or explicitly manage your `PYTHONPATH` to prioritize `docopt-ng` if both must coexist. Preferably, use separate virtual environments.
affects: All
gotcha`docopt-ng` strictly interprets the docstring format. Ambiguities, such as writing `--input ARG` when `--input=ARG` is intended (or vice-versa), or using `-fFILE` which could be interpreted as stacked short options (`-f -i -l`) rather than an option with an argument, can lead to incorrect parsing. Additionally, ensure two spaces separate options from their informal descriptions in the docstring.
fix
Always explicitly define options with arguments using either `--option=<arg>` or `-o <arg>` consistently, and always provide clear option descriptions. Refer to the `docopt` documentation for precise syntax rules for usage patterns and option descriptions.
affects: All
deprecatedPrior to version 0.8.0, the `DocoptExit` exception might not have been reliably accessible directly from the `docopt` module's top level. It was explicitly added to `__all__` in version 0.8.0 for easier and more consistent access.
fix
For versions 0.8.0 and later, `from docopt import DocoptExit` is the correct and reliable way to import the exception. For older versions, direct import might not work and a workaround might be needed, or consider upgrading.
affects: <0.8.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'docopt'
The `docopt-ng` package (or the original `docopt` library) is not installed in the Python environment currently in use, or there is an issue with the Python interpreter's path.
fix
Install `docopt-ng` using pip: `python -m pip install docopt-ng`. Ensure you are running your script with the Python interpreter where the package was installed, especially if you have multiple Python versions.
docopt.DocoptLanguageError: "usage:" (case-insensitive) not found.
The docstring provided to the `docopt` function does not contain a properly formatted 'Usage:' section, which is essential for `docopt-ng` to define the command-line interface.
fix
Ensure your script's docstring (or the string passed to `docopt`) includes a 'Usage:' line followed by your command-line interface patterns. For example: `"""My Program. Usage: my_program [options]"""`.
DocoptExit: Warning: found unmatched (duplicate?) arguments
This warning indicates an inconsistency or ambiguity in the docstring's usage patterns or option descriptions. It can occur if arguments or options are defined in the 'Usage:' section but not consistently described, or due to stricter parsing in `docopt-ng` compared to the original `docopt` library.
fix
Carefully review your docstring's 'Usage:' patterns and 'Options:' descriptions. Ensure all elements are clearly and consistently defined according to `docopt`'s grammar. Pay close attention to how arguments are specified (e.g., `--option ARG` vs `--option=ARG`) and that options are properly listed in the descriptions.
docopt takes precedence over docopt-ng (when both are installed)
If both the original `docopt` library and `docopt-ng` are installed in the same Python environment, the original `docopt` package might be imported due to Python's import resolution order, leading to unexpected behavior or missing `docopt-ng` features.
fix
Preferably, use separate virtual environments to prevent conflicts. If both cannot be avoided, explicitly manage your `PYTHONPATH` to prioritize `docopt-ng`, or ensure only `docopt-ng` is installed: `pip uninstall docopt` then `pip install docopt-ng`.
Upgrade
Version history
0.9.0latest on PyPI · released May 30, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
Resources
docopt-ng — pip install docopt-ng · libregistry