Skip to main content

A lightweight helper around docopt-ng adding automatic type conversion

Project description

docopt-typed

docopt-typed is a lightweight helper around docopt-ng that keeps the ergonomics of docstrings-as-interfaces while adding automatic type conversion. Annotate your usage text with the familiar <type> markers and receive a ParsedOptions object whose values already match those types.

Features

  • Works as a drop-in replacement for docopt while returning the same ParsedOptions object
  • Converts values to Python types by reading <type> (or <name:type>) hints in the usage text
  • Supports common primitives (int, float, bool, str, path) plus JSON-backed structures, complex numbers, and none/null
  • Keeps raw strings when a conversion fails so you can decide how to handle edge cases
  • Treats option and positional argument annotations the same, so flags and required arguments can both be typed

Installation

pip install docopt-typed

or

uv add docopt-typed

Usage

Import docopt_typed instead of docopt and describe your CLI exactly as you would with docopt-ng, adding type annotations inside angle brackets. The example below mirrors the runnable block at the bottom of src/docopt_typed/docopt_typed.py and shows both option and positional annotations:

from docopt_typed import docopt_typed

__doc__ = """Usage:
  myprogram.py --speed=<int> INPUT_FILE [options]

Arguments:
  INPUT_FILE <path>       The path to read from

Options:
  -s, --speed=<int>       Speed in mph (integer)
  -v, --verbose=<bool>    Enable verbose logging (true/false) [default: false]
  -n, --name=<str>        Your name [default: Alice]
  -d, --debug             Enable debug mode (no value needed)
"""

args = docopt_typed(__doc__)
print(args.speed, type(args.speed))        # 60 <class 'int'>
print(args.verbose, type(args.verbose))    # True <class 'bool'>
print(args.name, type(args.name))          # Bob <class 'str'>
print(args.debug, type(args.debug))        # True <class 'bool'>
print(args.INPUT_FILE, type(args.INPUT_FILE))  # Positional argument converted to Path

Running the script as python myprogram.py --speed=60 --verbose=true --name=Bob input.txt produces an already-typed result. Positional arguments are annotated by placing the type marker alongside the name (e.g. INPUT_FILE <path> in the Arguments section), while options continue to use the --flag=<type> pattern.

Supported type hints

The converter looks for the <type> token associated with each option or argument name and applies the matching callable:

  • int, float, complex
  • bool, boolean (accepts true/false, 1/0, yes/no, on/off in any case)
  • str, string, text
  • json, dict, list (parsed with json.loads)
  • bytes (UTF-8 encoding of the provided value)
  • path, file, filepath, dir, directory (converted to pathlib.Path)
  • none, null (converted to None)

Any pattern that cannot be converted cleanly is left untouched as the original string so you can handle validation yourself.

Attribute access and testing

docopt_typed returns the same ParsedOptions object as docopt-ng, so you can access results via args["--speed"] or args.speed. The accompanying tests in tests/test_docopt.py demonstrate how to supply custom argument lists, assert on types, and verify behavior for short-form flags and various boolean spellings.

Error handling

When a hint is missing, misspelled, or unsupported, the original string is preserved. This mirrors docopt's philosophy of trusting the usage documentation and keeps the wrapper safe as a drop-in replacement.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docopt_typed-0.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docopt_typed-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file docopt_typed-0.1.0.tar.gz.

File metadata

  • Download URL: docopt_typed-0.1.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for docopt_typed-0.1.0.tar.gz
Algorithm Hash digest
SHA256 111a93dd3fc3b4c8bfa69235c37424fbed7124badeeedc6804abfea4e4995a33
MD5 67137e5bd02b522e0cd391512714bda6
BLAKE2b-256 0f13c2922c4113c85149278cb9bb7352978906e422a61a1a64b59b8c4bcf557a

See more details on using hashes here.

File details

Details for the file docopt_typed-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: docopt_typed-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for docopt_typed-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9059367bd1990a8cc5565e7450e6543cf7eb69207d776114c29d34d480f6fe1b
MD5 42b4184bcdb295eaddf41d8100850d76
BLAKE2b-256 ec67400ad40c5bb68f7bbb27094077f284e207a0f0b9463a7245eb9befe88858

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page