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
  • Default factories for dynamic value generation (UUIDs, timestamps, etc.)
  • Dataclass conversion for easy integration with other libraries
  • JSON/Pickle serialization for configuration management
  • Configuration file support with command-line override capabilities

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"

Default Factories

Generate dynamic values at parse time:

import uuid
from datetime import datetime
from spargear import BaseArguments, ArgumentSpec


class AppConfig(BaseArguments):
    # Method 1: Direct callable assignment (auto-detected)
    session_id: str = lambda: str(uuid.uuid4())
    
    # Method 2: Explicit ArgumentSpec with default_factory
    log_file: ArgumentSpec[str] = ArgumentSpec(
        ["--log-file"],
        default_factory=lambda: f"log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
        help="Log file path"
    )
    
    name: str = "myapp"  # Regular default value


config = AppConfig()
print(f"Session ID: {config.session_id}")  # Unique UUID each time
print(f"Log file: {config.log_file.unwrap()}")  # Timestamp-based filename

Configuration Management

Save and load configurations:

from spargear import BaseArguments
from typing import List


class ServerConfig(BaseArguments):
    host: str = "localhost"
    port: int = 8080
    debug: bool = False
    allowed_hosts: List[str] = ["127.0.0.1", "localhost"]


# Create and configure
config = ServerConfig(["--port", "3000", "--debug"])

# Convert to dataclass
config_dc = config.to_dataclass()
print(f"Dataclass: {config_dc}")

# Save to JSON
config.save_config("server.json")

# Load from JSON with command-line overrides
loaded_config = ServerConfig.load_config("server.json", args=["--port", "9000"])
print(f"Port: {loaded_config.port}")  # 9000 (overridden)
print(f"Debug: {loaded_config.debug}")  # True (from file)

# Update from dictionary
config.update_from_dict({"host": "0.0.0.0", "port": 5000})

API Reference

BaseArguments Methods

Serialization

  • to_dict() -> Dict[str, Any] - Convert to dictionary
  • to_json(file_path=None, **kwargs) -> str - Serialize to JSON
  • to_pickle(file_path) - Serialize to pickle file
  • to_dataclass(class_name=None) - Convert to dataclass instance

Deserialization

  • from_dict(data, args=None) - Create from dictionary
  • from_json(json_data, args=None) - Create from JSON string/file
  • from_pickle(file_path, args=None) - Create from pickle file

Configuration Management

  • save_config(file_path, format="json") - Save configuration
  • load_config(file_path, format=None, args=None) - Load configuration
  • update_from_dict(data) - Update current instance

ArgumentSpec Features

Default Factories

ArgumentSpec(
    name_or_flags=["--arg"],
    default_factory=lambda: generate_value(),  # Called at parse time
    help="Description"
)

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.2.0.tar.gz (19.0 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.2.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for spargear-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9a25a7a1abb212d1789d9ea0485b5a12b8e96d9d8d2e6996396d43b9c4512d3d
MD5 2f5c9b0f4e50502de534617f41b80f6b
BLAKE2b-256 926d645dd6cce0f2f9cf9917a4b0951183a047f4eef1e4422da5a67887965cf8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for spargear-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bfef8e72c9cd34b23d31fa93db2f34d94bbc9cab31874c11c0caa5c8f646274
MD5 f534fb2ea080b8a929571beaa219310b
BLAKE2b-256 7243b6db3ed61580b6da5d19e39ac213d4776efd245ff44471af5919b01bcdc8

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