From d24315268c11d435bb9accbce87b7f46dda6ed3e Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 29 Jun 2025 00:42:31 +0100 Subject: cli: improve command dispatch a bit --- nihil.cli/command.ccm | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 nihil.cli/command.ccm (limited to 'nihil.cli/command.ccm') diff --git a/nihil.cli/command.ccm b/nihil.cli/command.ccm new file mode 100644 index 0000000..74ef030 --- /dev/null +++ b/nihil.cli/command.ccm @@ -0,0 +1,48 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include +#include +#include + +export module nihil.cli:command; + +import nihil.error; +import :command_node; + +namespace nihil { + +export struct command; + +/* + * A command that can be invoked. Instantiating a command adds this command + * to the global command table. If an error occurs, the program will abort. + */ + +using command_handler_t = int (int, char **); +using command_function_t = std::function; + +export struct command final : command_node { + command(std::string_view path, std::string_view usage, + command_function_t); + + command(std::string_view path, std::string_view usage, auto &&fn) + : command(path, usage, command_function_t(fn)) + {} + + [[nodiscard]] auto usage(this command const &) noexcept + -> std::string_view; + + [[nodiscard]] auto invoke(int argc, char **argv) const + -> std::expected override; + +private: + std::string_view m_path; + std::string_view m_usage; + command_function_t m_handler; +}; + +} // namespace nihil -- cgit v1.2.3