px普贤教程PuXian
普贤教程 › 包参考

cli 包

0.2.0 —— 命令行参数解析(增强版)

项值
包名cli
版本0.2.0
可用版本0.1.0 / 0.2.0
入口cli.px
分类文本与中文处理
文件数 / 行数1 / 315

安装与引入

bash
pxpkg init          # 首次
pxpkg add cli@0.2.0
pxpkg install
px
import cli
所属分类教程:文本与中文处理

公开接口

函数参数
cli_parse_cmdsargs, cmds
cli_helpspec
cli_help_cmdscmds
其余 10 个 def(内部与辅助,可直接调用但不属稳定接口)
函数参数
cli_find_by_shortspec, short
cli_err_msgr
cli_parse_bools
cli_is_int_strs
cli_is_float_strs
cli_castv, t
cli_get_envname
cli_init_defaultsspec
cli_check_constraintsopts, spec
cli_parseargs, spec

包内说明(源码头注释)

text
============================================================
普贤生态库:registry/cli 0.2.0 —— 命令行参数解析(增强版)
------------------------------------------------------------
纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。
对齐 Go flag + cobra 子集心智:

新增:
cli_parse_cmds(args, cmds) → Ok({cmd, opts, args}) | Err
子命令分发:cmds = {"serve": {"spec": ..., "desc": "..."}, ...}
cli_help(spec) → str          自动生成用法帮助
cli_help_cmds(cmds) → str     子命令列表帮助
spec 新增字段:
"env": "VAR_NAME"           环境变量提供默认值(优先级:命令行 > env > default)
"required": true            必须提供(否则 Err)
"choices": [a, b, c]        值必须在候选列表中
"desc": "说明"              用于 help 生成
============================================================
按短名查找 spec 键