普贤教程 › 包参考
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_cmds | args, cmds |
cli_help | spec |
cli_help_cmds | cmds |
其余 10 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
cli_find_by_short | spec, short |
cli_err_msg | r |
cli_parse_bool | s |
cli_is_int_str | s |
cli_is_float_str | s |
cli_cast | v, t |
cli_get_env | name |
cli_init_defaults | spec |
cli_check_constraints | opts, spec |
cli_parse | args, 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 键