From 8c9688fff4446a1b0f5fe9a9be0c50084726cc4d Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Wed, 2 Jul 2025 00:33:19 +0100 Subject: CLI cleanups; fix the FreeBSD build --- nihil.cli/command.ccm | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'nihil.cli/command.ccm') 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; - 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(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) { } -- cgit v1.2.3