タイパー、優れたCLIを構築します。コーディングが簡単。Python の型ヒントに基づきます。
ドキュメント:https://typer.tiangolo.com
ソースコード:https://github.com/tiangolo/typer
Typerは、ユーザーが使いこなし、開発者が作成するのが好きなCLIアプリケーションを構築するためのライブラリです。Python 3.6+ の型ヒントに基づいています。
主な機能は次のとおりです。
TyperはFastAPIの小さな兄弟です。
そして、それは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">'main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">'</font><font color="#A5A5A1"> for help.</font>
<font color="#F92672">╭─ Error ───────────────────────────────────────────╮</font>
<font color="#F92672">│</font> Missing argument 'NAME'. <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
これは可能な限り最も単純な例でした。
それでは、もう少し複雑なものを見てみましょう。
ファイルを変更します。
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
@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ライセンスの条件に基づいてライセンスされています。