aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.cli/command.ccm
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-29 00:42:31 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-29 00:42:31 +0100
commitd24315268c11d435bb9accbce87b7f46dda6ed3e (patch)
tree66589cb6a15fa74d4b09683105c583e4a5c222b4 /nihil.cli/command.ccm
parent7741a9698d29f79aca3e47495dcdf87c7a712f42 (diff)
downloadnihil-d24315268c11d435bb9accbce87b7f46dda6ed3e.tar.gz
nihil-d24315268c11d435bb9accbce87b7f46dda6ed3e.tar.bz2
cli: improve command dispatch a bit
Diffstat (limited to 'nihil.cli/command.ccm')
-rw-r--r--nihil.cli/command.ccm48
1 files changed, 48 insertions, 0 deletions
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 <expected>
+#include <functional>
+#include <string>
+
+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<command_handler_t>;
+
+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<int, error> override;
+
+private:
+ std::string_view m_path;
+ std::string_view m_usage;
+ command_function_t m_handler;
+};
+
+} // namespace nihil