Skip to main content

Declarative and typed argument parsing built on argparse.

Project description

⚙️ spargear

PyPI PyPI - Downloads

A powerful yet simple Python library for declarative command-line argument parsing, built on top of argparse. spargear enables elegant, type-safe definitions of CLI arguments and subcommands with minimal boilerplate.

Why spargear?

  • Declarative: Define your CLI arguments neatly using Python data classes.
  • 🚀 Typed and Safe: Leveraging Python typing and dataclasses to ensure type safety and developer productivity.
  • 🔧 Flexible: Supports complex argument parsing scenarios, including subcommands and nested configurations.
  • 📦 Minimal Dependencies: Pure Python, built directly upon the reliable argparse module.

Installation

Install with pip:

pip install spargear

Quick Start

Define your arguments:

from spargear import ArgumentSpec, BaseArguments


class MyArgs(BaseArguments):
    input_file: ArgumentSpec[str] = ArgumentSpec(["-i", "--input"], required=True, help="Input file path")
    verbose: ArgumentSpec[bool] = ArgumentSpec(["-v", "--verbose"], action="store_true", help="Enable verbose output")

# Parse the command-line arguments
args = MyArgs()

# Access the parsed arguments
input_file: str = args.input_file.unwrap()  # If none, it raises an error
# input_file: str | None = args.input_file.value
verbose: bool = args.verbose.unwrap()  # If none, it raises an error
# verbose: str | bool = args.verbose.value
print(f"Input file: {input_file}")
print(f"Verbose mode: {verbose}")

Run your CLI:

python app.py --input example.txt --verbose

Features

  • Automatic inference of argument types
  • Nested subcommands with clear definitions
  • Typed file handlers via custom protocols
  • Suppress arguments seamlessly

Advanced Usage

Subcommands:

from typing import Optional
from spargear import BaseArguments, SubcommandSpec, ArgumentSpec


class InitArgs(BaseArguments):
    name: ArgumentSpec[str] = ArgumentSpec(["name"], help="Project name")


class CommitArgs(BaseArguments):
    message: ArgumentSpec[str] = ArgumentSpec(["-m"], required=True, help="Commit message")


class GitCLI(BaseArguments):
    init = SubcommandSpec("init", InitArgs, help="Initialize a new repository")
    commit = SubcommandSpec("commit", CommitArgs, help="Commit changes")


# Parse the command line arguments
args = GitCLI()

# Print the parsed arguments
name: Optional[str] = args.init.argument_class.name.value
message: Optional[str] = args.commit.argument_class.message.value
print(f"Name: {name}")
print(f"Message: {message}")

Run your CLI:

python app.py init my_project
python app.py commit -m "Initial commit"

Compatibility

  • Python 3.8+

License

MIT

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

spargear-0.1.5.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

spargear-0.1.5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file spargear-0.1.5.tar.gz.

File metadata

  • Download URL: spargear-0.1.5.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for spargear-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9f652d7a51b7a3b02e6e3f499653d735e482cf3d28be5851e5a1e16a2821f7d9
MD5 a78e9021717c5f80050092009f03ef44
BLAKE2b-256 f2091d0283965e9cef402664218307773271975d5cdde375763b408ddff4cae5

See more details on using hashes here.

File details

Details for the file spargear-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: spargear-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for spargear-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b0483df29abfbb6e8ad5e9497d151bdafd91091326e1bca580ddac606ad86c36
MD5 702a3aa5093d7296b090b3d7979a2cd1
BLAKE2b-256 9676af8132138f8869f9c8383a8dfe62e1e1ed3779093c45bd378dab762a6e9d

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