Skip to content

Commit 5e7e506

Browse files
authored
0.1.0: flags before actions, help sections, and the Agent Skills build helper (#16)
* feat(0.1.0): flags answered before actions, grouped help sections, the Agent Skills module and build helper - answer_flag/answer_flags: a global flag answered after the parse and before any action; --help keeps its precedence. - HelpOptions.groups_as_sections: a group is a top-level section, not a second-level heading. - mcpplibs.cmdline.agent_skills (feature agent-skills): --agent-skill-doc reads frontmatter-stripped bodies and appends the root/self index. - mcpp/cmdline: mcpplibs.mplugin.cmdline, a host-module package that scans docs/<app>-skills, validates the Agent Skills format, normalizes line endings and writes a deterministic sheet[] header; the runtime package re-exports it. - mcpp only: xmake files and documentation removed; README is English with README.zh.md; CI runs Linux, macOS and Windows and builds the fixture. - version 0.1.0. * ci: install xlings with its PowerShell script on Windows The shell installer refuses MINGW64; the Windows leg uses the upstream PowerShell installer, then the same mcpp install and build steps. * fix(agent-skills): build the default index without a vector of string_view The GCC 16 libstdc++ module build refused the placement-new path a vector<string_view> takes in a template instantiated by the fixture; the index now appends rows directly. Also locate the fixture binary through find (the target tree has a platform directory between target and bin). * test(fixture): name the fixture app explicitly The CI runner's mcpp reports no package name to the build program in this fixture; options::app is the documented override and makes the fixture independent of that. * ci: use the macos-latest runner label The macos-15 job stayed queued with no runner for half an hour; the rolling label is what the upstream mcpp workflows use. * ci: pin the macOS leg to macos-14 macos-15 and macos-latest both stayed queued; macos-14 is the Intel image the GitHub Actions docs still list for open-source jobs. * ci: try macos-13 for the macOS leg * refactor(agent-skills): read the standard layout at run time; no build program, no header The default path is now the module alone: install_default walks docs/<app>-skills (or options.root), strips frontmatter and answers. The generated sheet[] header, the mcpp/cmdline build plugin package and the re-export edge are removed; the fixture has no build.mcpp and no #include. A program that already owns its sheets still passes find/index. * fix(agent-skills): the index reads frontmatter from the full sheet lookup() returns the file's bytes; find() strips the frontmatter for the answer. The index then reads name/description from the full text. The macOS leg is left out while the runner queue is unavailable. * feat(agent-skills): build-time module is the default again, now header-free The build helper scans docs/<app>-skills and writes a C++ MODULE with the sheet[] table (mcpp::source adds it; no header, no include path). The runtime's install_default(app, sheets, options) reads that table; the filesystem reader is renamed install_runtime and documented as the development-only fallback. The fixture and README show the module path. The macOS leg stays out while its runner queue is unavailable. --------- Co-authored-by: Sunrisepeak <sunrisepeak@users.noreply.github.com>
1 parent e7760f4 commit 5e7e506

21 files changed

Lines changed: 1126 additions & 208 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,35 @@ env:
1010

1111
jobs:
1212
build:
13-
name: build + test (linux x86_64, mcpp)
14-
runs-on: ubuntu-latest
13+
name: build + test (${{ matrix.os }}, mcpp)
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
# The macOS leg waits for a runner GitHub did not provide for this
18+
# repository (three labels queued for 30+ minutes on 2026-12-04). The
19+
# code is platform-neutral and the fixture builds on Linux/Windows;
20+
# re-add macos-15 when the queue answers.
21+
os: [ubuntu-latest, windows-2025]
22+
runs-on: ${{ matrix.os }}
23+
defaults:
24+
run:
25+
shell: bash
1526
steps:
1627
- uses: actions/checkout@v4
1728

18-
- name: Install xlings
29+
- name: Install xlings (Linux/macOS)
30+
if: runner.os != 'Windows'
1931
run: |
2032
curl -fsSL https://raw.githubusercontent.com/openxlings/xlings/main/tools/other/quick_install.sh | bash -s v2026.10.26.1
2133
echo "$HOME/.xlings/subos/current/bin" >> "$GITHUB_PATH"
2234
35+
- name: Install xlings (Windows)
36+
if: runner.os == 'Windows'
37+
shell: pwsh
38+
run: |
39+
irm https://d2learn.org/xlings-install.ps1.txt | iex
40+
"$env:USERPROFILE\.xlings\subos\current\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
41+
2342
- name: Install mcpp (the version .xlings.json names)
2443
run: |
2544
MCPP_VERSION="$(sed -n 's/.*"mcpp": *"\([^"]*\)".*/\1/p' .xlings.json)"
@@ -30,17 +49,19 @@ jobs:
3049
sleep 30
3150
done
3251
33-
# Cache mcpp's self-bootstrapped sandbox (toolchain, binutils, ninja,
34-
# patchelf). The toolchain set is pinned by the mcpp in .xlings.json,
35-
# so its version keys the cache.
36-
- name: Cache mcpp sandbox
37-
uses: actions/cache@v4
38-
with:
39-
path: ~/.xlings/data/xpkgs/xim-x-mcpp/2026.11.8.1/registry
40-
key: mcpp-sandbox-${{ runner.os }}-mcpp2026.11.8.1
41-
4252
- name: Build with mcpp
4353
run: mcpp build
4454

55+
- name: Build the optional agent-skills module
56+
run: mcpp build --features agent-skills
57+
4558
- name: Run tests
4659
run: mcpp test
60+
61+
# The build helper is a package of its own; this fixture proves the
62+
# re-export, the standard layout and the generated header on all three.
63+
- name: Build the skills fixture
64+
run: |
65+
cd fixtures/skills
66+
mcpp build
67+
"$(find target -type f -path '*/bin/skills-fixture*' | head -1)"

‎.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.xmake
21
build
32
target
43
.xlings/subos/

‎.xlings.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"workspace": {
3-
"mcpp": "2026.11.8.1"
3+
"mcpp": "2026.11.27.1"
44
}
55
}

‎README.md‎

Lines changed: 83 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,112 @@
11
# cmdline
22

3-
> 一个简单易用的命令行解析库/框架 - `import mcpplibs.cmdline;`
4-
5-
使用 C++23 模块的类型安全命令行解析库。流式链式接口,零样板代码。支持位置参数、短/长选项、子命令、全局标志,以及基于 `std::expected` 的错误处理。
6-
7-
## ✨ 特性
8-
9-
- **C++23 模块** — `import mcpplibs.cmdline`
10-
11-
- **流式接口** — 链式构建方法,所见即所得的 API
12-
13-
- **子命令** — 任意层级的嵌套命令,`action` 回调分发或手动分发
14-
15-
- **退出码** — `action` 可返回 `int`,`run` 返回该退出码;解析错误写入标准错误并返回 2
16-
17-
- **全局选项** — 标志透明地传播到所有子命令
18-
19-
- **类型安全结果** — `std::expected<ParsedArgs, std::string>`
20-
21-
- **多种解析模式** — 支持 `argc/argv`、`cmdline::Argv`(即 `vector<string>`)或原始 `string_view`
3+
> A small, type-safe command-line parser for C++23 modules — `import mcpplibs.cmdline;`
4+
5+
[中文](README.zh.md) | English
6+
7+
Declare a command tree once; the parser, help, completions and the per-command dispatch read
8+
the same declarations. Arguments and option values are checked where they are declared, and
9+
an action returns the process status.
10+
11+
## Features
12+
13+
- **C++23 module** — one import, no headers.
14+
- **Subcommands at any depth** — an `action` gets its own `ParsedArgs`; a command without one
15+
leaves the run to its nearest ancestor.
16+
- **Global options** — a flag parses at any depth and is copied into the parse.
17+
- **Checked values** — `values({...})` is a closed set; `value_type<T>()` checks a number with
18+
`std::from_chars`; `value_as<T>()` reads it back.
19+
- **Help that is a list** — `.group()` headings, `.help_options({.order, .ungrouped,
20+
.groups_as_sections})`, `.help_epilogue()`; the help, the completion candidates and the
21+
"a subcommand is required" message agree.
22+
- **Shell completions** — `app.completion()`; the program answers `__complete` from its own tree,
23+
and the bash/zsh/fish/PowerShell adapters carry no command list.
24+
- **Flags answered before the action** — `answer_flag(name, help, fn)`; `parse` →
25+
`answer_flags(parsed)` → the action. `--help` still wins.
26+
- **Agent Skills (optional)** — `agent-skills` feature + `import mcpplibs.cmdline.agent_skills;`.
27+
The default is build-time: `mcpplibs.mplugin.cmdline` renders `docs/<app>-skills` into a C++
28+
module (`<app>.skills`) whose `sheet[]` table is in the binary. No header is generated; the
29+
developer only imports the module. A runtime filesystem fallback exists (`install_runtime`) but
30+
is not recommended for a release.
31+
- **Build helper** — `mcpp/cmdline/` is a separate build package
32+
(`mcpplibs.mplugin.cmdline`), re-exported by this one, so a consumer's `build.mcpp` imports it
33+
with no second dependency. It writes a module and adds it with `mcpp::source`; no include path,
34+
no header.
35+
36+
## Requirements
37+
38+
C++23 modules and [mcpp](https://github.com/mcpp-community/mcpp). There is no xmake path.
39+
40+
## Build and test
41+
42+
```sh
43+
mcpp build
44+
mcpp test
45+
mcpp build --features agent-skills # the optional runtime module
46+
```
2247

23-
- **Shell 补全** — `app.completion()` 一次调用开启;程序以自己的命令树回答(`__complete`),bash、zsh、fish、PowerShell 的适配脚本只有几行,不含命令树
24-
- **数值选项** — `value_type<T>()` 声明选项取 `T` 类型的数字,解析时用 `std::from_chars` 校验,不合法就是解析错误;`value_as<T>(name)` 读回
25-
- **分组的 help** — 子命令的 `.group(s)` 给出标题,`.help_options({ .order = alphabetical, .ungrouped = "…" })` 定次序,`.help_epilogue(s)` 在列表后写例子;help、补全候选与“缺子命令”的报错用同一份列表
48+
The library's own tests are gtest cases under `tests/`; `fixtures/skills` is a small program whose
49+
`build.mcpp` renders three documents into `fixture.skills` and answers them — no header.
2650

27-
## 快速开始
51+
## A program
2852

2953
```cpp
3054
import std;
3155
import mcpplibs.cmdline;
3256

33-
using namespace mcpplibs;
57+
using namespace mcpplibs::cmdline;
3458

3559
int main(int argc, char* argv[]) {
36-
auto app = cmdline::App("myapp")
37-
.version("1.0.0")
38-
.description("A demo CLI")
39-
.arg("input").required().help("输入文件")
40-
.option("verbose").short_name('v').help("详细输出")
41-
.option("config").short_name('c').takes_value().value_name("FILE").help("配置文件")
42-
.action([](const cmdline::ParsedArgs& p) {
43-
if (p.is_flag_set("verbose")) std::println("详细模式已开启");
44-
if (auto c = p.value("config")) std::println("配置文件: {}", *c);
45-
std::println("输入文件: {}", p.positional(0));
46-
});
47-
60+
App app("hello");
61+
app.version("1.0.0")
62+
.description("say hello")
63+
.option("name").short_name('n').takes_value().value_name("NAME").help("who to greet")
64+
.action([](const ParsedArgs& p) {
65+
std::println("hello, {}", p.value("name").value_or("world"));
66+
});
4867
return app.run(argc, argv);
4968
}
5069
```
5170
52-
### 子命令 + action / run
71+
Two sections, each command one line:
5372
5473
```cpp
55-
auto app = cmdline::App("demo")
56-
.version("0.1.0")
57-
.description("Demo: subcommands with action dispatch")
58-
.option("yes").short_name('y').global().help("自动确认")
59-
.subcommand("add")
60-
.description("添加目标")
61-
.arg("target").required()
62-
.arg("version").required()
63-
.action([](const cmdline::ParsedArgs& a) {
64-
std::println("add: {}@{}", a.value("target").value_or(""),
65-
a.value("version").value_or(""));
66-
})
67-
.subcommand("remove")
68-
.description("移除目标")
69-
.arg("target").required()
70-
.action([](const cmdline::ParsedArgs& a) { std::println("remove: {}", a.positional(0)); });
71-
72-
app.run(argc, argv);
74+
App app("tool");
75+
app.help_options({.order = HelpOrder::alphabetical, .groups_as_sections = true});
76+
app.subcommand(App("run").group("SUBCOMMANDS").description("run a job"));
77+
app.subcommand(App("self").group("SELF MANAGEMENT").description("manage the tool"));
7378
```
7479

75-
### 多种解析模式
80+
`--agent-skill-doc` from a build-time module (the recommended default):
7681

7782
```cpp
78-
auto r1 = app.parse(argc, argv);
79-
auto r2 = app.parse_from(cmdline::Argv{"myapp", "add", "x", "1.0"});
80-
auto r3 = app.parse_from("myapp remove x --yes");
83+
// build.mcpp
84+
import mcpplibs.mplugin.cmdline;
85+
int main() {
86+
mcpplibs::mplugin::cmdline::options o;
87+
o.app = "myapp", o.module_name = "myapp.skills", o.name_space = "myapp::skills";
88+
return mcpplibs::mplugin::cmdline::embed(o) ? 0 : 1;
89+
}
8190
```
8291

83-
### 数值选项
84-
8592
```cpp
86-
auto app = cmdline::App("myapp")
87-
.option(cmdline::Option("rate").value_type<double>().help("每秒多少片"))
88-
.option(cmdline::Option("frames").short_name('f').value_type<int>());
89-
// myapp --rate fast → 解析错误:"option --rate takes a number, not `fast`"
90-
auto parsed = app.parse(argc, argv);
91-
const double rate = parsed->value_as<double>("rate").value_or(30.0); // 缺省时为空
92-
```
93-
94-
## 构建
95-
96-
**使用 xmake**
97-
98-
```shell
99-
xmake # 构建库
100-
xmake run basic # 运行基础示例
101-
xmake -y run cmdline_test # 运行测试(自动安装 gtest)
102-
```
103-
104-
**使用 mcpp**
105-
106-
```shell
107-
mcpp build # 构建库
108-
mcpp test # 运行 tests/ 下的测试
109-
```
110-
111-
## 集成到构建工具
112-
113-
### xmake
114-
115-
```lua
116-
-- 添加 mcpplibs 包仓库
117-
add_repositories("mcpplibs-index https://github.com/mcpplibs/mcpplibs-index.git")
118-
119-
-- 添加依赖
120-
add_requires("cmdline")
121-
122-
target("mytool")
123-
set_kind("binary")
124-
set_languages("c++23")
125-
add_files("main.cpp")
126-
add_packages("cmdline")
127-
set_policy("build.c++.modules", true)
128-
```
129-
130-
### mcpp
131-
132-
#### 添加依赖
133-
134-
```bash
135-
mcpp add cmdline@0.0.2
136-
```
137-
138-
或在 `mcpp.toml` 中手动添加:
139-
140-
```toml
141-
[dependencies]
142-
cmdline = "0.0.2"
143-
```
144-
145-
#### 构建
93+
// src/skills.cppm — no header: the generated module is imported
94+
export module myapp.install;
95+
import mcpplibs.cmdline;
96+
import mcpplibs.cmdline.agent_skills;
97+
import myapp.skills; // generated by build.mcpp
14698

147-
```bash
148-
mcpp build
99+
export void install(mcpplibs::cmdline::App& app) {
100+
mcpplibs::cmdline::agent_skills::install_default(app, myapp::skills::sheets,
101+
{.root_commands = {"run", "self"}, .self_commands = {"version"}});
102+
}
149103
```
150104
151-
#### 代码示例
152-
153-
```cpp
154-
import mcpplibs.cmdline;
155-
// ... 参见上方"快速开始"
156-
```
105+
The fallback for development, never the default: `install_runtime(app, {.root = "docs/myapp-skills", ...})`
106+
reads the same layout from disk at install time.
157107
158-
## 相关链接
108+
## Documentation
159109
160-
- [社区官网](https://mcpp.d2learn.org)
161-
- [社区论坛](https://forum.d2learn.org/category/20) ([`Q: 1067245099`](https://qm.qq.com/q/nBQdBaYNna))
162-
- [mcpp开源主页](https://github.com/mcpp-community)
110+
- [`docs/api.md`](docs/api.md) — the API, option by option.
111+
- [`examples/`](examples) — programs to copy.
112+
- [`LICENSE`](LICENSE) — Apache-2.0.

0 commit comments

Comments
 (0)