Skip to main content

A declarative and type-safe command-line argument parser.

Project description

typed-clap[^1]

CI

A declarative and type-safe argument parser for Python, inspired by clap-rs.

Installation uv

  • Using uv: uv add typed-clap
  • Using pip: pip install typed-clap

Example

import clap
from clap import arg, long, short
from pathlib import Path

@clap.command
class Cli(clap.Parser):
    """A tiny script."""

    input: Path = arg(value_name="PATH")
    """Path to the input file."""
    verbose: bool = arg(short, long)
    """Enable verbose output."""

args = Cli.parse()
if args.verbose:
    print(f"Reading {args.input}...")

See /examples for more examples.

Features

  • Help generation from docstrings

    Use the same string for the help output as well as documentation in the IDE.

  • Subcommands

    @clap.subcommand
    class Add:
        file: Path
    
    @clap.subcommand
    class List:
        directory: Path
    
    @clap.command
    class Cli(clap.Parser):
        command: Add | List
    
    args = Cli.parse()
    match args.command:
        case Add(file):
            print(f"Adding {file}...")
        case List(directory):
            print(f"Listing {directory}...")
    
  • Argument groups

    @clap.group(required=True, multiple=False)
    class InputOptions:
        """Only one of these can be provided.
    
        This can be shared between multiple parsers in different scripts!"""
        dpi: Optional[int] = arg(long)
        resolution: Optional[tuple[int, int]] = arg(long, value_name="PX")
    
    @clap.command
    class Cli(clap.Parser):
        input_options: InputOptions
    
    args = Cli.parse()
    print(args.input_options.dpi or args.input_options.resolution)
    
  • Separate short and long help with -h and --help. See example output here.

  • Customize help output with templates and styles.

Docs

Documentation along with the quickstart guide can be found on the docs website built from /docs.

Motivation

argparse doesn't work with static analysis tools.

Static analysis is important to prevent errors like these:

import argparse
from pathlib import Path

import torch

parser = argparse.ArgumentParser()
# 50 lines of other arguments...
parser.add_argument("--data", type=Path)
parser.add_argument("--some-number", type=Path)  # copy-paste error in type
# 30 lines of other arguments...
args = parser.parse_args()

# 500 lines of code...

c = 1 / args.some_number   # some_number was accidentally set to be Path
#   ~~^~~~~~~~~~~~~~~~~~

Once that error is fixed and the script is re-run:

# 1500 lines of code...
 
torch.save(agi, args.data_dir / "agi.pt")  # this attribute does not exist
#               ~~~~~~~~~^^^^

These errors are detected right when you typed them if you use this library and a type checker like pyright. (A dry run is obviously still recommended before starting long training runs. E.g., this library will not see if args.data_dir exists on disk.)

Also, using subcommands with argparse is error-prone because argparse returns a flat namespace, overwriting global arguments with subcommand arguments. This should be written in bold all over the argparse docs but it isn't. The workaround is to manually set dest for each argument, which is a tedious and error-prone process.

Supported type checkers

typed-clap is successfully type checked in CI by:

It can also be used with ty[^2].

Contributing

PRs that fix bugs, add features from clap-rs, or complete the following TODOs are welcome. For adding other features, please create a discussion before creating a PR. Thank you!

TODO (v1.0)

  • Better diagnostics (source range highlighting etc.) for incorrect parsers.
  • Parse arguments manually instead of using argparse. This will improve error messages for invalid arguments.

Future work (beyond v1.0)

  • Add support for custom value parsers, validation, conflicts_with, etc.
  • Generate shell completions.
  • Add a clap-like builder API to add arguments procedurally (after defining some arguments in a class), which can be used together with the declarative API.
  • Find or build out the python equivalent of color_print::cstr! and support styled help strings that wrap properly and are formatted depending on output file.

Acknowledgements

clap-rs. Most docstrings are lifted verbatim.

[^1]: Because the names clap{,-py,-python} were already taken on PyPI :-( [^2]: ty is not a part of the CI because it gives some false positives in the completely standard internal code since ty is not complete and still a WIP. It does not have any issues with the APIs of this library or their usage, though.

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

typed_clap-0.11.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

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

typed_clap-0.11.0-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file typed_clap-0.11.0.tar.gz.

File metadata

  • Download URL: typed_clap-0.11.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typed_clap-0.11.0.tar.gz
Algorithm Hash digest
SHA256 7b25d5e0573379239d314fe57613685da8520dc74901543162f1ed78983c5aa9
MD5 ba4327f574bdb56ea3164f7b23d341f5
BLAKE2b-256 80266aa402cbace8e66826a7db96fa26ced990699b833fbd0ef28e34f4eb069b

See more details on using hashes here.

File details

Details for the file typed_clap-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: typed_clap-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 26.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typed_clap-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b48b02c98691bebdd20b40ec575c145094809e9b28820f975d243e832975cad2
MD5 cb67d20f09bb91f7745badab7217ba46
BLAKE2b-256 4f2e68533544f235331d415399c4796b714f5b3902c83cb7dec841dec5046519

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