Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::Commands Class Reference

The commands a console runs: each has a name, its arguments and a help. More...

#include <aegis2d/core/commands.hpp>

Public Member Functions

 Commands ()
 Creates the commands, with help only.
 
void add (std::string_view name, std::vector< CommandArg > arguments, std::string_view description, CommandHandler handler)
 Adds a command.
 
void add_variables (ConsoleVariables &variables) noexcept
 Makes the variables of a registry commands too.
 
bool contains (std::string_view name) const
 Tells whether a line can start with a name.
 
CommandResult run (std::string_view line)
 Runs a line.
 
std::string usage (std::string_view name) const
 How to call a command.
 
std::vector< std::string > names () const
 Names of the commands and the variables, in alphabetical order.
 

Detailed Description

The commands a console runs: each has a name, its arguments and a help.

A line is a command's name and its arguments, separated by blanks; double quotes keep blanks in an argument (say "hello world"; inside them, a backslash keeps a quote or a backslash). The arguments are checked against what the command takes, in their types, before it runs. Console variables are commands too, once given with add_variables(): r_vsync shows the variable, its default and its help, r_vsync off changes it. help lists the commands and the variables, help <name> shows the usage and the help of one. Not thread-safe: use it from the game thread.

core::Commands commands;
commands.add("spawn", {{.name = "type"}, {.name = "count", .type = core::ArgType::Integer,
.optional = true}},
"Spawns entities of a type", [&](const core::CommandCall& call) {
const auto count = call.has("count") ? call.integer("count") : 1;
...
return std::format("{} {} spawned", count, call.text("type"));
});
commands.run("spawn crate 3"); // Done, "3 crate spawned"
commands.run("spawn"); // WrongArguments, "missing type: spawn <type> [count]"
The commands a console runs: each has a name, its arguments and a help.
Definition commands.hpp:140
CommandResult run(std::string_view line)
Runs a line.
void add(std::string_view name, std::vector< CommandArg > arguments, std::string_view description, CommandHandler handler)
Adds a command.
@ Integer
A whole number.

Constructor & Destructor Documentation

◆ Commands()

aegis2d::core::Commands::Commands ( )

Creates the commands, with help only.

Member Function Documentation

◆ add()

void aegis2d::core::Commands::add ( std::string_view  name,
std::vector< CommandArg >  arguments,
std::string_view  description,
CommandHandler  handler 
)

Adds a command.

Parameters
nameIts name, as a console variable's (is_console_name()).
argumentsWhat it takes, in order.
descriptionWhat it does, as the help shows it.
handlerWhat it does.
Exceptions
std::invalid_argumentif name is not valid or is taken by a command or a variable, if an optional argument comes before one that is not, or a rest argument is not the last one or not a text.

◆ add_variables()

void aegis2d::core::Commands::add_variables ( ConsoleVariables &  variables)
noexcept

Makes the variables of a registry commands too.

A command of the same name comes first.

Parameters
variablesVariables, kept by reference: they must outlive these commands.

◆ contains()

bool aegis2d::core::Commands::contains ( std::string_view  name) const

Tells whether a line can start with a name.

Parameters
nameName of a command or a variable.
Returns
true for a command or a variable.

◆ names()

std::vector< std::string > aegis2d::core::Commands::names ( ) const

Names of the commands and the variables, in alphabetical order.

Returns
A copy of each name, help included.

◆ run()

CommandResult aegis2d::core::Commands::run ( std::string_view  line)

Runs a line.

Parameters
lineSuch as "spawn crate 3".
Returns
How it went, and what to show.

◆ usage()

std::string aegis2d::core::Commands::usage ( std::string_view  name) const

How to call a command.

Parameters
nameIts name.
Returns
Such as "spawn <type> [count]", or "r_vsync [value]" for a variable.
Exceptions
std::out_of_rangeif no command and no variable has this name.

The documentation for this class was generated from the following file: