Skip to main content

TypedDict based cli argument parsing

Project description

sleazy

A tiny utility to build CLI tools from Python TypedDict schemas.
Define your parameters with type hints (and Annotated for positionals), then call parse() to get a typed dict and stringify() to turn it back into args.

Installation

uv pip install sleazy

Example CLI

from typing import Annotated, Literal, TypedDict
from sleazy import parse, stringify


class AppConfig(TypedDict):
    # 1) Required positional (exactly one string)
    input_file: Annotated[str, 1]

    # 2) Optional positional (zero or more strings)
    tags: Annotated[str, "*"]

    # 3) Optional keyword args
    output: str  # string
    verbose: bool  # flag
    retries: int  # integer
    mode: Literal["auto", "manual"]  # choice via Literal or enum


if __name__ == "__main__":
    # parse() returns a dict matching AppConfig
    args = parse(AppConfig) # python example.py data.txt tag1 tag2 --output out.txt --verbose --retries 5 --mode auto
    print(args)
    # {
    #   'input_file': 'data.txt',
    #   'tags': ['tag1', 'tag2'],
    #   'output': 'out.txt',
    #   'verbose': True,
    #   'retries': 5,
    #   'mode': 'auto'
    # }

    # stringify back to CLI args, using AppConfig to keep positionals first
    print(stringify(args, AppConfig))
    # [
    #   'data.txt',
    #   'tag1', 'tag2',
    #   '--output', 'out.txt',
    #   '--verbose',
    #   '--retries', '5',
    #   '--mode', 'auto'
    # ]

    # Without the TypedDict, stringify treats everything as flags
    print(stringify(args))
    # [
    #   '--input-file', 'data.txt',
    #   '--tags', 'tag1', '--tags', 'tag2',
    #   '--output', 'out.txt',
    #   '--verbose',
    #   '--retries', '5',
    #   '--mode', 'auto'
    # ]

Notes

  • Positionals are marked via Annotated[type, count] (1, ?, *, +, or exact integer).
  • All other fields become --field-name flags by default.
  • It's useful to pass your TypedDict type to stringify() so it knows which fields are positional (and their order). Without it, everything is emitted as a keyword flag.

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

sleazy-0.2.0.tar.gz (42.5 kB view details)

Uploaded Source

Built Distribution

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

sleazy-0.2.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file sleazy-0.2.0.tar.gz.

File metadata

  • Download URL: sleazy-0.2.0.tar.gz
  • Upload date:
  • Size: 42.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for sleazy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7499177079a60a112510416a55d24536808ebc6f4a371ff5aec5cb6675ef98a2
MD5 80707797a65fafc8ebe7b150ddc578bf
BLAKE2b-256 163e453c16b308d0e95fdc9039ef2e3d76678eef2e0d91045b0e975727a7414d

See more details on using hashes here.

File details

Details for the file sleazy-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sleazy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for sleazy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3a56ee770a80f84b8f4a0398d72f96abe3583cfd31ac1d381cf70f66346d636
MD5 d2f87bf22eda82f7e0885259890fc431
BLAKE2b-256 214ee666300efdd52f347c80c595e118fff110650eecc269d27ec3206cd57145

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