Skip to content

Commit a2eb9fb

Browse files
authored
0.0.6: completion is built in: one engine answers from the command tree, and a shell's adapter only asks it (#13)
Closes #9.
1 parent 87193ee commit a2eb9fb

13 files changed

Lines changed: 664 additions & 1403 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
- **多种解析模式** — 支持 `argc/argv`、`cmdline::Argv`(即 `vector<string>`)或原始 `string_view`
2222

23+
- **Shell 补全** — `app.completion()` 一次调用开启;程序以自己的命令树回答(`__complete`),bash、zsh、fish、PowerShell 的适配脚本只有几行,不含命令树
2324
- **数值选项** — `value_type<T>()` 声明选项取 `T` 类型的数字,解析时用 `std::from_chars` 校验,不合法就是解析错误;`value_as<T>(name)` 读回
2425

2526
## 快速开始

‎docs/api.md‎

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
| `OptionValue` | 单个选项取值(flag 计数 + values) |
1212
| `ParseError` | 解析错误(kind + message) |
1313
| `Argv` | `std::vector<std::string>` 的别名,用于 `parse_from` |
14-
| `Shell` | 目标 shell 枚举:`bash` / `fish` / `zsh` |
15-
| `Command` | 命令树快照,用于生成补全脚本 |
14+
| `Shell` | 目标 shell 枚举:`bash` / `fish` / `zsh` / `powershell` |
15+
| `Command` | 命令树快照,补全引擎读取它 |
1616

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()` 可区分真实错误。
1818

1919
---
2020

@@ -36,9 +36,11 @@
3636

3737
## Arg / Option
3838

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)`
4040

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 补全”)
4244

4345
---
4446

@@ -48,13 +50,13 @@
4850
|------|------|
4951
| `shell_from_string(sv)` | 字符串 → `std::optional<Shell>`(如 `"fish"` → `Shell::fish`) |
5052
| `to_string(shell)` | `Shell` → `std::string_view` |
51-
| `shell_supported(shell)` | 该 shell 补全是否已实现(fish / bash / zsh) |
53+
| `shell_supported(shell)` | 该 shell 补全是否已实现(bash / zsh / fish / powershell) |
5254

5355
## ParseError
5456

5557
| 成员 | 说明 |
5658
|------|------|
57-
| `kind` | 枚举:`help` / `version` / `error` |
59+
| `kind` | 枚举:`help` / `version` / `completion` / `error` |
5860
| `message` | 错误文本(仅 `kind == error` 时有内容) |
5961
| `.is_error()` | `kind == error` 时返回 `true` |
6062

@@ -206,63 +208,43 @@ app.subcommand(cmdline::App("add").description("Add").arg(cmdline::Arg("x").requ
206208

207209
## Shell 补全
208210

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+
### 一次调用开启
215212

216-
### completions::Command
213+
```cpp
214+
(void)app.completion(); // 缺省:`completions <shell>` 与隐藏入口 `__complete`
215+
(void)app.completion({.command = "", .entry = "__complete"}); // 不加可见命令,脚本由程序自己写出
216+
```
217217
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+
开启之后:
226219
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()` 时,行为与此前一致。
228227
229-
### 生成函数
228+
### 补全读取的事实
230229
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()` | 不出现在帮助与补全中,照常解析与执行 |
237237
238-
### App::completions
238+
### 引擎与适配器
239239
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`;隐藏命令不在其中 |
246247
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+
写出整棵命令树的脚本。
267250
268-
> 完整可运行示例见 `examples/completions/`。

‎examples/completions/src/main.cpp‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,10 @@ int main(int argc, char* argv[]) {
7777
.action(dummy_action);
7878
app.subcommand(create_subos());
7979

80-
// Completions
81-
app.subcommand("completions")
82-
.description("Generate shell completions")
83-
.option(Option("shell").takes_value().value_name("SHELL").help("Target shell [possible values: bash, fish, zsh]"))
84-
.action([&app](const ParsedArgs& p) {
85-
auto shell_opt = p.value("shell");
86-
if (!shell_opt) {
87-
std::println(std::cerr, "SHELL not provided.");
88-
return;
89-
}
90-
91-
auto shell = cmdline::shell_from_string(*shell_opt);
92-
if (!shell || !cmdline::shell_supported(*shell)) {
93-
std::println(std::cerr, "Unsupported shell: {}", *shell_opt);
94-
return;
95-
}
96-
97-
std::print("{}", app.completions(*shell));
98-
});
80+
// Completions: one call. `completions <shell>` prints the adapter a shell
81+
// sources; the adapter asks the program (`__complete`), which answers from
82+
// this tree.
83+
(void)app.completion();
9984

10085
return app.run(argc, argv);
10186
}

‎mcpp.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
namespace = "mcpplibs"
33
name = "cmdline"
4-
version = "0.0.5"
4+
version = "0.0.6"
55
description = "A simple command-line parsing library/framework for modern C++"
66
license = "Apache-2.0"
77
authors = ["mcpplibs"]

0 commit comments

Comments
 (0)