python-cli-ops について
Typer と Rich を使った Python コマンドラインアプリ開発向けのスキルです。パターン集、テンプレート、テーマ別リファレンスを提供します。
主な機能
- 01Typer アプリとコマンドグループ
- 02Rich のテーブル、進捗、書式設定
- 03エラー処理パターン
- 04設定と応用リファレンス
- 05再利用可能な CLI アプリテンプレート
活用シーン
- 01Typer ベースの CLI の開始
- 02コマンドグループの追加
- 03テーブルや進捗の表示
- 04設定パターンの参照
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ 0xdarkmatter/ claude-mods --skill python-cli-opsインストール後、Claude Code で/python-cli-opsを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
assets/cli-template.pyreferences/configuration.mdrich-output.mdtyper-patterns.mdscripts/.gitkeep詳細
Python CLI パターン
Typer と Rich を使ったモダンな CLI 開発。
基本的な Typer アプリ
import typer
app = typer.Typer(
name="myapp",
help="My awesome CLI application",
add_completion=True,
)
@app.command()
def hello(
name: str = typer.Argument(..., help="Name to greet"),
count: int = typer.Option(1, "--count", "-c", help="Times to greet"),
loud: bool = typer.Option(False, "--loud", "-l", help="Uppercase"),
):
"""Say hello to someone."""
message = f"Hello, {name}!"
if loud:
message = message.upper()
for _ in range(count):
typer.echo(message)
if __name__ == "__main__":
app()
コマンドグループ
import typer
app = typer.Typer()
users_app = typer.Typer(help="User management commands")
app.add_typer(users_app, name="users")
@users_app.command("list")
def list_users():
"""List all users."""
typer.echo("Listing users...")
@users_app.command("create")
def create_user(name: str, email: str):
"""Create a new user."""
typer.echo(f"Creating user: {name} <{email}>")
@app.command()
def version():
"""Show version."""
typer.echo("1.0.0")
# Usage: myapp users list
# myapp users create "John" "[email protected]"
# myapp version
Rich による出力
from rich.console import Console
from rich.table import Table
from rich.progress import track
from rich.panel import Panel
import typer
console = Console()
@app.command()
def show_users():
"""Display users in a table."""
table = Table(title="Users")
table.add_column("ID", style="cyan")
table.add_column("Name", style="green")
table.add_column("Email")
users = [
(1, "Alice", "[email protected]"),
(2, "Bob", "[email protected]"),
]
for id, name, email in users:
table.add_row(str(id), name, email)
console.print(table)
@app.command()
def process():
"""Process items with progress bar."""
items = list(range(100))
for item in track(items, description="Processing..."):
do_something(item)
console.print("[green]Done![/green]")
エラー処理
import typer
from rich.console import Console
console = Console()
def error(message: str, code: int = 1):
"""Print error and exit."""
console.print(f"[red]Error:[/red] {message}")
raise typer.Exit(code)
@app.command()
def process(file: str):
"""Process a file."""
if not os.path.exists(file):
error(f"File not found: {file}")
try:
result = process_file(file)
console.print(f"[green]Success:[/green] {result}")
except ValueError as e:
error(str(e))
クイックリファレンス
| 機能 | Typer の構文 |
|---|---|
| 必須引数 | name: str |
| オプション引数 | name: str = "default" |
| オプション | typer.Option(default, "--flag", "-f") |
| 引数 | typer.Argument(..., help="...") |
| ブール値フラグ | verbose: bool = False |
| 列挙型の選択肢 | color: Color = Color.red |
| Rich の機能 | 用途 |
|---|---|
| テーブル | Table() + add_column/row |
| 進捗状況 | track(items) |
| 色 | [red]text[/red] |
| パネル | Panel("content", title="Title") |
追加リソース
./references/typer-patterns.md- Typer の高度なパターン./references/rich-output.md- Rich のテーブル、進捗状況、書式設定./references/configuration.md- 設定ファイル、環境変数
アセット
./assets/cli-template.py- CLI アプリケーションの完全なテンプレート
関連項目
関連スキル:
python-typing-ops- CLI 引数の型ヒントpython-observability-ops- CLI アプリケーションのロギング
補完スキル:
python-env- 配布用の CLI パッケージ化
コメント
Markdown 対応