diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-07-01 17:07:04 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-07-01 17:07:04 +0100 |
| commit | 2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 (patch) | |
| tree | 54d37ffadf8e677938d9b7a28e4e9b71be1e75c1 /nihil.cli | |
| parent | 36427c0966faa7aecd586b397ed9b845f18172f5 (diff) | |
| download | nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.gz nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.bz2 | |
add nihil.std
Diffstat (limited to 'nihil.cli')
| -rw-r--r-- | nihil.cli/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | nihil.cli/command.cc | 12 | ||||
| -rw-r--r-- | nihil.cli/command.ccm | 6 | ||||
| -rw-r--r-- | nihil.cli/command_node.cc | 19 | ||||
| -rw-r--r-- | nihil.cli/command_node.ccm | 17 | ||||
| -rw-r--r-- | nihil.cli/command_tree.cc | 22 | ||||
| -rw-r--r-- | nihil.cli/command_tree.ccm | 14 | ||||
| -rw-r--r-- | nihil.cli/dispatch_command.cc | 20 | ||||
| -rw-r--r-- | nihil.cli/dispatch_command.ccm | 31 | ||||
| -rw-r--r-- | nihil.cli/registry.cc | 30 | ||||
| -rw-r--r-- | nihil.cli/registry.ccm | 22 | ||||
| -rw-r--r-- | nihil.cli/test.cc | 24 | ||||
| -rw-r--r-- | nihil.cli/usage_error.ccm | 16 |
13 files changed, 64 insertions, 171 deletions
diff --git a/nihil.cli/CMakeLists.txt b/nihil.cli/CMakeLists.txt index 78e9ff1..9f20cf7 100644 --- a/nihil.cli/CMakeLists.txt +++ b/nihil.cli/CMakeLists.txt @@ -1,7 +1,7 @@ # This source code is released into the public domain. add_library(nihil.cli STATIC) -target_link_libraries(nihil.cli PRIVATE nihil.util) +target_link_libraries(nihil.cli PRIVATE nihil.std nihil.util) target_sources(nihil.cli PUBLIC FILE_SET modules TYPE CXX_MODULES FILES nihil.cli.ccm diff --git a/nihil.cli/command.cc b/nihil.cli/command.cc index 725b4eb..6271cc6 100644 --- a/nihil.cli/command.cc +++ b/nihil.cli/command.cc @@ -1,15 +1,6 @@ -/* - * This source code is released into the public domain. - */ - +// This source code is released into the public domain. module; -#include <expected> -#include <functional> -#include <iostream> -#include <print> -#include <string> - // For EX_USAGE. While <sysexits.h> is deprecated, there's no other standard // exit code for 'usage error'; some programs use 2 (common on Linux), but // 2 is also used for many other exit codes. @@ -17,6 +8,7 @@ module; module nihil.cli; +import nihil.std; import nihil.error; import :registry; diff --git a/nihil.cli/command.ccm b/nihil.cli/command.ccm index 74ef030..da3444a 100644 --- a/nihil.cli/command.ccm +++ b/nihil.cli/command.ccm @@ -4,13 +4,10 @@ module; -#include <expected> -#include <functional> -#include <string> - export module nihil.cli:command; import nihil.error; +import nihil.std; import :command_node; namespace nihil { @@ -40,7 +37,6 @@ export struct command final : command_node { -> std::expected<int, error> override; private: - std::string_view m_path; std::string_view m_usage; command_function_t m_handler; }; diff --git a/nihil.cli/command_node.cc b/nihil.cli/command_node.cc index dd18716..5936b8c 100644 --- a/nihil.cli/command_node.cc +++ b/nihil.cli/command_node.cc @@ -1,18 +1,11 @@ -/* - * This source code is released into the public domain. - */ - +// This source code is released into the public domain. module; -#include <expected> -#include <iostream> -#include <print> -#include <string> - -#include <unistd.h> +#include <unistd.h> // _exit module nihil.cli; +import nihil.std; import nihil.core; import nihil.error; @@ -23,14 +16,12 @@ command_node::command_node(std::string_view path) noexcept try : m_path(path) { } catch (std::exception const &exc) { - std::fprintf(stderr, "%s\n", exc.what()); + std::print(std::cerr, "%s\n", exc.what()); _exit(1); /*NOTREACHED*/ } -command_node::~command_node() -{ -} +command_node::~command_node() = default; auto command_node::path(this command_node const &self) noexcept -> std::string_view diff --git a/nihil.cli/command_node.ccm b/nihil.cli/command_node.ccm index 546eb46..25b5006 100644 --- a/nihil.cli/command_node.ccm +++ b/nihil.cli/command_node.ccm @@ -1,18 +1,9 @@ -/* - * This source code is released into the public domain. - */ - -module; - -/* - * command_node represents a possibly-invocable command. - */ - -#include <expected> -#include <string> - +// This source code is released into the public domain. export module nihil.cli:command_node; +// command_node represents a possibly-invocable command. + +import nihil.std; import nihil.error; namespace nihil { diff --git a/nihil.cli/command_tree.cc b/nihil.cli/command_tree.cc index 2d14669..a77131a 100644 --- a/nihil.cli/command_tree.cc +++ b/nihil.cli/command_tree.cc @@ -1,18 +1,8 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <iostream> -#include <memory> -#include <print> -#include <ranges> -#include <string> -#include <vector> - +// This source code is released into the public domain. module nihil.cli; +import nihil.std; + namespace nihil { command_tree_node::command_tree_node() @@ -64,7 +54,7 @@ auto command_tree_node::get_or_create_child(this command_tree_node &self, -> command_tree_node * { // Return the existing child, if there is one. - if (auto ptr = self.get_child(child); ptr != nullptr) + if (auto *ptr = self.get_child(child); ptr != nullptr) return ptr; // Insert a new child. @@ -135,11 +125,11 @@ auto command_tree::insert(this command_tree &self, auto command_tree::find(this command_tree const &self, int &argc, char **&argv) -> command_tree_node const * { - auto *this_node = &self.m_root_node; + auto const *this_node = &self.m_root_node; // Iterate until we don't find a child command, then return that node. while (argv[0] != nullptr) { - auto *next_node = this_node->get_child(argv[0]); + auto const *next_node = this_node->get_child(argv[0]); if (next_node == nullptr) return this_node; diff --git a/nihil.cli/command_tree.ccm b/nihil.cli/command_tree.ccm index 7297af7..84e4a0d 100644 --- a/nihil.cli/command_tree.ccm +++ b/nihil.cli/command_tree.ccm @@ -1,17 +1,7 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <map> -#include <memory> -#include <optional> -#include <ranges> -#include <vector> - +// This source code is released into the public domain. export module nihil.cli:command_tree; +import nihil.std; import :command; namespace nihil { diff --git a/nihil.cli/dispatch_command.cc b/nihil.cli/dispatch_command.cc index 736e16e..7b4cf39 100644 --- a/nihil.cli/dispatch_command.cc +++ b/nihil.cli/dispatch_command.cc @@ -1,22 +1,12 @@ -/* - * This source code is released into the public domain. - */ - +// This source code is released into the public domain. module; -#include <cstdio> -#include <functional> -#include <iostream> -#include <map> -#include <print> -#include <ranges> -#include <string> -#include <utility> - -#include <unistd.h> +#include <stdlib.h> // getprogname, NOLINT +#include <unistd.h> // getopt module nihil.cli; +import nihil.std; import nihil.core; namespace nihil { @@ -45,7 +35,7 @@ auto dispatch_command(int argc, char **argv) -> int * Set the program name to the existing progname plus the full path * to the command being invoked; this makes error messages nicer. */ - auto *old_progname = ::getprogname(); + auto const *old_progname = ::getprogname(); { auto cprogname = std::format("{} {}", ::getprogname(), diff --git a/nihil.cli/dispatch_command.ccm b/nihil.cli/dispatch_command.ccm index 1ba55bb..de94714 100644 --- a/nihil.cli/dispatch_command.ccm +++ b/nihil.cli/dispatch_command.ccm @@ -1,31 +1,14 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <functional> -#include <iostream> -#include <map> -#include <string> -#include <utility> - +// This source code is released into the public domain. export module nihil.cli:dispatch_command; -import nihil.util; -import :command; -import :usage_error; - namespace nihil { -/* - * Invoke a command (which must have been previously registered) using - * the provided argument vector. - * - * The caller should have already stripped the executable name from argv[0] - * so that the vector starts with the command name. This is implicitly - * done if main() uses getopt(). - */ +// Invoke a command (which must have been previously registered) using +// the provided argument vector. +// +// The caller should have already stripped the executable name from argv[0] +// so that the vector starts with the command name. This is implicitly +// done if main() uses getopt(). export [[nodiscard]] auto dispatch_command(int argc, char **argv) -> int; } // namespace nihil diff --git a/nihil.cli/registry.cc b/nihil.cli/registry.cc index e35078d..0f0041b 100644 --- a/nihil.cli/registry.cc +++ b/nihil.cli/registry.cc @@ -1,16 +1,8 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <cstdio> -#include <exception> -#include <memory> -#include <vector> - +// This source code is released into the public domain. module nihil.cli; +import nihil.std; + namespace nihil { /* @@ -22,11 +14,11 @@ try { static auto commands = std::vector<std::shared_ptr<command_node>>(); return commands; } catch (std::exception const &exc) { - std::printf("%s\n", exc.what()); - std::exit(1); + std::println(std::cerr, "{}", exc.what()); + std::exit(1); // NOLINT } catch (...) { - std::printf("get_registered_commands(): unknown error\n"); - std::exit(1); + std::println(std::cerr, "get_registered_commands(): unknown error\n"); + std::exit(1); // NOLINT } /* @@ -39,11 +31,11 @@ try { auto &commands = get_registry(); commands.emplace_back(cmd, null_deleter); } catch (std::exception const &exc) { - std::printf("%s\n", exc.what()); - std::exit(1); + std::println(std::cerr, "{}", exc.what()); + std::exit(1); // NOLINT } catch (...) { - std::printf("get_registered_commands(): unknown error\n"); - std::exit(1); + std::println(std::cerr, "get_registered_commands(): unknown error\n"); + std::exit(1); // NOLINT } /* diff --git a/nihil.cli/registry.ccm b/nihil.cli/registry.ccm index 0b9754d..673f516 100644 --- a/nihil.cli/registry.ccm +++ b/nihil.cli/registry.ccm @@ -1,28 +1,18 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <memory> -#include <span> - +// This source code is released into the public domain. export module nihil.cli:registry; +import nihil.std; + namespace nihil { export struct command; export struct command_node; -/* - * Register a command. This is guaranteed not to throw; errors will print - * a diagnostic and exit. - */ +// Register a command. This is guaranteed not to throw; errors will print +// a diagnostic and exit. auto register_command(command *cmd) noexcept -> void; -/* - * Get previously registered commands. - */ +// Get previously registered commands. auto get_registered_commands() -> std::span<std::shared_ptr<command_node>>; } // namespace nihil diff --git a/nihil.cli/test.cc b/nihil.cli/test.cc index c82281e..61d164d 100644 --- a/nihil.cli/test.cc +++ b/nihil.cli/test.cc @@ -1,12 +1,8 @@ -/* - * This source code is released into the public domain. - */ - -#include <iostream> -#include <vector> +// This source code is released into the public domain. #include <catch2/catch_test_macros.hpp> +import nihil.std; import nihil.cli; import nihil.util; @@ -34,9 +30,9 @@ TEST_CASE("nihil.cli: dispatch_command: basic", "[nihil.cli]") auto args = std::vector<char const *>{ "cmd", "sub1", nullptr }; - auto argv = const_cast<char **>(args.data()); + auto *argv = const_cast<char **>(args.data()); - int ret = nihil::dispatch_command( + auto const ret = nihil::dispatch_command( static_cast<int>(args.size()) - 1, argv); REQUIRE(ret == 0); REQUIRE(cmd_sub1_called == true); @@ -47,9 +43,9 @@ TEST_CASE("nihil.cli: dispatch_command: basic", "[nihil.cli]") auto args = std::vector<char const *>{ "cmd", "sub2", nullptr }; - auto argv = const_cast<char **>(args.data()); + auto *argv = const_cast<char **>(args.data()); - int ret = nihil::dispatch_command( + auto const ret = nihil::dispatch_command( static_cast<int>(args.size()) - 1, argv); REQUIRE(ret == 0); REQUIRE(cmd_sub2_called == true); @@ -61,7 +57,7 @@ TEST_CASE("nihil.cli: dispatch_command: unknown command", "[nihil.cli]") auto args = std::vector<char const *>{ "nocomd", "sub", nullptr }; - auto argv = const_cast<char **>(args.data()); + auto *argv = const_cast<char **>(args.data()); auto output = std::string(); auto ret = int{}; @@ -75,7 +71,7 @@ TEST_CASE("nihil.cli: dispatch_command: unknown command", "[nihil.cli]") REQUIRE(ret == 1); - auto *progname = ::getprogname(); + auto const *progname = ::getprogname(); REQUIRE(output == std::format("{}: usage:\n cmd\n", progname)); } @@ -84,7 +80,7 @@ TEST_CASE("nihil.cli: dispatch_command: incomplete command", "[nihil.cli]") auto args = std::vector<char const *>{ "cmd", nullptr }; - auto argv = const_cast<char **>(args.data()); + auto *argv = const_cast<char **>(args.data()); auto output = std::string(); auto ret = int{}; @@ -98,7 +94,7 @@ TEST_CASE("nihil.cli: dispatch_command: incomplete command", "[nihil.cli]") REQUIRE(ret == 1); - auto *progname = ::getprogname(); + auto const *progname = ::getprogname(); REQUIRE(output == std::format("{}: usage:\n cmd sub1\n cmd sub2\n", progname)); } diff --git a/nihil.cli/usage_error.ccm b/nihil.cli/usage_error.ccm index 61feba7..7de178e 100644 --- a/nihil.cli/usage_error.ccm +++ b/nihil.cli/usage_error.ccm @@ -1,22 +1,14 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <string> - +// This source code is released into the public domain. export module nihil.cli:usage_error; +import nihil.std; import nihil.error; namespace nihil { -/* - * Exception thrown to indicate invalid command-line arguments. - */ +// Exception thrown to indicate invalid command-line arguments. export struct usage_error : error { - usage_error(std::string_view what) : error(what) {} + explicit usage_error(std::string_view what) : error(what) {} }; } // namespace nihil |
