Argparse: add value_name, alias and hide customization

This commit is contained in:
faumaray committed 2026-12-04 01:40:25 +03:00
1 parent 3990c77155
commit e40e2ca715
2 files changed
+69 -1

No files matched your search

@@ -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<Options>(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<std::string> 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 };