コンテンツへスキップ

サブタイパーコールバックオーバーライド

Typer アプリを作成する際に、コールバック関数を定義できます。これは常に実行され、コマンドの前に来るCLI引数CLIオプションを定義します。

別のTyperアプリの中にTyperアプリを追加する場合、サブタイパーにも独自のコールバックを持たせることができます。

これは、それ自身の独自のコマンドの前に来るCLIパラメータを処理し、追加のコードを実行することができます。

import typer

app = typer.Typer()

users_app = typer.Typer()
app.add_typer(users_app, name="users")


@users_app.callback()
def users_callback():
    print("Running a users command")


@users_app.command()
def create(name: str):
    print(f"Creating user: {name}")


if __name__ == "__main__":
    app()

この例では、CLIパラメータは定義せず、メッセージを出力するだけです。

確認してください

$ python main.py users create Camila

// Notice the first message is not created by the command function but by the callback
Running a users command
Creating user: Camila

作成時にコールバックを追加する

別のTyperアプリに追加されるtyper.Typer()アプリを作成する際に、コールバックを追加することも可能です。

import typer

app = typer.Typer()


def users_callback():
    print("Running a users command")


users_app = typer.Typer(callback=users_callback)
app.add_typer(users_app, name="users")


@users_app.command()
def create(name: str):
    print(f"Creating user: {name}")


if __name__ == "__main__":
    app()

これは上記と全く同じ効果があり、コールバックを追加する場所がもう一つ増えただけです。

確認してください

$ python main.py users create Camila

Running a users command
Creating user: Camila

作成時のコールバックをオーバーライドする

typer.Typer()アプリの作成時にコールバックが追加されていた場合、@app.callback()を使用して新しいコールバックでオーバーライドできます。

これはコマンド - Typerコールバックに関するセクションで見た情報と同じであり、サブタイパーアプリにも同様に適用されます。

import typer

app = typer.Typer()


def default_callback():
    print("Running a users command")


users_app = typer.Typer(callback=default_callback)
app.add_typer(users_app, name="users")


@users_app.callback()
def user_callback():
    print("Callback override, running users command")


@users_app.command()
def create(name: str):
    print(f"Creating user: {name}")


if __name__ == "__main__":
    app()

ここでは、typer.Typer()サブアプリの作成時にコールバックを定義しましたが、その後、関数user_callback()を使用して新しいコールバックでオーバーライドします。

@app.callback()typer.Typer(callback=some_function)よりも優先されるため、CLIアプリはこの新しいコールバックを使用するようになります。

確認してください

$ python main.py users create Camila

// Notice the message from the new callback
Callback override, running users command
Creating user: Camila

サブタイパーを追加する際のコールバックをオーバーライドする

最後に、app.add_typer()を使用してサブタイパーを追加する際に、callbackパラメータを使用して、他の場所で定義されたコールバックをオーバーライドできます。

これが最高の優先順位を持ちます。

import typer

app = typer.Typer()


def default_callback():
    print("Running a users command")


users_app = typer.Typer(callback=default_callback)


def callback_for_add_typer():
    print("I have the high land! Running users command")


app.add_typer(users_app, name="users", callback=callback_for_add_typer)


@users_app.callback()
def user_callback():
    print("Callback override, running users command")


@users_app.command()
def create(name: str):
    print(f"Creating user: {name}")


if __name__ == "__main__":
    app()

優先順位はapp.add_typer()にあり、実行順序の影響を受けないことに注意してください。以下に別のコールバックが定義されていますが、app.add_typer()からのものが優先されます。

これで、CLIプログラムを使用すると、新しいコールバック関数callback_for_add_typer()が使用されます。

確認してください

$ python users create Camila

// Notice the message from the callback added in add_typer()
I have the high land! Running users command
Creating user: Camila