typer - タイパー、優れたCLIを構築します。コーディングが簡単。Python の型ヒントに基づきます。

(Typer, build great CLIs. Easy to code. Based on Python type hints.)

Created at: 2019-12-24 20:24:11
Language: Python
License: MIT

タイパー

タイパー、優れたCLIを構築します。コーディングが簡単。Python の型ヒントに基づきます。

試験 著す カバレッジ パッケージバージョン


ドキュメント:https://typer.tiangolo.com

ソースコード:https://github.com/tiangolo/typer


Typerは、ユーザーが使いこなし、開発者が作成するのが好きなCLIアプリケーションを構築するためのライブラリです。Python 3.6+ の型ヒントに基づいています。

主な機能は次のとおりです。

  • 直感的に書く:優れたエディターサポート。どこでも完了。デバッグ時間の短縮。使いやすく、習得しやすいように設計されています。ドキュメントを読む時間が短縮されます。
  • 使いやすい:最終ユーザーにとって使いやすいです。自動ヘルプ、およびすべてのシェルの自動補完。
  • 短い: コードの重複を最小限に抑えます。各パラメーター宣言からの複数の機能。バグが少ない。
  • シンプルに始める: 最も単純な例では、アプリに 2 行のコードを追加するだけです: 1 つのインポート、1 つの関数呼び出し
  • 大きくする:複雑さを好きなだけ増やし、オプションと引数を使用して、コマンドの任意の複雑なツリーとサブコマンドのグループを作成します。

CLI の FastAPI

TyperFastAPIの小さな兄弟です。

そして、それはCLIのFastAPIとなることを意図しています。

必要条件

パイソン3.6+

タイパーは巨人の肩の上に立っています。その唯一の内部依存関係はクリックです。

取り付け

$ pip install "typer[all]"
---> 100%
Successfully installed typer

:これにはリッチが含まれます。Richは端末に情報を表示するための推奨ライブラリであり、オプションですが、インストールすると、Typerに深く統合されて美しい出力が表示されます。

絶対最小値

  • 次のファイルを作成します。
    main.py
import typer


def main(name: str):
    print(f"Hello {name}")


if __name__ == "__main__":
    typer.run(main)

実行する

アプリケーションを実行します。

// Run your application
$ python main.py

// You get a nice error, you are missing NAME
<font color="#F4BF75">Usage: </font>main.py [OPTIONS] NAME
<font color="#A5A5A1">Try </font><font color="#44919F">&apos;main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">&apos;</font><font color="#A5A5A1"> for help.</font>
<font color="#F92672">╭─ Error ───────────────────────────────────────────╮</font>
<font color="#F92672">│</font> Missing argument &apos;NAME&apos;.                          <font color="#F92672">│</font>
<font color="#F92672">╰───────────────────────────────────────────────────╯</font>


// You get a --help for free
$ python main.py --help

<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] NAME                       </b>
<b>                                                     </b>
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font>    name      <font color="#F4BF75"><b>TEXT</b></font>  [default: None] <font color="#A6194C">[required]</font>   │
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font>          Install completion  │
<font color="#A5A5A1">│                               for the current     │</font>
<font color="#A5A5A1">│                               shell.              │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font>             Show completion for │
<font color="#A5A5A1">│                               the current shell,  │</font>
<font color="#A5A5A1">│                               to copy it or       │</font>
<font color="#A5A5A1">│                               customize the       │</font>
<font color="#A5A5A1">│                               installation.       │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font>                        Show this message   │
<font color="#A5A5A1">│                               and exit.           │</font>
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>

// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion

// Now pass the NAME argument
$ python main.py Camila

Hello Camila

// It works! 🎉

: オートコンプリートは、Python パッケージを作成して実行すると、または Typer CLI を使用するときに機能します。

--install-completion

アップグレードの例

これは可能な限り最も単純な例でした。

それでは、もう少し複雑なものを見てみましょう。

2 つのサブコマンドの例

ファイルを変更します。

main.py

aapp を作成し、パラメーターを指定して 2 つのサブコマンドを作成します。

typer.Typer()

import typer

app = typer.Typer()


@app.command()
def hello(name: str):
    print(f"Hello {name}")


@app.command()
def goodbye(name: str, formal: bool = False):
    if formal:
        print(f"Goodbye Ms. {name}. Have a good day.")
    else:
        print(f"Bye {name}!")


if __name__ == "__main__":
    app()

そしてそれは:

  • 明示的にアプリを作成します。
    typer.Typer
    • 前は実際にあなたのために暗黙的に作成します。
      typer.run
  • で 2 つのサブコマンドを追加します。
    @app.command()
  • 実行するそれ自体、まるでそれが(代わりに)関数であるかのように。
    app()
    typer.run

アップグレードされた例を実行する

新しいヘルプを確認します。

$ python main.py --help

<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] COMMAND [ARGS]...          </b>
<b>                                                     </b>
<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font>          Install completion  │
<font color="#A5A5A1">│                               for the current     │</font>
<font color="#A5A5A1">│                               shell.              │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font>             Show completion for │
<font color="#A5A5A1">│                               the current shell,  │</font>
<font color="#A5A5A1">│                               to copy it or       │</font>
<font color="#A5A5A1">│                               customize the       │</font>
<font color="#A5A5A1">│                               installation.       │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font>                        Show this message   │
<font color="#A5A5A1">│                               and exit.           │</font>
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Commands ────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>goodbye                                     </b></font>      │
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>hello                                       </b></font>      │
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>

// You have 2 subcommands (the 2 functions): goodbye and hello

次に、ヘルプを確認してくださいコマンド:

hello

$ python main.py hello --help

<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py hello [OPTIONS] NAME                 </b>
<b>                                                     </b>
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font>    name      <font color="#F4BF75"><b>TEXT</b></font>  [default: None] <font color="#A6194C">[required]</font>   │
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font>          Show this message and exit.       │
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>

そして今、ヘルプをチェックしてくださいコマンド:

goodbye

$ python main.py goodbye --help

<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py goodbye [OPTIONS] NAME               </b>
<b>                                                     </b>
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font>    name      <font color="#F4BF75"><b>TEXT</b></font>  [default: None] <font color="#A6194C">[required]</font>   │
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--formal</b></font>    <font color="#AE81FF"><b>--no-formal</b></font>      [default: no-formal] │
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font>                       Show this message    │
<font color="#A5A5A1">│                              and exit.            │</font>
<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>

// Automatic --formal and --no-formal for the bool option 🎉

これで、新しいコマンドラインアプリケーションを試すことができます。

// Use it with the hello command

$ python main.py hello Camila

Hello Camila

// And with the goodbye command

$ python main.py goodbye Camila

Bye Camila!

// And with --formal

$ python main.py goodbye --formal Camila

Goodbye Ms. Camila. Have a good day.

要約

要約すると、パラメーターのタイプ (CLI 引数CLI オプション) を関数パラメーターとして一度宣言します。

これは、標準の最新の Python 型で行います。

新しい構文、特定のライブラリのメソッドやクラスなどを学ぶ必要はありません。

ちょうど標準的なPython 3.6+

たとえば、次の場合です。

int

total: int

またはフラグの場合:

bool

force: bool

同様に、ファイルパス、列挙型(選択肢)などについても同様です。また、サブコマンドのグループを作成したり、メタデータを追加したり、追加の検証を行ったりするためのツールもあります。

あなたが得る:どこでも補完型チェックを含む素晴らしいエディターサポート。

ユーザーは、パッケージをインストールするとき、またはTyper CLIを使用するときに、ターミナル(Bash、Zsh、Fish、PowerShell)で自動ヘルプオートコンプリートを取得します。

より多くの機能を含むより完全な例については、チュートリアル - ユーザーガイドを参照してください。

オプションの依存関係

Typer は内部的にクリックを使用します。それが唯一の依存関係です。

ただし、エクストラをインストールすることもできます。

  • リッチ:Typerは、適切にフォーマットされたエラーを自動的に表示します。
  • シェリンガム:そしてTyperはインストール完了時に現在のシェルを自動的に検出します。
    • とあなたはただ使うことができます。
      shellingham
      --install-completion
    • なしでは、完了をインストールするシェルの名前を渡す必要があります。
      shellingham
      --install-completion bash

インストールできます とと。

typer
rich
shellingham
pip install typer[all]

ライセンス

このプロジェクトは、MITライセンスの条件に基づいてライセンスされています。