コンテンツへスキップ

Typerを追加

まず、コアとなるアイデアから始めましょう。

別の typer.Typer() アプリケーションの中に typer.Typer() アプリケーションを追加することです。

アイテムの管理

遠い土地のアイテムを管理するための*CLIプログラム*を作成していると想像してみてください。

これは items.py ファイルに次のように記述できます。

import typer

app = typer.Typer()


@app.command()
def create(item: str):
    print(f"Creating item: {item}")


@app.command()
def delete(item: str):
    print(f"Deleting item: {item}")


@app.command()
def sell(item: str):
    print(f"Selling item: {item}")


if __name__ == "__main__":
    app()

そして、このように使用します。

$ python items.py create Wand

Creating item: Wand

ユーザーの管理

しかし、その後、*CLIアプリケーション*からユーザーも管理する必要があることに気づきました。

これは、users.py ファイルに次のように記述できます。

import typer

app = typer.Typer()


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


@app.command()
def delete(user_name: str):
    print(f"Deleting user: {user_name}")


if __name__ == "__main__":
    app()

そして、このように使用します。

$ python users.py create Camila

Creating user: Camila

それらをまとめる

両方の部分は似ています。実際、items.pyusers.py はどちらも createdelete のコマンドを持っています。

しかし、それらを同じ*CLIプログラム*の一部にする必要があります。

この場合、git remote のように、別の typer.Typer() *CLIプログラム*にサブコマンドとしてまとめることができます。

次に、次のように main.py を作成します。

import typer

import items
import users

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

if __name__ == "__main__":
    app()

以下が main.py で行っていることです。

  • 他のPythonモジュール(users.py および items.py ファイル)をインポートします。
  • メインの typer.Typer() アプリケーションを作成します。
  • app.add_typer() を使用して、items.py および users.py からの app を含めます。これらの2つも typer.Typer() で作成されました。
  • これらの「サブタイパー」のそれぞれが、独自のコマンドをグループ化するために使用されるコマンドで name を定義します。

これで、*CLIプログラム*に2つのコマンドができました。

  • usersusers.py からの app にあるすべてのコマンド(サブコマンド)付き。
  • itemsitems.py からの app にあるすべてのコマンド(サブコマンド)付き。

確認しましょう

// Check the help
$ python main.py --help

Usage: main.py [OPTIONS] COMMAND [ARGS]...

Options:
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it or customize the installation.
  --help                Show this message and exit.

Commands:
  items
  users

これで、*CLIプログラム*に itemsusers のコマンドがあり、それらはさらに独自のコマンド(サブコマンド)を持っています。

items コマンドを確認しましょう。

// Check the help for items
$ python main.py items --help

// It shows its own commands (subcommands): create, delete, sell
Usage: main.py items [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  create
  delete
  sell

// Try it
$ python main.py items create Wand

Creating item: Wand

$ python main.py items sell Vase

Selling item: Vase

ヒント

$ python main.py をまだ呼び出していますが、今は items コマンドを使用していることに注意してください。

次に、すべてのサブコマンドを持つ users コマンドを確認します。

$ python main.py users --help

Usage: main.py users [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  create
  delete

// Try it
$ python main.py users create Camila

Creating user: Camila

まとめ

これがコアとなるアイデアです。

typer.Typer() アプリケーションを作成して、それらを互いの中に追加するだけです。

そして、任意のレベルのコマンドでそれを行うことができます。

サブ-サブ-サブ-サブコマンドが必要ですか?どうぞ、必要なすべての typer.Typer() を作成し、app.add_typer() でそれらをまとめてください。

次のセクションでこれをさらに多くの機能で更新しますが、すでにコアとなるアイデアは理解できているはずです。

このように、Clickの精神に基づき、**Typer** アプリケーションは構成可能であり、各 typer.Typer() はそれ自体で*CLIアプリ*にできますが、別のTyperアプリにコマンドグループとして追加することもできます。