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.
|
// strict validation after a successful parse.
|
||||||
struct NumericSink {
|
struct NumericSink {
|
||||||
std::string text{};
|
std::string text{};
|
||||||
|
std::vector<std::string> texts{};
|
||||||
std::string option{};
|
std::string option{};
|
||||||
|
bool many{false};
|
||||||
std::function<bool(std::string_view)> assign{};
|
std::function<bool(std::string_view)> assign{};
|
||||||
};
|
};
|
||||||
// Heap-owned so the parser's bound string pointer stays valid when the
|
// 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>) {
|
} else if constexpr (std::same_as<MT, bool>) {
|
||||||
parser.flag(lng, sht, desc, out.[:m:]);
|
parser.flag(lng, sht, desc, out.[:m:]);
|
||||||
} else if constexpr (Void::Meta::is_optional_v<MT>) {
|
} 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>>) {
|
} else if constexpr (std::same_as<MT, std::vector<std::string>>) {
|
||||||
parser.option_many(lng, sht, desc, out.[:m:]);
|
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>) {
|
} else if constexpr (std::same_as<MT, std::string>) {
|
||||||
const std::string def = out.[:m:];
|
const std::string def = out.[:m:];
|
||||||
parser.option_default(lng, sht, desc, out.[:m:], def);
|
parser.option_default(lng, sht, desc, out.[:m:], def);
|
||||||
@@ -338,8 +398,15 @@ namespace Void::Argparse
|
|||||||
return std::unexpected{r.error()};
|
return std::unexpected{r.error()};
|
||||||
}
|
}
|
||||||
for (const auto& sink : numeric_sinks) {
|
for (const auto& sink : numeric_sinks) {
|
||||||
if (!sink->assign(sink->text)) {
|
if (sink->many) {
|
||||||
return std::unexpected{"invalid numeric value '" + sink->text +
|
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};
|
"' for " + sink->option};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,33 @@ Void::Testing::Test argparse_reflect_numeric_fields()
|
|||||||
Void::Testing::expect(!bad.has_value());
|
Void::Testing::expect(!bad.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test argparse_reflect_optional_and_vector_numerics()
|
||||||
|
{
|
||||||
|
struct Options {
|
||||||
|
std::optional<int> port;
|
||||||
|
std::vector<int> ids;
|
||||||
|
};
|
||||||
|
|
||||||
|
const char* absent[] = {"app"};
|
||||||
|
auto defaults = Void::Argparse::parse<Options>(1, absent);
|
||||||
|
Void::Testing::require(defaults.has_value());
|
||||||
|
Void::Testing::expect(!defaults->port.has_value());
|
||||||
|
Void::Testing::expect(defaults->ids.empty());
|
||||||
|
|
||||||
|
const char* argv[] = {"app", "--port", "8080", "--ids", "1", "--ids", "2",
|
||||||
|
"--ids", "3"};
|
||||||
|
auto parsed = Void::Argparse::parse<Options>(9, argv);
|
||||||
|
Void::Testing::require(parsed.has_value());
|
||||||
|
Void::Testing::require(parsed->port.has_value());
|
||||||
|
Void::Testing::expect_eq(*parsed->port, 8080);
|
||||||
|
Void::Testing::expect_eq(parsed->ids.size(), std::size_t{3});
|
||||||
|
Void::Testing::expect_eq(parsed->ids[2], 3);
|
||||||
|
|
||||||
|
const char* bad[] = {"app", "--ids", "nope"};
|
||||||
|
auto rejected = Void::Argparse::parse<Options>(3, bad);
|
||||||
|
Void::Testing::expect(!rejected.has_value());
|
||||||
|
}
|
||||||
|
|
||||||
Void::Testing::Test argparse_reflect_enum_fields()
|
Void::Testing::Test argparse_reflect_enum_fields()
|
||||||
{
|
{
|
||||||
enum class Color { Red, Green, Blue };
|
enum class Color { Red, Green, Blue };
|
||||||
|
|||||||
Reference in new issue
Block a user