Skip to main content

SwiftCLI

Build testable CLI apps with click and pydantic

swiftcli makes it easy to define CLI parameters with pydantic BaseModels, combining the power of click with pydantic's data validation.

Features

  • Define CLI parameters using pydantic models
  • Type validation and conversion out of the box
  • Support for arguments, options, flags and switches
  • Easy to test CLI applications

Installation

pip install swiftcli

Simple Example

from pydantic import BaseModel

from swiftcli import BaseCommand, Group
from swiftcli.types import Argument, Option


class GreetParams(BaseModel):
    name: Argument[str]  # required argument
    color: Option[str] = ""  # Optional --color option with default value


class GreetCommand(BaseCommand[GreetParams]):
    NAME = "greet"

    def run(self) -> None:
        if self.params.color:
            print(f"Hello, {self.params.name}. You like the color {self.params.color}.")
        else:
            print(f"Hello, {self.params.name}.")


cli = Group()
cli.add_command_cls(GreetCommand)

if __name__ == "__main__":
    cli()

Parameter Types

SwiftCLI provides several parameter types through the swiftcli.types module:

Argument

Required positional arguments:

class MyParams(BaseModel):
    filename: Argument[str]  # Required positional argument

Option

Optional named parameters with default values:

class MyParams(BaseModel):
    output: Option[str] = "output.txt"  # --output option with default
    count: Option[int] = 1  # --count option with default

Flag

Boolean flags that can be enabled:

class MyParams(BaseModel):
    verbose: Flag  # --verbose flag, defaults to False

Switch

Enum-based switches that create multiple mutually exclusive flags:

from enum import Enum

class LogLevel(str, Enum):
    DEBUG = "debug"
    INFO = "info" 
    ERROR = "error"

class MyParams(BaseModel):
    log_level: Switch[LogLevel] = LogLevel.INFO  # Creates --debug, --info, --error flags

Advanced Usage

Command Configuration

Commands can be configured using the CONFIG class variable:

class MyCommand(BaseCommand[MyParams]):
    NAME = "mycommand"
    CONFIG = {
        "help": "My command help text",
        "short_help": "Short help",
        "epilog": "Additional help text at the bottom",
        "hidden": False,
        "deprecated": False,
    }

Option Customization

Options can be customized using OptionSettings:

from typing import Annotated
from swiftcli.types import OptionSettings

class MyParams(BaseModel):
    verbose: Annotated[
        int,
        OptionSettings(
            count=True,  # Allow multiple flags (-vvv)
            aliases=["-v"],  # Add short alias
            help="Sets the verbosity level"
        )
    ] = 0

Command Groups

Group multiple commands together:

from swiftcli import Group

cli = Group()
cli.add_command_cls(CommandOne)
cli.add_command_cls(CommandTwo)

if __name__ == "__main__":
    cli()

Testing

SwiftCLI makes it easy to test your CLI applications:

# Import the command we want to test
from my_cli.commands import MyCommand

# Test the command
def test_my_command():
    # You can mock, stub, or read the stdout/err
    # Run the command:
    MyCommand(param1="a", param2="b").run()

License

MIT

Metadata

Release files for swiftcli 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swiftcli 0.2.0
File Size Uploaded Provenance Actions
swiftcli-0.2.0.tar.gz 6.2 kB Without provenance Details for swiftcli-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swiftcli 0.2.0
File Interpreter ABI Platform Actions
swiftcli-0.2.0-py3-none-any.whl Python 3 none any Details for swiftcli-0.2.0-py3-none-any.whl

Total release size: 14.5 kB

Release files / swiftcli-0.2.0.tar.gz

Download URL swiftcli-0.2.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3c9646d7d8bd4c85c15422c12a34f3fae890ed011b6b1d295d2a9aa8a02e0f83
BLAKE2b-256 checksum
How to use checksums
8a7f467a8d274778021fef9d56cf9ab8b0b5a4c558c0560c39cf625248f47743
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.12 Linux/6.11.0-1018-azure

Release files / swiftcli-0.2.0-py3-none-any.whl

Download URL swiftcli-0.2.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27715207ed4cc613e0a7f97c028012eb6ecc22951885eb7792d45f6a4c26f970
BLAKE2b-256 checksum
How to use checksums
04f6cb6a6175641c57c4235c1f637dee532a5bc5c86b926a7ebb46fac8cad51d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.12 Linux/6.11.0-1018-azure

Security for swiftcli 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page