aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.cli/command.ccm
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-07-02 00:33:19 +0100
committerLexi Winter <lexi@le-fay.org>2025-07-02 00:33:19 +0100
commit8c9688fff4446a1b0f5fe9a9be0c50084726cc4d (patch)
treeca9a10be5795d976c0cbc73ad1111517bb4e22bf /nihil.cli/command.ccm
parent47999457e647352ae7e71d43c65e7b39ae5ca567 (diff)
downloadnihil-8c9688fff4446a1b0f5fe9a9be0c50084726cc4d.tar.gz
nihil-8c9688fff4446a1b0f5fe9a9be0c50084726cc4d.tar.bz2
CLI cleanups; fix the FreeBSD build
Diffstat (limited to 'nihil.cli/command.ccm')
-rw-r--r--nihil.cli/command.ccm17
1 files changed, 13 insertions, 4 deletions
diff --git a/nihil.cli/command.ccm b/nihil.cli/command.ccm
index dc3f29a..ea52bbf 100644
--- a/nihil.cli/command.ccm
+++ b/nihil.cli/command.ccm
@@ -31,15 +31,24 @@ export struct command final
{
using command_handler_t = std::function<int(int, char **)>;
- command(std::string_view const path, std::string_view const usage, command_handler_t handler) noexcept
- : m_path(path)
+ // Construct a new command with a handler and register it. Since this is usually invoked
+ // at global scope by static object construction, we handle exception internally.
+ command(std::string_view const path, std::string_view const usage, auto &&handler) noexcept
+ try : m_path(path)
, m_usage(usage)
- , m_handler(std::move(handler))
+ , m_handler(std::forward<decltype(handler)>(handler))
{
register_command(this);
+ } catch (std::exception const &exc) {
+ std::println(std::cerr, "register_command(): {}", exc.what());
+ std::quick_exit(1);
+ } catch (...) {
+ std::println(std::cerr, "register_command(): unknown error");
+ std::quick_exit(1);
}
- explicit command(std::string_view const path) noexcept
+ // Create a stub command which doesn't have a handler.
+ explicit command(std::string_view const path)
: m_path(path)
{
}