diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-07-02 00:33:19 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-07-02 00:33:19 +0100 |
| commit | 8c9688fff4446a1b0f5fe9a9be0c50084726cc4d (patch) | |
| tree | ca9a10be5795d976c0cbc73ad1111517bb4e22bf /nihil.cli/command.ccm | |
| parent | 47999457e647352ae7e71d43c65e7b39ae5ca567 (diff) | |
| download | nihil-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.ccm | 17 |
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) { } |
