Argparse: derive enum option fields with possible values
This commit is contained in:
1 parent
520849f82e
commit
28e253b49c
2 files changed
+44
No files matched your search
@@ -290,6 +290,27 @@ 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);
|
||||
} else if constexpr (std::is_enum_v<MT>) {
|
||||
auto sink = std::make_unique<NumericSink>();
|
||||
sink->option = lng;
|
||||
sink->text = std::string{Void::Meta::enum_name(out.[:m:])};
|
||||
const std::string initial = sink->text;
|
||||
std::vector<std::string> allowed;
|
||||
for (const auto& entry : Void::Meta::enum_entries<MT>()) {
|
||||
allowed.emplace_back(entry.second);
|
||||
}
|
||||
sink->assign = [target = &out.[:m:], initial](std::string_view text) {
|
||||
if (const auto parsed = Void::Meta::enum_parse<MT>(text)) {
|
||||
*target = *parsed;
|
||||
return true;
|
||||
}
|
||||
// A default that is not an enumerator name (e.g. a cast value)
|
||||
// is left untouched rather than failing the whole parse.
|
||||
return text == initial;
|
||||
};
|
||||
parser.option_default_checked(lng, sht, desc, sink->text, sink->text,
|
||||
std::move(allowed));
|
||||
numeric_sinks.push_back(std::move(sink));
|
||||
} else if constexpr (std::is_arithmetic_v<MT>) {
|
||||
auto sink = std::make_unique<NumericSink>();
|
||||
sink->option = lng;
|
||||
|
||||
Reference in new issue
Block a user