Argparse: add value_name, alias and hide customization
This commit is contained in:
1 parent
3990c77155
commit
e40e2ca715
2 files changed
+69
-1
No files matched your search
@@ -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<std::string>* out_opt{nullptr};
|
||||
std::string* out_str{nullptr};
|
||||
std::vector<std::string>* 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<Help>(m);
|
||||
const std::string desc =
|
||||
hp.has_value() ? std::string{hp->text.view()} : std::string{};
|
||||
constexpr auto vn = Void::Meta::annotation_of<ValueName>(m);
|
||||
constexpr auto al = Void::Meta::annotation_of<Alias>(m);
|
||||
constexpr bool hidden = Void::Meta::has_annotation<Hide>(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<Positional>(m)) {
|
||||
parser.positional(std::meta::identifier_of(m), desc, out.[:m:]);
|
||||
@@ -350,6 +385,7 @@ namespace Void::Argparse
|
||||
} else if constexpr (std::same_as<MT, std::string>) {
|
||||
const std::string def = out.[:m:];
|
||||
parser.option_default(lng, sht, desc, out.[:m:], def);
|
||||
decorate();
|
||||
} else if constexpr (std::is_enum_v<MT>) {
|
||||
auto sink = std::make_unique<NumericSink>();
|
||||
sink->option = lng;
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in new issue
Block a user