Definition and Registry

Definition and Registry

DefinitionRegistry is a singleton — the root of your application’s entire declarative command surface. Every NamespaceDefinition and CommandDefinition is registered here, and through each command, every option, flag, default, constraint, and handler it owns hangs off that registration. The whole CLI is assembled in the registry before parsing, help, version, or execution ever runs.

var registry = DefinitionRegistry.GetInstance();

registry.Add(new NamespaceDefinition("report", "Reporting commands."));

var exportDef = new CommandDefinition("report.export");
exportDef.Help = "Export a report to a file.";

exportDef.AddOption<string>(key: "format", defaultArg: "csv", help: "Output format.", ("-f", "--format"));
exportDef["format"].AcceptedValues("csv", "json", "xml");

exportDef.AddOption<string>(key: "path", help: "Destination file path.", ("-p", "--path"));

exportDef.Handler = (cmd) =>
{
    string format = cmd["format"].GetValue<string>();
    string path = cmd["path"].GetValue<string>();
    // ...
};

registry.Add(exportDef);

Command cmd = CommandBuilder.Build(args);
CommandExecutor executor = registry.GetCommandExecutor(cmd);
executor.Execute();

report is a namespace; report.export is a command registered under it, carrying its own help text, two options (one with a default and an accepted-values constraint, one required), and a handler. Everything needed to parse, validate, render help for, and dispatch report.export lives on that single registration.

Registration happens once, at process startup, and rejects naming collisions between commands and namespaces.