From e40e2ca715f4fceb7b053337eea2a2830c6ac4e9 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Fri, 4 Dec 2026 01:40:25 +0300 Subject: [PATCH] Argparse: add value_name, alias and hide customization --- include/Void/Configuration/Argparse.hpp | 38 ++++++++++++++++++- .../Configuration/ArgparseReflect.cpp | 32 ++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/include/Void/Configuration/Argparse.hpp b/include/Void/Configuration/Argparse.hpp index 7fb6b0d..6149678 100644 --- a/include/Void/Configuration/Argparse.hpp +++ b/include/Void/Configuration/Argparse.hpp @@ -130,6 +130,16 @@ namespace Void::Argparse // Two options may not be used together. Parser& conflicts(std::string_view first, std::string_view second); + // Customize the help placeholder for an option (default "value"). + Parser& value_name(std::string_view long_name, + std::string_view name); + + // Accept an extra long spelling for an option or flag. + Parser& alias(std::string_view long_name, std::string_view alias); + + // Omit an option or flag from the generated help. + Parser& hide(std::string_view long_name); + // Add a required positional argument. Parser& positional(std::string_view name, std::string_view description, std::string& out); @@ -171,12 +181,15 @@ namespace Void::Argparse private: struct FlagEntry { - std::string lng, sht, desc; + std::string lng, sht, desc, alias; + bool hidden{false}; bool* out{nullptr}; int* count_out{nullptr}; }; struct OptionEntry { std::string lng, sht, desc, def; + std::string value_name{}, alias{}; + bool hidden{false}; std::optional* out_opt{nullptr}; std::string* out_str{nullptr}; std::vector* out_many{nullptr}; @@ -240,6 +253,14 @@ namespace Void::Argparse struct Name { Void::Meta::Str text; }; // long-name override (without "--") + struct ValueName { + Void::Meta::Str text; + }; // help placeholder for an option + struct Alias { + Void::Meta::Str text; + }; // extra long spelling (with "--") + struct Hide { + }; // omit from help struct Positional { }; @@ -280,6 +301,20 @@ namespace Void::Argparse constexpr auto hp = Void::Meta::annotation_of(m); const std::string desc = hp.has_value() ? std::string{hp->text.view()} : std::string{}; + constexpr auto vn = Void::Meta::annotation_of(m); + constexpr auto al = Void::Meta::annotation_of(m); + constexpr bool hidden = Void::Meta::has_annotation(m); + auto decorate = [&] { + if (vn.has_value()) { + parser.value_name(lng, vn->text.view()); + } + if (al.has_value()) { + parser.alias(lng, al->text.view()); + } + if (hidden) { + parser.hide(lng); + } + }; if constexpr (Void::Meta::has_annotation(m)) { parser.positional(std::meta::identifier_of(m), desc, out.[:m:]); @@ -350,6 +385,7 @@ namespace Void::Argparse } else if constexpr (std::same_as) { const std::string def = out.[:m:]; parser.option_default(lng, sht, desc, out.[:m:], def); + decorate(); } else if constexpr (std::is_enum_v) { auto sink = std::make_unique(); sink->option = lng; diff --git a/tests/standalone/Configuration/ArgparseReflect.cpp b/tests/standalone/Configuration/ArgparseReflect.cpp index d3877cd..0ccb081 100644 --- a/tests/standalone/Configuration/ArgparseReflect.cpp +++ b/tests/standalone/Configuration/ArgparseReflect.cpp @@ -96,6 +96,38 @@ Void::Testing::Test argparse_reflect_optional_and_vector_numerics() Void::Testing::expect(!rejected.has_value()); } +Void::Testing::Test argparse_reflect_value_name_alias_and_hide() +{ + struct Options { + [[= Void::Argparse::ValueName{"PORT"}]] + [[= Void::Argparse::Alias{"--bind"}]] + std::string address = ""; + + [[= Void::Argparse::Hide{}]] std::string secret = ""; + }; + + const char* argv[] = {"app", "--bind", "127.0.0.1", "--secret", "x"}; + auto parsed = Void::Argparse::parse(5, argv); + Void::Testing::require(parsed.has_value()); + Void::Testing::expect_eq(parsed->address, std::string{"127.0.0.1"}); + Void::Testing::expect_eq(parsed->secret, std::string{"x"}); + + // The fluent API exposes the same customization. + std::optional value; + Void::Argparse::Parser parser{"tool"}; + parser.option("--out", "-o", "output", value) + .value_name("--out", "FILE") + .alias("--out", "--output") + .hide("--out"); + const std::string help = parser.help(); + Void::Testing::expect(help.find("--out") == std::string::npos); + const char* alias_argv[] = {"tool", "--output", "f.txt"}; + auto result = parser.parse_detailed(3, alias_argv); + Void::Testing::expect(result.ok()); + Void::Testing::require(value.has_value()); + Void::Testing::expect_eq(*value, std::string{"f.txt"}); +} + Void::Testing::Test argparse_reflect_enum_fields() { enum class Color { Red, Green, Blue };