Argparse: derive optional and repeated numeric/enum fields
This commit is contained in:
1 parent
4c0ebc76be
commit
546628c5b9
2 files changed
+97
-3
No files matched your search
@@ -257,7 +257,9 @@ namespace Void::Argparse
|
||||
// strict validation after a successful parse.
|
||||
struct NumericSink {
|
||||
std::string text{};
|
||||
std::vector<std::string> texts{};
|
||||
std::string option{};
|
||||
bool many{false};
|
||||
std::function<bool(std::string_view)> assign{};
|
||||
};
|
||||
// Heap-owned so the parser's bound string pointer stays valid when the
|
||||
@@ -284,9 +286,67 @@ namespace Void::Argparse
|
||||
} else if constexpr (std::same_as<MT, bool>) {
|
||||
parser.flag(lng, sht, desc, out.[:m:]);
|
||||
} else if constexpr (Void::Meta::is_optional_v<MT>) {
|
||||
parser.option(lng, sht, desc, out.[:m:]);
|
||||
using Inner = typename MT::value_type;
|
||||
if constexpr (std::same_as<Inner, std::string>) {
|
||||
parser.option(lng, sht, desc, out.[:m:]);
|
||||
} else {
|
||||
auto sink = std::make_unique<NumericSink>();
|
||||
sink->option = lng;
|
||||
sink->assign = [target = &out.[:m:]](std::string_view text) {
|
||||
if (text.empty()) {
|
||||
target->reset();
|
||||
return true;
|
||||
}
|
||||
Inner value{};
|
||||
if constexpr (std::is_enum_v<Inner>) {
|
||||
const auto parsed = Void::Meta::enum_parse<Inner>(text);
|
||||
if (!parsed) {
|
||||
return false;
|
||||
}
|
||||
*target = *parsed;
|
||||
return true;
|
||||
} else {
|
||||
const auto result = std::from_chars(
|
||||
text.data(), text.data() + text.size(), value);
|
||||
if (result.ec != std::errc{} ||
|
||||
result.ptr != text.data() + text.size()) {
|
||||
return false;
|
||||
}
|
||||
*target = value;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
parser.option_default(lng, sht, desc, sink->text, "");
|
||||
numeric_sinks.push_back(std::move(sink));
|
||||
}
|
||||
} else if constexpr (std::same_as<MT, std::vector<std::string>>) {
|
||||
parser.option_many(lng, sht, desc, out.[:m:]);
|
||||
} else if constexpr (Void::Meta::is_vector_v<MT>) {
|
||||
using Inner = typename MT::value_type;
|
||||
auto sink = std::make_unique<NumericSink>();
|
||||
sink->option = lng;
|
||||
sink->many = true;
|
||||
sink->assign = [target = &out.[:m:]](std::string_view text) {
|
||||
Inner value{};
|
||||
if constexpr (std::is_enum_v<Inner>) {
|
||||
const auto parsed = Void::Meta::enum_parse<Inner>(text);
|
||||
if (!parsed) {
|
||||
return false;
|
||||
}
|
||||
value = *parsed;
|
||||
} else {
|
||||
const auto result = std::from_chars(
|
||||
text.data(), text.data() + text.size(), value);
|
||||
if (result.ec != std::errc{} ||
|
||||
result.ptr != text.data() + text.size()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
target->push_back(value);
|
||||
return true;
|
||||
};
|
||||
parser.option_many(lng, sht, desc, sink->texts);
|
||||
numeric_sinks.push_back(std::move(sink));
|
||||
} else if constexpr (std::same_as<MT, std::string>) {
|
||||
const std::string def = out.[:m:];
|
||||
parser.option_default(lng, sht, desc, out.[:m:], def);
|
||||
@@ -338,8 +398,15 @@ namespace Void::Argparse
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
for (const auto& sink : numeric_sinks) {
|
||||
if (!sink->assign(sink->text)) {
|
||||
return std::unexpected{"invalid numeric value '" + sink->text +
|
||||
if (sink->many) {
|
||||
for (const auto& text : sink->texts) {
|
||||
if (!sink->assign(text)) {
|
||||
return std::unexpected{"invalid value '" + text +
|
||||
"' for " + sink->option};
|
||||
}
|
||||
}
|
||||
} else if (!sink->assign(sink->text)) {
|
||||
return std::unexpected{"invalid value '" + sink->text +
|
||||
"' for " + sink->option};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user