Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export void install(mcpplibs::cmdline::App& app) {
The fallback for development, never the default: `install_runtime(app, {.root = "docs/myapp-skills", ...})`
reads the same layout from disk at install time.

A test keeps the sheets and the binary in agreement (0.2.0): `agent_skills::check(app, myapp::skills::sheets,
{.ascii_only = true})` returns one line per disagreement — a visible top-level command without a sheet, a sheet
that names no command, an example in a fenced block that does not parse by the tree.

Usage errors carry a stable `code`, the `command` the words reached and the closest declared names
(`suggestions`); `run` prints the reason, the closest name and that command's `--help` (0.2.0).

## Documentation

- [`docs/api.md`](docs/api.md) — the API, option by option.
Expand Down
2 changes: 1 addition & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- **help 就是列表** — `.group()` 标题、`.help_options({.order, .ungrouped, .groups_as_sections})`、`.help_epilogue()`;help、补全候选和“缺子命令”的报错一致。
- **Shell 补全** — `app.completion()`;程序用自己的命令树回答 `__complete`,bash/zsh/fish/PowerShell 适配脚本不含命令表。
- **动作前应答的 flag** — `answer_flag(name, help, fn)`;`parse` → `answer_flags(parsed)` → action。`--help` 仍优先。
- **Agent Skills(可选)** — `agent-skills` feature + `import mcpplibs.cmdline.agent_skills;`。默认构建期:`mcpplibs.mplugin.cmdline` 把 `docs/<app>-skills` 渲染成 C++ module(`<app>.skills`),`sheet[]` 表在二进制里;不生成头文件,开发者只 import module。运行时读取(`install_runtime`)保留,但不推荐用于发布。
- **Agent Skills(可选)** — `agent-skills` feature + `import mcpplibs.cmdline.agent_skills;`。默认构建期:`mcpplibs.mplugin.cmdline` 把 `docs/<app>-skills` 渲染成 C++ module(`<app>.skills`),`sheet[]` 表在二进制里;不生成头文件,开发者只 import module。运行时读取(`install_runtime`)保留,但不推荐用于发布。`agent_skills::check(app, sheets)` 在测试中核对说明与命令树:每个可见的顶层命令都有说明,每份说明都对应一个命令,代码块中的每个例子都能按命令树解析(自 0.2.0)。
- **构建 helper** — `mcpp/cmdline/` 是独立构建包(`mcpplibs.mplugin.cmdline`),由 runtime 包 re-export;消费者的 `build.mcpp` 直接 import,不需要第二条依赖。它写 module 并用 `mcpp::source` 加入构建,不碰 include 路径。
- **C++ Modules** — 库与可选模块都是 module,没有头文件。

Expand Down
28 changes: 25 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| `Option` | 选项(短/长、带值、多值、全局) |
| `ParsedArgs` | 解析结果 |
| `OptionValue` | 单个选项取值(flag 计数 + values) |
| `ParseError` | 解析错误(kind + message) |
| `ParseError` | 解析错误(kind + message;自 0.2.0 另有 code、command、suggestions) |
| `Argv` | `std::vector<std::string>` 的别名,用于 `parse_from` |
| `Shell` | 目标 shell 枚举:`bash` / `fish` / `zsh` / `powershell` |
| `Command` | 命令树快照,补全引擎读取它 |
Expand All @@ -30,6 +30,7 @@
| `.subcommand(App)` `.subcommand("name")` | 添加子命令 |
| `.action(fn)` | 根/子命令执行函数:`int(const ParsedArgs&)`(返回值即退出码)或 `void(const ParsedArgs&)`(退出码 0) |
| `.parse(argc, argv)` `.parse_from(Argv)` `.parse_from(string_view)` | 解析,返回 `ParseResult` |
| `.parse_silent(Argv)` `.parse_silent(string_view)` | 与 `parse_from` 相同的解析,不写出任何内容:help、版本与补全以各自的 `kind` 返回而不打印(自 0.2.0) |
| `.run(parsed)` | 执行解析结果所命中的命令的 action,返回其退出码(规则见“分发规则”) |
| `.run(argc, argv)` `.run(Argv)` | parse + dispatch,返回退出码(推荐主入口):help/version 为 0,解析错误写入标准错误并返回 2 |
| `.print_help(program_name)` | 打印帮助(子命令的列表见“帮助的列表”) |
Expand Down Expand Up @@ -89,14 +90,35 @@
|------|------|
| `kind` | 枚举:`help` / `version` / `completion` / `error` |
| `message` | 错误文本(仅 `kind == error` 时有内容) |
| `code` | 稳定的代码(自 0.2.0):`unknown-command`、`unknown-option`、`missing-value`、`invalid-value`、`missing-argument`、`invalid-argument`、`empty-command-line`;help、版本与补全时为空 |
| `command` | 词所到达的命令,按输入写出(`app sub`);它的 `--help` 适用于这个错误(自 0.2.0) |
| `suggestions` | 与未知的名字最相近的已声明名字,最近者在前,至多三个;取自 help 所列的同一份列表,选项带短横线(自 0.2.0) |
| `.is_error()` | `kind == error` 时返回 `true` |

### 用法错误(自 0.2.0)

- **未知的命令。** 一个有子命令而不声明任何位置参数的命令,收到一个既不是子命令也不是选项的词时,解析返回
`unknown-command`。0.1.0 及以前,这个词成为一个没有动作读取的位置参数。声明了位置参数的命令不变。隐藏的命令
照常解析,但不出现在 `suggestions` 中。
- **相近的名字。** 未知的命令取该级 help 所列的命令;未知的长选项取该命令的选项与根的全局选项,再加 `--help`。
判据是编辑距离不超过词长的三分之一(至少为 1;相邻两个字符的互换算一次编辑),或以该词开头。同一函数以
`similar_names(word, candidates, limit = 3)` 导出,供程序用于自己的名字(例如错误代码)。
- **`run` 的输出。** 解析错误写到标准错误,退出码 2,依次为原因、最相近的名字(有时)与所到达的那一级命令的
help:

```
tool: unknown option: --jbos
tool: the most similar option is '--jobs'
tool: see 'tool build --help'
```

```cpp
auto result = app.parse(argc, argv);
if (!result) {
if (!result.error().is_error()) return 0; // help / version,已打印
std::println("Error: {}", result.error().message);
return 1;
std::println(std::cerr, "error: {} ({})", result.error().message, result.error().code);
for (const auto& name : result.error().suggestions) std::println(std::cerr, " similar: {}", name);
return 2;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ description: Say hello.
---

Say hello, then stop.

```
$ skills-fixture hello
skills-fixture hello --agent-skill-doc | head -1
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
name: skills-fixture-self
description: The commands about the fixture itself.
---

The commands under self are listed below.
17 changes: 17 additions & 0 deletions fixtures/skills/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,22 @@ int main() {
if (app.run(Argv{"skills-fixture", "hello", "--agent-skill-doc"}) != 0) return 1;
if (app.run(Argv{"skills-fixture", "self", "world", "--agent-skill-doc"}) != 0) return 2;
if (app.run(Argv{"skills-fixture", "--agent-skill-doc"}) != 0) return 3;

// The sheets agree with the tree; a sheet that does not is reported.
if (const auto problems = agent_skills::check(app, fixture::skills::sheets, {.ascii_only = true});
!problems.empty()) {
for (const auto& line : problems) std::println(std::cerr, "{}", line);
return 4;
}
static constexpr unsigned char drifted[] =
"---\nname: x\n---\n\n```\nskills-fixture hello --loud\nskills-fixture helo\n```\n";
static constexpr unsigned char plain[] = "---\nname: y\n---\n\nNo example.\n";
const fixture::skills::sheet wrong[] = {{"hello", drifted, sizeof drifted - 1}, {"gone", plain, sizeof plain - 1}};
const auto problems = agent_skills::check(app, wrong);
// --loud is unknown, helo is no command, 'gone' names no command, 'self' has no sheet.
if (problems.size() != 4) {
for (const auto& line : problems) std::println(std::cerr, "{}", line);
return 5;
}
return 0;
}
2 changes: 1 addition & 1 deletion mcpp.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
namespace = "mcpplibs"
name = "cmdline"
version = "0.1.0"
version = "0.2.0"
description = "A simple command-line parsing library/framework for modern C++"
license = "Apache-2.0"
authors = ["mcpplibs"]
Expand Down
Loading
Loading