feat(system,config,sqlite): implement VL-001, VL-003, VL-010, VL-011
This commit is contained in:
1 parent
adfc8548dc
commit
b50131f52e
14 files changed
+2660
-139
No files matched your search
@@ -2,7 +2,10 @@
|
||||
|
||||
#include <Void/Core/Meta.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -11,23 +14,56 @@
|
||||
namespace Void::Argparse
|
||||
{
|
||||
|
||||
enum class ParseOutcome : std::uint8_t {
|
||||
Ok,
|
||||
Help,
|
||||
Error,
|
||||
};
|
||||
|
||||
struct ParseResult {
|
||||
ParseOutcome outcome{ParseOutcome::Ok};
|
||||
std::string message{};
|
||||
std::vector<std::string> matched_command_path{};
|
||||
|
||||
[[nodiscard]] bool ok() const noexcept
|
||||
{
|
||||
return outcome == ParseOutcome::Ok;
|
||||
}
|
||||
[[nodiscard]] bool is_help() const noexcept
|
||||
{
|
||||
return outcome == ParseOutcome::Help;
|
||||
}
|
||||
[[nodiscard]] bool is_error() const noexcept
|
||||
{
|
||||
return outcome == ParseOutcome::Error;
|
||||
}
|
||||
};
|
||||
|
||||
// ── Parser
|
||||
// ────────────────────────────────────────────────────────────────────
|
||||
// Fluent command-line argument parser.
|
||||
//
|
||||
// Supports:
|
||||
// --flag, -f boolean flag (default false, set to true by
|
||||
// presence)
|
||||
// presence)
|
||||
// --key=value option with value (= or space separator)
|
||||
// --key value option with value
|
||||
// option_many(...) repeatable option collecting all occurrences
|
||||
// subcommand(...) nested subcommand parsers (e.g. "compdb use")
|
||||
// positional args collected in declaration order
|
||||
// --help / -h auto-generated help text
|
||||
// --help / -h auto-generated help text (ParseOutcome::Help)
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
|
||||
explicit Parser(std::string program, std::string description = {});
|
||||
~Parser();
|
||||
|
||||
Parser(const Parser&) = delete;
|
||||
Parser& operator=(const Parser&) = delete;
|
||||
Parser(Parser&&) noexcept;
|
||||
Parser& operator=(Parser&&) noexcept;
|
||||
|
||||
// Add a boolean flag.
|
||||
// long_name: e.g. "--verbose" short_name: e.g. "-v" (short_name
|
||||
@@ -43,6 +79,12 @@ namespace Void::Argparse
|
||||
std::string_view description,
|
||||
std::optional<std::string>& out);
|
||||
|
||||
// Add a repeatable option collecting all occurrences into `out`.
|
||||
Parser& option_many(std::string_view long_name,
|
||||
std::string_view short_name,
|
||||
std::string_view description,
|
||||
std::vector<std::string>& out);
|
||||
|
||||
// Add an option with a default value. out is always set after
|
||||
// parse().
|
||||
Parser& option_default(std::string_view long_name,
|
||||
@@ -55,7 +97,28 @@ namespace Void::Argparse
|
||||
Parser& positional(std::string_view name,
|
||||
std::string_view description, std::string& out);
|
||||
|
||||
// Parse argc/argv. Returns error message on failure.
|
||||
// Add a variadic trailing positional argument list.
|
||||
Parser& positional_many(std::string_view name,
|
||||
std::string_view description,
|
||||
std::vector<std::string>& out,
|
||||
bool required = false);
|
||||
|
||||
// Register a nested subcommand and return a reference to its
|
||||
// Parser. The returned reference remains valid for the lifetime of
|
||||
// the parent Parser.
|
||||
Parser& subcommand(std::string name, std::string description = {},
|
||||
std::function<void()> on_selected = {});
|
||||
|
||||
// Control whether a parser with subcommands requires one to be
|
||||
// selected (default: true when any subcommand is registered).
|
||||
Parser& require_subcommand(bool required = true) noexcept;
|
||||
|
||||
// Parse argc/argv with explicit Ok / Help / Error classification.
|
||||
[[nodiscard]] ParseResult
|
||||
parse_detailed(int argc, const char* const* argv) const;
|
||||
|
||||
// Parse argc/argv. Returns error message (or help text) on
|
||||
// non-Ok outcome for 1.x compatibility.
|
||||
[[nodiscard]] std::expected<void, std::string>
|
||||
parse(int argc, const char* const* argv) const;
|
||||
|
||||
@@ -70,18 +133,33 @@ namespace Void::Argparse
|
||||
};
|
||||
struct OptionEntry {
|
||||
std::string lng, sht, desc, def;
|
||||
std::optional<std::string>* out_opt;
|
||||
std::string* out_str;
|
||||
std::optional<std::string>* out_opt{nullptr};
|
||||
std::string* out_str{nullptr};
|
||||
std::vector<std::string>* out_many{nullptr};
|
||||
};
|
||||
struct PosEntry {
|
||||
std::string name, desc;
|
||||
std::string* out;
|
||||
};
|
||||
struct MultiPosEntry {
|
||||
std::string name, desc;
|
||||
std::vector<std::string>* out{nullptr};
|
||||
bool required{false};
|
||||
};
|
||||
struct SubcommandEntry {
|
||||
std::string name;
|
||||
std::unique_ptr<Parser> parser;
|
||||
std::function<void()> on_selected;
|
||||
};
|
||||
|
||||
std::string program_, description_;
|
||||
std::vector<FlagEntry> flags_;
|
||||
std::vector<OptionEntry> options_;
|
||||
std::vector<PosEntry> positionals_;
|
||||
std::optional<MultiPosEntry> positional_many_;
|
||||
std::vector<SubcommandEntry> subcommands_;
|
||||
bool has_explicit_require_sub_{false};
|
||||
bool require_subcommand_{true};
|
||||
};
|
||||
|
||||
// ── reflective options struct
|
||||
@@ -147,6 +225,8 @@ namespace Void::Argparse
|
||||
parser.flag(lng, sht, desc, out.[:m:]);
|
||||
} else if constexpr (Void::Meta::is_optional_v<MT>) {
|
||||
parser.option(lng, sht, desc, out.[:m:]);
|
||||
} else if constexpr (std::same_as<MT, std::vector<std::string>>) {
|
||||
parser.option_many(lng, sht, desc, out.[:m:]);
|
||||
} else if constexpr (std::same_as<MT, std::string>) {
|
||||
const std::string def = out.[:m:];
|
||||
parser.option_default(lng, sht, desc, out.[:m:], def);
|
||||
|
||||
Reference in new issue
Block a user