Skip to content
mcpplibsPublic

About

一个简单易用的命令行解析库/框架 - import mcpplibs.cmdline;

Resources

Stars

3 stars

Watchers

1 watching

Forks

Repository files navigation

cmdline

A small, type-safe command-line parser for C++23 modules — import mcpplibs.cmdline;

中文 | English

Declare a command tree once; the parser, help, completions and the per-command dispatch read the same declarations. Arguments and option values are checked where they are declared, and an action returns the process status.

Features

  • C++23 module — one import, no headers.
  • Subcommands at any depth — an action gets its own ParsedArgs; a command without one leaves the run to its nearest ancestor.
  • Global options — a flag parses at any depth and is copied into the parse.
  • Checked values — values({...}) is a closed set; value_type<T>() checks a number with std::from_chars; value_as<T>() reads it back.
  • Help that is a list — .group() headings, .help_options({.order, .ungrouped, .groups_as_sections}), .help_epilogue(); the help, the completion candidates and the "a subcommand is required" message agree.
  • Shell completions — app.completion(); the program answers __complete from its own tree, and the bash/zsh/fish/PowerShell adapters carry no command list.
  • Flags answered before the action — answer_flag(name, help, fn); parse → answer_flags(parsed) → the action. --help still wins.
  • Agent Skills (optional) — agent-skills feature + import mcpplibs.cmdline.agent_skills;. The default is build-time: mcpplibs.mplugin.cmdline renders docs/<app>-skills into a C++ module (<app>.skills) whose sheet[] table is in the binary. No header is generated; the developer only imports the module. A runtime filesystem fallback exists (install_runtime) but is not recommended for a release.
  • Build helper — mcpp/cmdline/ is a separate build package (mcpplibs.mplugin.cmdline), re-exported by this one, so a consumer's build.mcpp imports it with no second dependency. It writes a module and adds it with mcpp::source; no include path, no header.

Requirements

C++23 modules and mcpp. There is no xmake path.

Build and test

mcpp build
mcpp test
mcpp build --features agent-skills     # the optional runtime module

The library's own tests are gtest cases under tests/; fixtures/skills is a small program whose build.mcpp renders three documents into fixture.skills and answers them — no header.

A program

import std;
import mcpplibs.cmdline;

using namespace mcpplibs::cmdline;

int main(int argc, char* argv[]) {
    App app("hello");
    app.version("1.0.0")
       .description("say hello")
       .option("name").short_name('n').takes_value().value_name("NAME").help("who to greet")
       .action([](const ParsedArgs& p) {
           std::println("hello, {}", p.value("name").value_or("world"));
       });
    return app.run(argc, argv);
}

Two sections, each command one line:

App app("tool");
app.help_options({.order = HelpOrder::alphabetical, .groups_as_sections = true});
app.subcommand(App("run").group("SUBCOMMANDS").description("run a job"));
app.subcommand(App("self").group("SELF MANAGEMENT").description("manage the tool"));

--agent-skill-doc from a build-time module (the recommended default):

// build.mcpp
import mcpplibs.mplugin.cmdline;
int main() {
    mcpplibs::mplugin::cmdline::options o;
    o.app = "myapp", o.module_name = "myapp.skills", o.name_space = "myapp::skills";
    return mcpplibs::mplugin::cmdline::embed(o) ? 0 : 1;
}
// src/skills.cppm — no header: the generated module is imported
export module myapp.install;
import mcpplibs.cmdline;
import mcpplibs.cmdline.agent_skills;
import myapp.skills;   // generated by build.mcpp

export void install(mcpplibs::cmdline::App& app) {
    mcpplibs::cmdline::agent_skills::install_default(app, myapp::skills::sheets,
        {.root_commands = {"run", "self"}, .self_commands = {"version"}});
}

The fallback for development, never the default: install_runtime(app, {.root = "docs/myapp-skills", ...}) reads the same layout from disk at install time.

Documentation

About

一个简单易用的命令行解析库/框架 - import mcpplibs.cmdline;

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages