ZXFoundation™ 26h2
Loading...
Searching...
No Matches
core.cxxm
1/// SPDX-License-Identifier: Apache 2.0
2/// @file cmdline.cxxm
3/// @brief Kernel command-line parameter registry and parser.
4
5export module zxfoundation.base.cmdline.core;
6import zxfoundation.base.types;
7import std;
8
9export {
10
11namespace zxfoundation::base::cmdline {
12
13 constexpr usize MAX_PARAMS = 64;
14
15 /// @brief How a parameter's value is interpreted.
16 enum class value_type : u8 {
17 flag, ///< Presence-only boolean; sets @c *(bool*)storage = true.
18 str, ///< String value copied into storage buffer.
19 u64, ///< Unsigned 64-bit integer.
20 boolean, ///< 0/1, on/off, true/false, yes/no — sets @c *(bool*)storage.
21 callback, ///< Custom handler: @c handler(value) is called.
22 };
23
24 /// @brief Signature for @c value_type::callback handlers.
25 /// @param value The token after '=', or empty for flags.
26 /// @return true on success, false to abort parsing.
27 using handler_fn = auto (*)(std::string_view value) noexcept -> bool;
28
29 /// @brief Descriptor for a single cmdline parameter.
30 struct param {
31 std::string_view name{};
32 value_type type;
33 handler_fn handler{nullptr}; ///< null = use default dispatch for type.
34 void* storage{nullptr}; ///< Pointer to the target variable.
35 usize storage_size{0}; ///< For str: capacity in bytes.
36 std::string_view help{}; ///< Human-readable description.
37 };
38
39 /// @brief Look up a registered parameter by name.
40 /// @return Pointer to the param, or nullptr.
41 [[nodiscard]] auto find(std::string_view name) noexcept -> param*;
42
43 /// @brief Register a cmdline parameter.
44 /// @return true on success, false if table is full or duplicate name.
45 auto add(const param& p) noexcept -> bool;
46
47 /// @brief Parse a complete command line against registered parameters.
48 auto parse(std::string_view cmdline) noexcept -> void;
49
50} // namespace zxfoundation::base::cmdline
51
52} // end export