Skip to main content

Type-safe facade for the venerable argparse

Project description

paramspecli

paramspecli is a facade for the venerable argparse. It's simple, composable, and fun. But, most important, it's type-safe thanks to the ParamSpec magic.

Quick start

from paramspecli import Command, argument, option, flag


def ping(addr: str, *, until_stopped: bool, count: int | None):
    print(f"Pinging {addr} ...")


cli = Command(ping, prog="ping")
cli.bind(
    -argument("IP"),
    until_stopped=-flag("-t"),
    count=-option("-n", type=int),
)

result = cli.parse()
result()

Running it:

$ python ping.py -t -n 10 127.0.0.1
Pinging 127.0.0.1 ...

Type checker catches common errors:

cli.bind(
    # "bool" is not assignable to "str"
    -argument("IP", type=bool),
    # Type "bool | None" is not assignable to type "bool"
    until_stopped=-flag("-t", default=None),
    #
    # Argument missing for parameter "count"
)

IDE suggestions work too:

cli.bind(⏎
    (addr: str, *, until_stopped: bool, count: int | None) -> None

Installation

pip install paramspecli

Key points

paramspecli builds hierarchical CLIs with groups and commands.

  • Commands match handler functions parameters with arguments and options.
  • Arguments are bound to the handler's positional parameters.
  • Options are bound to the handler's keyword parameters.
  • Groups organize the commands. Groups may be nested. They could act like an intermediate commands, i.e. have own handlers, options and arguments.
  • CLI 'compiles' to the argparse, runs it, then outputs the parse result.
  • Parse result is a callable. Calling it invokes handlers along the route.

paramspecli supports several advanced argparse features: help sections, mutually exclusive options, options with the same destination. And adds a few own, like const options and passing contexts. It also includes a markdown documentation generator.

Read the documentation

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

paramspecli-0.3.0.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

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

paramspecli-0.3.0-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file paramspecli-0.3.0.tar.gz.

File metadata

  • Download URL: paramspecli-0.3.0.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for paramspecli-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ccca029a7193045da4ded57b624aac4591473f308d7512a54ea170965bb23179
MD5 2613a1488fda1a0c6d43d25ca754777e
BLAKE2b-256 1769cf9a8e754f7d82d17c18c0ee0362ec6b20e383147b483bdce2b2977849d6

See more details on using hashes here.

File details

Details for the file paramspecli-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: paramspecli-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for paramspecli-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2f49ede144926279134091f89ccb6bab7a51dd17386c9de2f5862c88e270b71
MD5 ec554866a141d83e9733b6fd1d23e8b2
BLAKE2b-256 2aed1a2d90728d6996fc445911bcc5e852e6d39fe315b144bf3a7398132a59e3

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