Skip to content

Latest commit

 

History

History
101 lines (85 loc) · 3.92 KB

skill.md

File metadata and controls

101 lines (85 loc) · 3.92 KB
name mcpplibs-cmdline
description Use when declaring a program's command line in C++23 - commands, options, arguments, help, completion and the --agent-skill-doc sheets.

mcpplibs.cmdline

One declaration of a command tree is read by the parse, the help, the shell completion and the per-command dispatch. import mcpplibs.cmdline; - no header.

[dependencies]
mcpplibs.cmdline = "0.4.0"
# with --agent-skill-doc: { version = "0.4.0", features = ["agent-skills"] }

A program

import std;
import mcpplibs.cmdline;
using namespace mcpplibs::cmdline;

int main(int argc, char* argv[]) {
    App app("tool");
    (void)app.version("1.0.0").description("a tool")
        .help_options({.layout = HelpLayout::compact})
        .option(Option("verbose").short_name('v').global().help("Print each step"));
    (void)app.subcommand(App("greet").description("Greet someone")
        .arg(Arg("name").required())
        .option(Option("color").values({"auto", "never"}).help("When to colour"))
        .example("tool greet ada")
        .action([](const ParsedArgs& p) {
            std::println("hello, {}", p.positional(0));
            return 0;
        }));
    return app.run(argc, argv);   // help and version: 0; a usage error: 2
}
  • ParsedArgs: positional(i), value("name"), is_flag_set("name"), value_as<int>("jobs"), subcommand_name().
  • An option takes a value with takes_value(), values({...}) (a closed set), value_type<int>() (checked with from_chars) or value_hint(ValueHint::file).
  • global() parses at any depth; multiple() collects every value.
  • An action returning int is the status; a command without an action leaves the run to its nearest ancestor that has one.

Help

  • group("Packages") is the section a command is listed under.
  • help_options({.layout = HelpLayout::compact, .style = fn, .width = n}), on the root: one usage line, columns as wide as their labels, Examples:, See also:. style(HelpRole, text) adds the program's escapes (it decides whether the terminal takes them); width breaks prose at words. The default layout, classic, is the output of 0.3.0.
  • level(Level::more) lists a command or option only under --help --all.
  • example("tool greet ada"), related({"info", "index update"}).
  • lint(app, {.max_description = 40, .max_option_help = 60}) returns what is too long; call it from a test.

Agent sheets (feature agent-skills)

Sheets live in docs/<app>-skills/<app>-<command>/SKILL.md; the build helper renders them into a module, so the binary reads no file:

// build.mcpp
import mcpplibs.mplugin.cmdline;
int main() {
    mcpplibs::mplugin::cmdline::options o;
    o.app = "tool", o.module_name = "tool.skills", o.name_space = "tool::skills";
    return mcpplibs::mplugin::cmdline::embed(o) ? 0 : 1;
}
import mcpplibs.cmdline.agent_skills;
import tool.skills;
agent_skills::install_default(app, tool::skills::sheets, {
    .subjects = {{.command = "info", .form = "tool info <package> --agent-skill-doc",
                  .what = "the package's own sheet",
                  .answer = [](const ParsedArgs& p) -> std::optional<int> {
                      if (p.positional(0).empty()) return std::nullopt;  // the sheet of info
                      return print_package_sheet(p.positional(0));
                  }}}});
  • tool --agent-skill-doc prints the root sheet and an index of the others; tool <command> --agent-skill-doc the command's.
  • A subject answers for what a command's argument names; nullopt leaves the answer to the command's sheet.
  • agent_skills::check(app, sheets, {.ascii_only = true, .subjects = ...}) in a test: every command has a sheet, examples parse, each example is in the sheet's ## Examples, each related in its ## Related, each subject's form in its sheet.

Related

  • API, option by option: docs/api.md in the package.
  • The completion protocol: docs/completion-protocol.md.