20 #ifndef ANTKEEPER_DEBUG_CLI_HPP
21 #define ANTKEEPER_DEBUG_CLI_HPP
27 #include <type_traits>
28 #include <unordered_map>
44 std::string
interpret(
const std::string& line)
const;
56 template <
class T,
class... Args>
57 void register_command(
const std::string& name,
const std::function<T(Args...)>&
function);
58 template <
class T,
class... Args>
71 typedef std::function<std::string(
const std::string&)> command_type;
81 [[nodiscard]]
static T parse(std::istringstream& stream);
93 template <
class T,
class... Args>
94 [[nodiscard]]
static command_type wrap(std::function<T(Args...)>
function);
96 std::unordered_map<std::string, command_type> commands;
99 template <
class T,
class... Args>
102 commands[name] = wrap(
function);
105 template <
class T,
class... Args>
108 commands[name] = wrap(std::function(
function));
112 T cli::parse(std::istringstream& stream)
119 template <
class T,
class... Args>
120 typename cli::command_type cli::wrap(std::function<T(Args...)>
function)
124 [
function](
const std::string& line) -> std::string
129 std::istringstream istream(line);
130 std::tuple<Args...> arguments{parse<Args>(istream)...};
132 if constexpr(std::is_void_v<T>)
135 std::apply(
function, arguments);
138 return std::string();
143 T result = std::apply(
function, arguments);
146 std::ostringstream ostream;
148 return ostream.str();
151 std::placeholders::_1
Minimal command-line interpreter.
std::string interpret(const std::string &line) const
Interprets a command line as a function invocation.
void unregister_command(const std::string &name)
Unregisters a command from the CLI.
void register_command(const std::string &name, const std::function< T(Args...)> &function)
Registers a command with the CLI.
Debugging functions and classes.