Commander is a lightweight, header-only library designed to simplify command parsing, context handling, and command execution for C++ projects. The library provides a flexible and extensible interface for defining commands with custom formats, argument parsing, and execution logic, making it ideal for scenarios like scripting systems, console commands, and user input processing.
- Interface-based Command System: Easily define commands by implementing the
InterfaceCommand
class, allowing customization of parsing, context management, and execution. - Template-based Command Implementation: Use the
ImplCommand
template to create commands with automatic argument parsing based on a format string, leveraging powerful tools likefmt
andscn
for formatting and scanning inputs. - Context Management: Commands can store and utilize a custom context, allowing them to carry and operate on necessary runtime data.
- Command Registration and Execution: The
ExecuterCommands
class allows dynamic registration and management of commands using a map, enabling easy lookup and execution of commands by name. - Regular Expression-based Command Extraction: Extract command names from format strings using
ctre
, allowing for flexible and customizable command formats. - Integration with Popular Libraries: Leverages well-known libraries such as
fmt
,scn
,ctre
, andplog
for powerful logging, formatting, and parsing functionalities.
Define a new command by inheriting from ImplCommand
and providing a format string for argument parsing. The library supports automatic scanning of arguments based on the provided format and effortless execution of commands.
struct MyCommand : ox::ImplCommand<ox::InterfaceCommand<std::string>, "{0}", std::string> {
int execute() override {
fmt::print("Executed command with argument: {}\n", std::get<0>(args()));
return 0;
}
};
Commands can be registered and executed using the ExecuterCommands
system:
ox::ExecuterCommands<std::string> commandManager;
commandManager.create_command<MyCommand>();
if (auto cmd = commandManager["MyCommand"]; cmd) {
cmd->parser("Hello");
cmd->execute();
}
fmt
: Formatting and printingscn
: Input scanning and argument parsingctre
: Regular expressions for command extractionplog
: Logging utilitiesfixed_string
: Compile-time fixed string support for format strings
This library is well-suited for applications where command execution is crucial, such as game engines, scripting systems, bots, or CLIs.