コマンドヘルプ
以前と同様に、ドキュメント文字列とCLIオプションにコマンドのヘルプを追加できます。
そして、typer.Typer()
アプリケーションは、CLIプログラムのメインヘルプテキストとして渡すことができるパラメータhelp
を受け取ります。
import typer
from typing_extensions import Annotated
app = typer.Typer(help="Awesome CLI user manager.")
@app.command()
def create(username: str):
"""
Create a new user with USERNAME.
"""
print(f"Creating user: {username}")
@app.command()
def delete(
username: str,
force: Annotated[
bool,
typer.Option(
prompt="Are you sure you want to delete the user?",
help="Force deletion without confirmation.",
),
],
):
"""
Delete a user with USERNAME.
If --force is not used, will ask for confirmation.
"""
if force:
print(f"Deleting user: {username}")
else:
print("Operation cancelled")
@app.command()
def delete_all(
force: Annotated[
bool,
typer.Option(
prompt="Are you sure you want to delete ALL users?",
help="Force deletion without confirmation.",
),
],
):
"""
Delete ALL users in the database.
If --force is not used, will ask for confirmation.
"""
if force:
print("Deleting all users")
else:
print("Operation cancelled")
@app.command()
def init():
"""
Initialize the users database.
"""
print("Initializing user database")
if __name__ == "__main__":
app()
ヒント
可能な場合は、Annotated
バージョンを使用することをお勧めします。
import typer
app = typer.Typer(help="Awesome CLI user manager.")
@app.command()
def create(username: str):
"""
Create a new user with USERNAME.
"""
print(f"Creating user: {username}")
@app.command()
def delete(
username: str,
force: bool = typer.Option(
...,
prompt="Are you sure you want to delete the user?",
help="Force deletion without confirmation.",
),
):
"""
Delete a user with USERNAME.
If --force is not used, will ask for confirmation.
"""
if force:
print(f"Deleting user: {username}")
else:
print("Operation cancelled")
@app.command()
def delete_all(
force: bool = typer.Option(
...,
prompt="Are you sure you want to delete ALL users?",
help="Force deletion without confirmation.",
),
):
"""
Delete ALL users in the database.
If --force is not used, will ask for confirmation.
"""
if force:
print("Deleting all users")
else:
print("Operation cancelled")
@app.command()
def init():
"""
Initialize the users database.
"""
print("Initializing user database")
if __name__ == "__main__":
app()
確認してください
// Check the new help
$ python main.py --help
Usage: main.py [OPTIONS] COMMAND [ARGS]...
Awesome CLI user manager.
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:
create Create a new user with USERNAME.
delete Delete a user with USERNAME.
delete-all Delete ALL users in the database.
init Initialize the users database.
// Now the commands have inline help 🎉
// Check the help for create
$ python main.py create --help
Usage: main.py create [OPTIONS] USERNAME
Create a new user with USERNAME.
Options:
--help Show this message and exit.
// Check the help for delete
$ python main.py delete --help
Usage: main.py delete [OPTIONS] USERNAME
Delete a user with USERNAME.
If --force is not used, will ask for confirmation.
Options:
--force / --no-force Force deletion without confirmation. [required]
--help Show this message and exit.
// Check the help for delete-all
$ python main.py delete-all --help
Usage: main.py delete-all [OPTIONS]
Delete ALL users in the database.
If --force is not used, will ask for confirmation.
Options:
--force / --no-force Force deletion without confirmation. [required]
--help Show this message and exit.
// Check the help for init
$ python main.py init --help
Usage: main.py init [OPTIONS]
Initialize the users database.
Options:
--help Show this message and exit.
ヒント
typer.Typer()
は、他の目的のための他のいくつかのパラメータを受け取ります。それについては後で説明します。
後で「コールバック」の使用方法についても説明します。これには、関数ドキュメント文字列にこの同じヘルプメッセージを追加する方法が含まれます。
コマンドヘルプのオーバーライド¶
おそらく、関数にドキュメント文字列としてヘルプテキストを追加する方が良いでしょうが、何らかの理由でオーバーライドしたい場合は、@app.command()
に渡されるhelp
関数引数を使用できます。
import typer
app = typer.Typer()
@app.command(help="Create a new user with USERNAME.")
def create(username: str):
"""
Some internal utility function to create.
"""
print(f"Creating user: {username}")
@app.command(help="Delete a user with USERNAME.")
def delete(username: str):
"""
Some internal utility function to delete.
"""
print(f"Deleting user: {username}")
if __name__ == "__main__":
app()
確認してください
// Check the help
$ python main.py --help
// Notice it uses the help passed to @app.command()
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:
create Create a new user with USERNAME.
delete Delete a user with USERNAME.
// It uses "Create a new user with USERNAME." instead of "Some internal utility function to create."
コマンドの非推奨化¶
アプリに、非推奨にする必要があるコマンドがある場合があります。これにより、しばらくの間サポートされていても、ユーザーが使用を停止するようにします。
パラメータdeprecated=True
でマークできます。
import typer
app = typer.Typer()
@app.command()
def create(username: str):
"""
Create a user.
"""
print(f"Creating user: {username}")
@app.command(deprecated=True)
def delete(username: str):
"""
Delete a user.
This is deprecated and will stop being supported soon.
"""
print(f"Deleting user: {username}")
if __name__ == "__main__":
app()
そして、--help
オプションを表示すると、「deprecated
」とマークされていることがわかります。
$ 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 for the current │
<font color="#A5A5A1">│ shell. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for the current │
<font color="#A5A5A1">│ shell, to copy it or customize the │</font>
<font color="#A5A5A1">│ installation. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Commands ────────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>create </b></font> Create a user. │
<font color="#A5A5A1">│ </font><font color="#6B9F98"><b>delete </b></font> Delete a user. <font color="#F92672">(deprecated) </font> │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
また、非推奨のコマンド(この例では、delete
コマンド)の--help
を確認すると、非推奨として表示されます。
$ python main.py delete --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py delete [OPTIONS] USERNAME </b>
<b> </b>
<font color="#F92672">(deprecated) </font>
Delete a user.
This is deprecated and will stop being supported soon.
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font> username <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>
リッチマークダウンとマークアップ¶
印刷と色で説明されているように、Richがインストールされている場合は、パラメータrich_markup_mode
を使用してマークアップテキストを有効にするようにアプリを構成できます。
次に、ドキュメント文字列と、CLI引数およびCLIオプションのhelp
パラメータで、より多くの書式設定を使用できます。以下で詳しく説明します。👇
情報
デフォルトでは、rich_markup_mode
はNone
であり、リッチテキストの書式設定が無効になります。
リッチマークアップ¶
typer.Typer()
アプリを作成するときにrich_markup_mode="rich"
を設定すると、ドキュメント文字列でRich Console Markupを使用できるようになります。また、CLI引数およびオプションのヘルプでも使用できます。
import typer
from typing_extensions import Annotated
app = typer.Typer(rich_markup_mode="rich")
@app.command()
def create(
username: Annotated[
str, typer.Argument(help="The username to be [green]created[/green]")
],
):
"""
[bold green]Create[/bold green] a new [italic]shinny[/italic] user. :sparkles:
This requires a [underline]username[/underline].
"""
print(f"Creating user: {username}")
@app.command(help="[bold red]Delete[/bold red] a user with [italic]USERNAME[/italic].")
def delete(
username: Annotated[
str, typer.Argument(help="The username to be [red]deleted[/red]")
],
force: Annotated[
bool, typer.Option(help="Force the [bold red]deletion[/bold red] :boom:")
] = False,
):
"""
Some internal utility function to delete.
"""
print(f"Deleting user: {username}")
if __name__ == "__main__":
app()
ヒント
可能な場合は、Annotated
バージョンを使用することをお勧めします。
import typer
app = typer.Typer(rich_markup_mode="rich")
@app.command()
def create(
username: str = typer.Argument(
..., help="The username to be [green]created[/green]"
),
):
"""
[bold green]Create[/bold green] a new [italic]shiny[/italic] user. :sparkles:
This requires a [underline]username[/underline].
"""
print(f"Creating user: {username}")
@app.command(help="[bold red]Delete[/bold red] a user with [italic]USERNAME[/italic].")
def delete(
username: str = typer.Argument(..., help="The username to be [red]deleted[/red]"),
force: bool = typer.Option(
False, help="Force the [bold red]deletion[/bold red] :boom:"
),
):
"""
Some internal utility function to delete.
"""
print(f"Deleting user: {username}")
if __name__ == "__main__":
app()
これにより、Rich Console Markupを使用して、コマンドcreate
のドキュメント文字列のテキストをフォーマットし、単語「create
」を太字と緑色にし、絵文字を使用することもできます。
username
CLI引数のヘルプでもマークアップを使用できます。
そして以前と同様に、コマンドdelete
にオーバーライドされたヘルプテキストもRich Markupを使用できます。CLI引数とCLIオプションでも同じです。
プログラムを実行してヘルプを確認すると、Typerが内部的にRichを使用してヘルプをフォーマットすることがわかります。
create
コマンドのヘルプを確認してください。
$ python main.py create --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py create [OPTIONS] USERNAME </b>
<b> </b>
<font color="#A6E22E"><b>Create</b></font> a new <i>shiny</i> user. ✨
This requires a <font color="#A5A5A1"><u style="text-decoration-style:single">username</u></font><font color="#A5A5A1">. </font>
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font> username <font color="#F4BF75"><b>TEXT</b></font> The username to be <font color="#A6E22E">created</font> │
<font color="#A5A5A1">│ [default: None] │</font>
<font color="#A5A5A1">│ </font><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>
delete
コマンドのヘルプを確認してください。
$ python main.py delete --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py delete [OPTIONS] USERNAME </b>
<b> </b>
<font color="#F92672"><b>Delete</b></font> a user with <i>USERNAME</i>.
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font> username <font color="#F4BF75"><b>TEXT</b></font> The username to be <font color="#F92672">deleted</font> │
<font color="#A5A5A1">│ [default: None] │</font>
<font color="#A5A5A1">│ </font><font color="#A6194C">[required] </font> │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--force</b></font> <font color="#AE81FF"><b>--no-force</b></font> Force the <font color="#F92672"><b>deletion</b></font> 💥 │
<font color="#A5A5A1">│ [default: no-force] │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
リッチマークダウン¶
typer.Typer()
アプリを作成するときにrich_markup_mode="markdown"
を設定すると、ドキュメント文字列でマークダウンを使用できるようになります。
import typer
from typing_extensions import Annotated
app = typer.Typer(rich_markup_mode="markdown")
@app.command()
def create(
username: Annotated[str, typer.Argument(help="The username to be **created**")],
):
"""
**Create** a new *shinny* user. :sparkles:
* Create a username
* Show that the username is created
---
Learn more at the [Typer docs website](https://typer.dokyumento.jp)
"""
print(f"Creating user: {username}")
@app.command(help="**Delete** a user with *USERNAME*.")
def delete(
username: Annotated[str, typer.Argument(help="The username to be **deleted**")],
force: Annotated[bool, typer.Option(help="Force the **deletion** :boom:")] = False,
):
"""
Some internal utility function to delete.
"""
print(f"Deleting user: {username}")
if __name__ == "__main__":
app()
ヒント
可能な場合は、Annotated
バージョンを使用することをお勧めします。
import typer
app = typer.Typer(rich_markup_mode="markdown")
@app.command()
def create(username: str = typer.Argument(..., help="The username to be **created**")):
"""
**Create** a new *shiny* user. :sparkles:
* Create a username
* Show that the username is created
---
Learn more at the [Typer docs website](https://typer.dokyumento.jp)
"""
print(f"Creating user: {username}")
@app.command(help="**Delete** a user with *USERNAME*.")
def delete(
username: str = typer.Argument(..., help="The username to be **deleted**"),
force: bool = typer.Option(False, help="Force the **deletion** :boom:"),
):
"""
Some internal utility function to delete.
"""
print(f"Deleting user: {username}")
if __name__ == "__main__":
app()
これにより、マークダウンを使用して、コマンドcreate
のドキュメント文字列のテキストをフォーマットし、単語「create
」を太字にし、項目のリストを表示し、絵文字を使用することもできます。
そして以前と同様に、コマンドdelete
にオーバーライドされたヘルプテキストもマークダウンを使用できます。
create
コマンドのヘルプを確認してください。
$ python main.py create --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py create [OPTIONS] USERNAME </b>
<b> </b>
<b>Create</b> a new <i>shiny</i> user. ✨
<font color="#F4BF75"><b> • </b></font><font color="#A5A5A1">Create a username </font>
<font color="#F4BF75"><b> • </b></font><font color="#A5A5A1">Show that the username is created </font>
<font color="#F4BF75">───────────────────────────────────────────────────────────────────</font>
Learn more at the <font color="#44919F">Typer docs website</font>
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font> username <font color="#F4BF75"><b>TEXT</b></font> The username to be <b>created</b> │
<font color="#A5A5A1">│ [default: None] │</font>
<font color="#A5A5A1">│ </font><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>
delete
コマンドでも同様です。
$ python main.py delete --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py delete [OPTIONS] USERNAME </b>
<b> </b>
<b>Delete</b> a user with <i>USERNAME</i>.
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font> username <font color="#F4BF75"><b>TEXT</b></font> The username to be <b>deleted</b> │
<font color="#A5A5A1">│ [default: None] │</font>
<font color="#A5A5A1">│ </font><font color="#A6194C">[required] </font> │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--force</b></font> <font color="#AE81FF"><b>--no-force</b></font> Force the <b>deletion</b> 💥 │
<font color="#A5A5A1">│ [default: no-force] │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
情報
マークダウンでは色を定義できないことに注意してください。色については、Richマークアップを使用することをお勧めします。
ヘルプパネル¶
多くのコマンドまたはCLIパラメータがある場合は、--help
オプションを使用するときに、ドキュメントを異なるパネルに表示することができます。
Richを印刷と色で説明されているようにインストールした場合は、各コマンドまたはCLIパラメータに使用するパネルを構成できます。
コマンドのヘルプパネル¶
コマンドのパネルを設定するには、使用するパネルの名前で引数rich_help_panel
を渡すことができます。
import typer
app = typer.Typer(rich_markup_mode="rich")
@app.command()
def create(username: str):
"""
[green]Create[/green] a new user. :sparkles:
"""
print(f"Creating user: {username}")
@app.command()
def delete(username: str):
"""
[red]Delete[/red] a user. :fire:
"""
print(f"Deleting user: {username}")
@app.command(rich_help_panel="Utils and Configs")
def config(configuration: str):
"""
[blue]Configure[/blue] the system. :wrench:
"""
print(f"Configuring the system with: {configuration}")
@app.command(rich_help_panel="Utils and Configs")
def sync():
"""
[blue]Synchronize[/blue] the system or something fancy like that. :recycle:
"""
print("Syncing the system")
@app.command(rich_help_panel="Help and Others")
def help():
"""
Get [yellow]help[/yellow] with the system. :question:
"""
print("Opening help portal...")
@app.command(rich_help_panel="Help and Others")
def report():
"""
[yellow]Report[/yellow] an issue. :bug:
"""
print("Please open a new issue online, not a direct message")
if __name__ == "__main__":
app()
パネルのないコマンドはデフォルトのCommands
パネルに表示され、残りは次のパネルに表示されます。
$ 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 for the current │
<font color="#A5A5A1">│ shell. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for the current │
<font color="#A5A5A1">│ shell, to copy it or customize the │</font>
<font color="#A5A5A1">│ installation. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Commands ────────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>create </b></font> <font color="#A6E22E">Create</font> a new user. ✨ │
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>delete </b></font> <font color="#F92672">Delete</font> a user. 🔥 │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Utils and Configs ───────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>config </b></font> <font color="#66D9EF">Configure</font> the system. 🔧 │
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>sync </b></font> <font color="#66D9EF">Synchronize</font> the system or something fancy like that. ♻ │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Help and Others ─────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>help </b></font> Get <font color="#F4BF75">help</font> with the system. ❓ │
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>report </b></font> <font color="#F4BF75">Report</font> an issue. 🐛 │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
CLIパラメータのヘルプパネル¶
同様に、rich_help_panel
を使用して、CLI引数およびCLIオプションのパネルを構成できます。
そしてもちろん、同じアプリケーションで、コマンドのrich_help_panel
も設定できます。
from typing import Union
import typer
from typing_extensions import Annotated
app = typer.Typer(rich_markup_mode="rich")
@app.command()
def create(
username: Annotated[str, typer.Argument(help="The username to create")],
lastname: Annotated[
str,
typer.Argument(
help="The last name of the new user", rich_help_panel="Secondary Arguments"
),
] = "",
force: Annotated[bool, typer.Option(help="Force the creation of the user")] = False,
age: Annotated[
Union[int, None],
typer.Option(help="The age of the new user", rich_help_panel="Additional Data"),
] = None,
favorite_color: Annotated[
Union[str, None],
typer.Option(
help="The favorite color of the new user",
rich_help_panel="Additional Data",
),
] = None,
):
"""
[green]Create[/green] a new user. :sparkles:
"""
print(f"Creating user: {username}")
@app.command(rich_help_panel="Utils and Configs")
def config(configuration: str):
"""
[blue]Configure[/blue] the system. :wrench:
"""
print(f"Configuring the system with: {configuration}")
if __name__ == "__main__":
app()
ヒント
可能な場合は、Annotated
バージョンを使用することをお勧めします。
from typing import Union
import typer
app = typer.Typer(rich_markup_mode="rich")
@app.command()
def create(
username: str = typer.Argument(..., help="The username to create"),
lastname: str = typer.Argument(
"", help="The last name of the new user", rich_help_panel="Secondary Arguments"
),
force: bool = typer.Option(False, help="Force the creation of the user"),
age: Union[int, None] = typer.Option(
None, help="The age of the new user", rich_help_panel="Additional Data"
),
favorite_color: Union[str, None] = typer.Option(
None,
help="The favorite color of the new user",
rich_help_panel="Additional Data",
),
):
"""
[green]Create[/green] a new user. :sparkles:
"""
print(f"Creating user: {username}")
@app.command(rich_help_panel="Utils and Configs")
def config(configuration: str):
"""
[blue]Configure[/blue] the system. :wrench:
"""
print(f"Configuring the system with: {configuration}")
if __name__ == "__main__":
app()
次に、アプリケーションを実行すると、すべてのCLIパラメータがそれぞれのパネルに表示されます。
- まず、パネル名が設定されていないCLI引数が「
Arguments
」という名前のデフォルトのパネルに表示されます。 - 次に、カスタムパネルを持つCLI引数です。この例では、「
Secondary Arguments
」という名前です。 - その後、パネルのないCLIオプションが「
Options
」という名前のデフォルトのパネルに表示されます。 - そして最後に、カスタムパネルが設定されたCLIオプションです。この例では、「
Additional Data
」という名前です。
create
コマンドの--help
オプションを確認できます。
$ python main.py create --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py create [OPTIONS] USERNAME [LASTNAME] </b>
<b> </b>
<font color="#A6E22E">Create</font> a new user. ✨
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font> username <font color="#F4BF75"><b>TEXT</b></font> The username to create [default: None] │
<font color="#A5A5A1">│ </font><font color="#A6194C">[required] </font> │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Secondary Arguments ─────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ lastname </font><font color="#A37F4E"><b>[LASTNAME]</b></font> The last name of the new user │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--force</b></font> <font color="#AE81FF"><b>--no-force</b></font> Force the creation of the user │
<font color="#A5A5A1">│ [default: no-force] │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Additional Data ─────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--age</b></font> <font color="#F4BF75"><b>INTEGER</b></font> The age of the new user │
<font color="#A5A5A1">│ [default: None] │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--favorite-color</b></font> <font color="#F4BF75"><b>TEXT </b></font> The favorite color of the new │
<font color="#A5A5A1">│ user │</font>
<font color="#A5A5A1">│ [default: None] │</font>
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
そしてもちろん、rich_help_panel
は同じアプリケーション内のコマンドでも同じように使用できます。
そして、これらのパネルは、メインの--help
オプションを使用すると表示されます。
$ 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 for the current │
<font color="#A5A5A1">│ shell. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for the current │
<font color="#A5A5A1">│ shell, to copy it or customize the │</font>
<font color="#A5A5A1">│ installation. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Commands ────────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>create </b></font> <font color="#A6E22E">Create</font> a new user. ✨ │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#A5A5A1">╭─ Utils and Configs ───────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>config </b></font> <font color="#66D9EF">Configure</font> the system. 🔧 │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
「Utils and Configs
」のコマンドのカスタムパネルを確認できます。
エピローグ¶
必要に応じて、コマンドのヘルプにエピローグセクションを追加することもできます。
import typer
app = typer.Typer(rich_markup_mode="rich")
@app.command(epilog="Made with :heart: in [blue]Venus[/blue]")
def create(username: str):
"""
[green]Create[/green] a new user. :sparkles:
"""
print(f"Creating user: {username}")
if __name__ == "__main__":
app()
そして、--help
オプションを確認すると、次のようになります。
$ python main.py --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] USERNAME </b>
<b> </b>
<font color="#A6E22E">Create</font> a new user. ✨
<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
<font color="#A5A5A1">│ </font><font color="#F92672">*</font> username <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 for the current │
<font color="#A5A5A1">│ shell. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for the current │
<font color="#A5A5A1">│ shell, to copy it or customize the │</font>
<font color="#A5A5A1">│ installation. │</font>
<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │
<font color="#A5A5A1">╰───────────────────────────────────────────────────────────────────╯</font>
Made with ❤ in <font color="#66D9EF">Venus</font>