SuperAPI AI 工具集

argparse-patterns

通用与其它

查看项目

更新于 10-16 06:43

技能介绍

用途:提供仅使用 Python 标准库 argparse 的命令行参数解析模式,用于构建无需外部依赖的 Python CLI。

主要能力:基础解析器设置(description、action='version'、formatter_class、parse_args);子命令与嵌套子命令(add_subparsers、add_parser,支持多级如 mycli config get key);choices 与自定义 type 校验;action 模式(store_true/store_false、store_const、append、count、version 及自定义 Action 子类);位置参数与可选参数、type 强制转换(int、float、pathlib.Path)、nargs(?、*、+);互斥组与参数分组。

模板与脚本:提供 basic-parser.py、subparser-pattern.py、nested-subparser.py、choices-validation.py、boolean-flags.py、custom-actions.py、mutually-exclusive.py、argument-groups.py、type-coercion.py、variadic-args.py 等 Python 模板,以及 argparse-to-commander.ts、argparse-to-yargs.ts、parser-comparison.ts 等 TypeScript 对照模板;脚本包括 generate-parser.sh、validate-parser.sh、test-parser.sh、convert-to-click.sh。

使用方式:按模板编写解析器,用 add_argument 定义参数,用 add_subparsers 组织命令,用 type 函数做校验,用 add_mutually_exclusive_group 与 add_argument_group 组织选项。

适用场景:简单到中等复杂度的 CLI、需避免外部依赖、受限环境、学习 CLI 模式。若需装饰器语法可选 Click/Typer,需类型安全与自动补全可选 Typer,快速原型可选 Fire。

限制:要求 Python 3.7+,无外部依赖,跨平台;资料未提供具体安装命令、收费信息或性能数据。