Skip to main content

Hooks for typer CLIs.

Project description

TyperHook

Overview

Hook decorators to enhance Typer CLIs. Current hooks:

  1. defaultparams: adapted from GitHub user Antoine adm271828, shares parameters between typer commands

Installation

pip install typerhook

Quick Start

import typer
import typerhook
from typing import Annotated

app = typer.Typer()

def debug_params(
    ctx: typer.Context,
    debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False,
    verbose: Annotated[bool, typer.Option(help="Verbose output")] = False,
):
    ctx.ensure_object(dict)
    ctx.obj['debug'] = debug
    ctx.obj['verbose'] = verbose

@app.command()
@typerhook.defaultparams(debug_params)
def hello(
    ctx: typer.Context,
    name: Annotated[str, typer.Option(help="Name to greet")] = "World"
):
    debug_mode = ctx.obj.get('debug', False)
    typer.echo(f"Hello {name}! Debug mode: {debug}; Verbose mode: {verbose}")

@app.command()
@typerhook.defaultparams(debug_params)
def bye(
    ctx: typer.Context,
    name: Annotated[str, typer.Option(help="Name to greet")] = "World"
):
    debug = ctx.obj.get('debug', False)
    verbose = ctx.obj.get('verbose', False)
    typer.echo(f"Goodbye {name}! Debug mode: {debug}; Verbose mode: {verbose}")

if __name__ == "__main__":
    app()

API Reference

defaultparams(extra: Callable, *, drop: Optional[Sequence[str]]=None)

Decorator factory that adds extra parameters to a Typer command.

Parameters:

  • extra: Function containing the extra parameters to inject
  • drop: List of parameter names to exclude from the final signature

License

MIT License - see LICENSE file for details.

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

typerhook-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

typerhook-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file typerhook-0.1.0.tar.gz.

File metadata

  • Download URL: typerhook-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for typerhook-0.1.0.tar.gz
Algorithm Hash digest
SHA256 df3ce0a9c09d463b1b450f11be91a225100568f04bcaac3097025e37baa7eb55
MD5 214c410b08dafe6444bd9c35b9923fd9
BLAKE2b-256 7aca73209203cacd383f65bea4d5518f57ca993f8ca8a4ecf78f81ed603a9b58

See more details on using hashes here.

File details

Details for the file typerhook-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: typerhook-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for typerhook-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43e9a17d07ee074340d100d209beb21f885c1af1f87fe9125474aef850b1b013
MD5 cff5855e5801e33892982a5c23b5bc5a
BLAKE2b-256 aacef55019ca4941e96d28591ce0eeb087f26954835ca9ba8bf4419a8a93fbd7

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