cli_args 利用ガイド
CLI 引数の定義・検証、コマンドの組み立てと実行、JSON / テキスト出力を使い回すための単一ファイル。
📄 単一ファイル 🧰 標準ライブラリのみ 🐍 Python 3.11+
📥 取得
1 ファイルを置くだけで動きます(pip 不要)。
curl -fsSLO https://raw.githubusercontent.com/myon-bioinformatics/cli_args/main/cli_args.py
▶️ 実行
🤔 タイポの補助
打ち間違いに候補を出します(終了コードは 2 のまま)。
$ python -c 'from cli_args import Argument, parse_args; parse_args([Argument(("--format",), {"choices": ("text", "json")})], ["--fomrat", "json"])'
usage: -c [-h] [--format {text,json}]
-c: error: unrecognized arguments: --fomrat json (did you mean '--format'?)🧾 JSON を整形して出す
標準入力の JSON 値を render_output で出します。
$ echo '{"a": 1}' | python cli_args.py render --format json
{
"a": 1
}🏃 コマンドを実行
標準入力の JSON リクエストからコマンドを実行します(シェルは通しません)。
$ python cli_args.py run --help
🐍 Python から呼ぶ
$ python -c 'from cli_args import Argument, parse_args; print(parse_args([Argument(("--mode",), {"choices": ("read", "convert"), "required": True}), Argument(("files",), {"nargs": "+"})], ["--mode", "read", "a.html"]))'
Namespace(mode='read', files=['a.html'])$ python -c 'from cli_args import Command; print(Command(("git",)).option("-C", "/repo").positional("status").flag("--short").argv)'
('git', '-C', '/repo', 'status', '--short')