Skip to main content

Create lightweight CLI arg parsers from pydantic models

Project description

Quickparser

Create lightweight CLI arg parsers from pydantic models.

This provides a clean, declarative way to create a command-line interface by allowing you to define the interface using Pydantic models.

For example,

from pydantic import BaseModel, Field
from quickparser import parser

@parser
class SimpleModel(BaseModel):
    """
    Example CLI for demonstration.
    """
    name: str
    age: int = Field(default=30)
    hobby: str | None
    employed: bool

args = SimpleModel.parse()

Is the equivalent of the following argparse code:

import argparse

parser = argparse.ArgumentParser(description="Example CLI for demonstration.")
parser.add_argument("--name", type=str, required=True)
parser.add_argument("--age", type=int, default=30)
parser.add_argument("--hobby", type=str, default=None)
parser.add_argument("--employed", action="store_true")
args = parser.parse_args()

Installation

pip install quickparser

Usage

We can take the example above and put it into an example.py script to demonstrate the basic usage:

# example.py
from pydantic import BaseModel, Field
from quickparser import parser


@parser
class SimpleModel(BaseModel):
    """
    Example CLI for demonstration.
    """
    name: str
    age: int = Field(default=30)
    hobby: str | None
    employed: bool

if __name__ == "__main__":
    args = SimpleModel.parse()
    print(args)

This script can be run from the command line, and it will parse the arguments provided to it. For example:

python example.py --name John --age 25 --hobby "reading" --employed

This will output:

SimpleModel(name='John', age=25, hobby='reading', employed=True)

Built on top of argparse, the parser provided automatically includes a help message, type casting based on type hints, default values, and required vs optional arguments. Help can be accessed by running the script with the --help flag:

python example.py --help

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

quickparser-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

quickparser-0.1.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file quickparser-0.1.1.tar.gz.

File metadata

  • Download URL: quickparser-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for quickparser-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1f133937473dce3ac214a5d579ec68442b8148d40f925f73b103a429c0bfff55
MD5 6e2c0d519e1f4c04bd78812ab2725e1f
BLAKE2b-256 a2fd1b39207fbe76d7ca5ce337bd95ad59435268fd62e00793e4eb201f91e9f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickparser-0.1.1.tar.gz:

Publisher: workflow.on-pr-merge.yml on jdraines/quickparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickparser-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: quickparser-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for quickparser-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 982b6a4af243128127aa409ed127773a0bd6f17f3fa2450bbfa6e4a1c13a0816
MD5 e43397bd27b9c5e1154c100c039a43f2
BLAKE2b-256 1709c56c2262bec123f04f664378c5cb5cbe18cbfca834e7e19aba0631a1be09

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickparser-0.1.1-py3-none-any.whl:

Publisher: workflow.on-pr-merge.yml on jdraines/quickparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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