Metadata-Version: 2.3
Name: typer-swissknife
Version: 0.2.0
Summary: lightweight companion library for Typer that helps you build cleaner, more expressive CLI applications.
Author: aaltatan
Author-email: aaltatan <a.altatan@gmail.com>
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Operating System :: OS Independent
Requires-Dist: pyfiglet>=1.0.4
Requires-Dist: typer>=0.26.8
Requires-Python: >=3.12
Project-URL: Homepage, https://github.com/aaltatan/typer-swissknife
Project-URL: Issues, https://github.com/aaltatan/typer-swissknife/issues
Description-Content-Type: text/markdown

# typer-swissknife

typer-swissknife is a lightweight companion library for [Typer](https://typer.tiangolo.com/) 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

```bash
pip install typer-swissknife
```

## Quick example

```python
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](https://github.com/pwaller/pyfiglet) (default: `ansi_shadow`).
- The banner is formatted for the app's `rich_markup_mode` (`"rich"`, `"markdown"` or `None`).

## 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:

```python
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()
```

```console
$ 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:

  ```python
  @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

```bash
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).
