Skip to main content

typer-swissknife

Documentation: https://aaltatan.github.io/typer-swissknife/

typer-swissknife is a lightweight companion library for Typer that helps you build cleaner, more expressive CLI applications.

It focuses on a small set of practical helpers for common CLI patterns such as alias-aware commands, nested sub-apps, and richer help output.

Why use it

  • Keep command registration simple and readable.
  • Organize larger CLIs into smaller, reusable Typer components.
  • Improve discoverability with optional visual help text.
  • Stay close to Typer's native behavior while adding a bit more structure.

Features

  • Alias-aware command decorators
  • Nested Typer app registration
  • Optional ASCII-enhanced help sections
  • Undeclared options collected into **kwargs
  • Simple integration into existing Typer projects

Installation

pip install typer-swissknife

Quick example

import typer
from typer_swissknife import add_typer, command

app = typer.Typer()
admin = typer.Typer()

@command(app, name="list", aliases=("ls",), ascii_name="List")
def list_items():
    """List all items."""
    print("Listing items...")

@command(admin, aliases=("rm",))
def remove(item: str):
    print(f"Removing {item}")

add_typer(
    app,
    admin,
    name="admin",
    ascii_name="Admin",
    aliases=("a",),
    help="Administrative commands",
)

if __name__ == "__main__":
    app()
  • list, ls, admin and a all work; the help lists them once as list (ls) and admin (a).
  • ascii_name draws a banner at the top of that command's or group's --help page, while the commands list keeps the plain help text. Use ascii_font to pick any pyfiglet font (default: ansi_shadow).
  • The banner is formatted for the app's rich_markup_mode ("rich", "markdown" or None).

Classes as command groups

Mark methods with @command() (no app) and pass the class to add_typer. Undecorated methods are not commands. Passing the class creates it per command call, so its __init__ parameters (including typer-di Depends) become options of every command.

class Users:
    """Manage users."""

    def __init__(self, db_url: str = typer.Option("sqlite:///app.db", "--db-url")) -> None:
        self.db_url = db_url

    @command(name="list", aliases=("ls",))
    def list_users(self) -> None:
        print(f"users in {self.db_url}")

    def connect(self) -> None:  # helper, not a command
        ...

add_typer(app, Users, aliases=("u",))   # $ app u ls --db-url postgresql://...

Undeclared options with **kwargs

Typer only accepts options that you declare. With command, a function that takes **kwargs also receives any option you did not declare:

import typer
from typer_swissknife import command

app = typer.Typer()

@command(app)
def some_command(name: str, **options: int):
    print(name, options)

if __name__ == "__main__":
    app()
$ some-command Abdullah --num1 10 --num2 20
Abdullah {'num1': 10, 'num2': 20}

$ some-command Abdullah --num1=10 --page-size=50
Abdullah {'num1': 10, 'page_size': 50}

$ some-command Abdullah --num1 abc
Usage: some-command [OPTIONS] NAME
Try 'some-command --help' for help.
┌─ Error ─────────────────────────────────────────────┐
│ Invalid value for --num1: 'abc' is not a valid int. │
└─────────────────────────────────────────────────────┘
  • Undeclared options can go before, between or after the arguments (some-command --num1 10 Abdullah works too).

  • Both --key value and --key=value work. Dashes in a name become underscores (--page-size gives page_size).

  • Each value is converted with the annotation of **kwargs (int, float, bool, ...). Without an annotation, values stay strings:

    @command(app)
    def tag(**labels):
        print(labels)  # tag --env prod --tier 2  ->  {'env': 'prod', 'tier': '2'}
    
  • Declared parameters work as usual, alongside the undeclared ones.

  • These inputs exit with a usage error: an option with no value (such as a bare --verbose), a stray positional word, and an option that has the same name as a declared parameter.

  • Undeclared options are not listed in --help.

Development

uv sync
uv run pytest

Tests run with branch coverage, and the run fails below 100%.

Project philosophy

This package is intentionally compact and focused. As the project grows, the goal is to keep the documentation high-level and feature-oriented rather than listing every helper in detail.

License

typer-swissknife is licensed under the GNU General Public License v3 (GPLv3).

Metadata

Release files for typer-swissknife 0.3.1

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

Source distribution (sdist)

Source distribution for typer-swissknife 0.3.1
File Size Uploaded Provenance Actions
typer_swissknife-0.3.1.tar.gz 12.3 kB Without provenance Details for typer_swissknife-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for typer-swissknife 0.3.1
File Interpreter ABI Platform Actions
typer_swissknife-0.3.1-py3-none-any.whl Python 3 none any Details for typer_swissknife-0.3.1-py3-none-any.whl

Total release size: 27.6 kB

Release files / typer_swissknife-0.3.1.tar.gz

Download URL typer_swissknife-0.3.1.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
27a365fd9febb2a31c457dfdc46d30785603b881f607d529497b217abdc65295
BLAKE2b-256 checksum
How to use checksums
b033b71f3dfccbb10d2524e97fbe348bb110baab2deae5f1c178cc8bd13363e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / typer_swissknife-0.3.1-py3-none-any.whl

Download URL typer_swissknife-0.3.1-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07db68b33b2650346971ae59f3315621bc96073d52572f839b352c0009242959
BLAKE2b-256 checksum
How to use checksums
6cac0d310676ea3a3737d2af819c39e360eb0a0382abeaac9b9dbd9579e274d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for typer-swissknife 0.3.1

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.

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