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;
|
||||
|
||||
Reference in new issue
Block a user