|
11 | 11 | | `OptionValue` | 单个选项取值(flag 计数 + values) | |
12 | 12 | | `ParseError` | 解析错误(kind + message) | |
13 | 13 | | `Argv` | `std::vector<std::string>` 的别名,用于 `parse_from` | |
14 | | -| `Shell` | 目标 shell 枚举:`bash` / `fish` / `zsh` | |
15 | | -| `Command` | 命令树快照,用于生成补全脚本 | |
| 14 | +| `Shell` | 目标 shell 枚举:`bash` / `fish` / `zsh` / `powershell` | |
| 15 | +| `Command` | 命令树快照,补全引擎读取它 | |
16 | 16 |
|
17 | | -解析返回 `ParseResult`,即 `std::expected<ParsedArgs, ParseError>`。`-h`/`--help`、`--version` 时,`ParseError::kind` 分别为 `help` / `version`,不携带 `message`,通过 `is_error()` 可区分真实错误。 |
| 17 | +解析返回 `ParseResult`,即 `std::expected<ParsedArgs, ParseError>`。`-h`/`--help`、`--version` 时,`ParseError::kind` 分别为 `help` / `version`(补全见“Shell 补全”,为 `completion`),不携带 `message`,通过 `is_error()` 可区分真实错误。 |
18 | 18 |
|
19 | 19 | --- |
20 | 20 |
|
|
36 | 36 |
|
37 | 37 | ## Arg / Option |
38 | 38 |
|
39 | | -**Arg**:`.help(s)` `.required(bool)` `.default_value(s)` |
| 39 | +**Arg**:`.help(s)` `.required(bool)` `.default_value(s)` `.repeatable(bool)` `.values({...})` `.value_hint(h)` `.complete(fn)` |
40 | 40 |
|
41 | | -**Option**:`.short_name(c)` `.long_opt(s)` `.help(s)` `.takes_value(bool)` `.value_name(s)` `.multiple(bool)` `.global(bool)` |
| 41 | +**Option**:`.short_name(c)` `.long_opt(s)` `.help(s)` `.takes_value(bool)` `.value_name(s)` `.multiple(bool)` `.global(bool)` `.values({...})` `.value_hint(h)` `.complete(fn)` |
| 42 | + |
| 43 | +**App**(子命令):`.hidden(bool)`;根命令:`.completion(options)`(见“Shell 补全”) |
42 | 44 |
|
43 | 45 | --- |
44 | 46 |
|
|
48 | 50 | |------|------| |
49 | 51 | | `shell_from_string(sv)` | 字符串 → `std::optional<Shell>`(如 `"fish"` → `Shell::fish`) | |
50 | 52 | | `to_string(shell)` | `Shell` → `std::string_view` | |
51 | | -| `shell_supported(shell)` | 该 shell 补全是否已实现(fish / bash / zsh) | |
| 53 | +| `shell_supported(shell)` | 该 shell 补全是否已实现(bash / zsh / fish / powershell) | |
52 | 54 |
|
53 | 55 | ## ParseError |
54 | 56 |
|
55 | 57 | | 成员 | 说明 | |
56 | 58 | |------|------| |
57 | | -| `kind` | 枚举:`help` / `version` / `error` | |
| 59 | +| `kind` | 枚举:`help` / `version` / `completion` / `error` | |
58 | 60 | | `message` | 错误文本(仅 `kind == error` 时有内容) | |
59 | 61 | | `.is_error()` | `kind == error` 时返回 `true` | |
60 | 62 |
|
@@ -206,63 +208,43 @@ app.subcommand(cmdline::App("add").description("Add").arg(cmdline::Arg("x").requ |
206 | 208 |
|
207 | 209 | ## Shell 补全 |
208 | 210 |
|
209 | | -### 生成流程 |
210 | | - |
211 | | -1. 从 `App` 构建 `completions::Command` 树(`snapshot(app)` 或自动由 `generate_completions` 调用)。`snapshot` 是导出的函数(自 0.0.5): |
212 | | - 补全脚本、手册页与工具对自身命令和选项的列举都由同一棵树生成 |
213 | | -2. `Command` 包含名称、描述、版本、`Arg`/`Option` 列表、子命令列表 |
214 | | -3. 选择目标 `Shell` 后调用 `generate_completions`,输出补全脚本文本 |
| 211 | +### 一次调用开启 |
215 | 212 |
|
216 | | -### completions::Command |
| 213 | +```cpp |
| 214 | +(void)app.completion(); // 缺省:`completions <shell>` 与隐藏入口 `__complete` |
| 215 | +(void)app.completion({.command = "", .entry = "__complete"}); // 不加可见命令,脚本由程序自己写出 |
| 216 | +``` |
217 | 217 |
|
218 | | -| 成员 | 说明 | |
219 | | -|------|------| |
220 | | -| `name` | 命令名 | |
221 | | -| `description` | 描述文本 | |
222 | | -| `version` | 版本号 | |
223 | | -| `args` / `options` / `subcommands` | `vector<Arg>` / `vector<Option>` / `vector<Command>` | |
224 | | -| `.has_positionals()` | 是否有位置参数 | |
225 | | -| `.has_subcommands()` | 是否有子命令 | |
| 218 | +开启之后: |
226 | 219 |
|
227 | | -> `snapshot()` 构建的 `Command` 会自动包含 `-h`/`--help` 和 `--version` 选项(与 `App::parse_impl` 行为一致)。 |
| 220 | +- `myapp completions bash`(`zsh`、`fish`、`powershell`)打印该 shell 的适配脚本。脚本只有几行,内容是把光标前的词交给 |
| 221 | + `myapp __complete …` 并列出回答,不含命令树;程序新增命令之后,用户无需重新安装脚本。 |
| 222 | +- `myapp __complete <words…>` 是隐藏入口:以命令树、解析所用的同一套词法规则(`completions::classify`)回答最后一个词 |
| 223 | + 可以补成什么。回答是文本,每行一个候选(`值<TAB>说明`),最后一行 `:<指令>`:`:0` 无附加,`:1` 由 shell 补文件, |
| 224 | + `:2` 由 shell 补目录。 |
| 225 | +- 两者都在 `--help` 的位置处理,解析返回 `ParseError`,其 `kind` 为 `completion`,`is_error()` 为 `false`。 |
| 226 | +- 不调用 `completion()` 时,行为与此前一致。 |
228 | 227 |
|
229 | | -### 生成函数 |
| 228 | +### 补全读取的事实 |
230 | 229 |
|
231 | | -| 重载 | 说明 | |
232 | | -|------|------| |
233 | | -| `generate_completions(cmd, shell)` | `Command` → `std::string` | |
234 | | -| `generate_completions(cmd, shell, out)` | `Command` → `std::ostream&` | |
235 | | -| `generate_completions(app, shell)` | `App` → `std::string`(内部调用 snapshot) | |
236 | | -| `generate_completions(app, shell, out)` | `App` → `std::ostream&` | |
| 230 | +| 位置 | 方法 | 含义 | |
| 231 | +|------|------|------| |
| 232 | +| `Option`、`Arg` | `.values({...})` | 可选值的封闭集合:解析校验它,帮助与补全列出它;`Option` 由此带值 | |
| 233 | +| `Option`、`Arg` | `.value_hint(ValueHint::file / directory)` | 值是文件或目录,由 shell 补全 | |
| 234 | +| `Option`、`Arg` | `.complete(fn)` | 候选回调 `std::vector<std::string>(std::string_view prefix)`,只在补全时调用 | |
| 235 | +| `Arg` | `.repeatable()` | 该位置参数代表其后的每个位置参数 | |
| 236 | +| `App`(子命令) | `.hidden()` | 不出现在帮助与补全中,照常解析与执行 | |
237 | 237 |
|
238 | | -### App::completions |
| 238 | +### 引擎与适配器 |
239 | 239 |
|
240 | | -```cpp |
241 | | -// App 上的便捷方法,等价于 generate_completions(*this, shell) |
242 | | -std::string completions(Shell shell) const; |
243 | | -``` |
244 | | -
|
245 | | -### 示例:添加 completions 子命令 |
| 240 | +| 函数 | 说明 | |
| 241 | +|------|------| |
| 242 | +| `completions::complete(cmd, words)` | 纯函数:命令树与光标前的词 → `Answer`(候选与指令) | |
| 243 | +| `completions::render(answer)` | `Answer` → 上述文本协议 | |
| 244 | +| `completions::adapter(shell, program, entry)` | 某个 shell 的适配脚本 | |
| 245 | +| `completions::classify(word, isSubcommand)` | 一个词对一个命令的含义:`--`、长选项(含 `--k=v`)、短选项、子命令、位置参数;解析与补全共用 | |
| 246 | +| `snapshot(app)` | 命令树快照(自 0.0.5):每个命令含自己的 `-h`/`--help`,有版本的命令含 `--version`;隐藏命令不在其中 | |
246 | 247 |
|
247 | | -```cpp |
248 | | -app.subcommand("completions") |
249 | | - .description("Generate shell completions") |
250 | | - .option(Option("shell").takes_value().value_name("SHELL") |
251 | | - .help("Target shell [possible values: bash, fish, zsh]")) |
252 | | - .action([&app](const ParsedArgs& p) -> int { |
253 | | - auto shell_opt = p.value("shell"); |
254 | | - if (!shell_opt) { |
255 | | - std::println(std::cerr, "SHELL not provided."); |
256 | | - return 2; |
257 | | - } |
258 | | - auto shell = shell_from_string(*shell_opt); |
259 | | - if (!shell || !shell_supported(*shell)) { |
260 | | - std::println(std::cerr, "Unsupported shell: {}", *shell_opt); |
261 | | - return 2; |
262 | | - } |
263 | | - std::print("{}", app.completions(*shell)); |
264 | | - return 0; |
265 | | - }); |
266 | | -``` |
| 248 | +`generate_completions(cmd|app, shell[, out])` 与 `App::completions(shell)` 的签名不变,自 0.0.6 起输出适配脚本,而不是 |
| 249 | +写出整棵命令树的脚本。 |
267 | 250 |
|
268 | | -> 完整可运行示例见 `examples/completions/`。 |
0 commit comments