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.1.tar.gz (23.4 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.1-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for paramspecli-0.3.1.tar.gz
Algorithm Hash digest
SHA256 30c00cf7bc8b5369fbda29cbc6e6994520612f0fffcd99bd59525b2cff163103
MD5 01aef6e85192c3f20ecd8770edff3370
BLAKE2b-256 2d042c528392d546c592652cce6b2b51628e57bdbc9b36eaf139eee67df86323

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for paramspecli-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 53325fcc6d50b48e9b8c248197c81439451c23b293005bcab69770ec55121198
MD5 92efbf62d82c47116266ba19c716dd0b
BLAKE2b-256 f3303c7f5630cea9b4aa18a780edbaefacbba98d48a9efcd3cd7f701029049da

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