better type safety for action types.

This commit is contained in:
2025-10-04 00:12:23 +04:00
parent ebbf3983de
commit 179f5e5d1b
5 changed files with 262 additions and 435 deletions

View File

@@ -10,6 +10,23 @@
using namespace argument_parser::conventions;
struct Point {
int x, y;
};
template<>
struct argument_parser::parsing_traits::parser_trait<Point> {
static Point parse(const std::string& input) {
auto comma_pos = input.find(',');
if (comma_pos == std::string::npos) {
throw std::runtime_error("Invalid Point format. Expected 'x,y'.");
}
int x = std::stoi(input.substr(0, comma_pos));
int y = std::stoi(input.substr(comma_pos + 1));
return {x, y};
}
};
const std::initializer_list<argument_parser::conventions::convention const* const> conventions = {
&gnu_argument_convention,
&gnu_equal_argument_convention
@@ -19,6 +36,11 @@ const auto echo = argument_parser::helpers::make_parametered_action<std::string>
std::cout << text << std::endl;
});
const auto echo_point = argument_parser::helpers::make_parametered_action<Point>([](Point const& point) {
std::cout << "Point(" << point.x << ", " << point.y << ")" << std::endl;
});
const auto cat = argument_parser::helpers::make_parametered_action<std::string>([](std::string const& file_name) {
std::ifstream file(file_name);
if (!file.is_open()) {
@@ -86,10 +108,11 @@ auto make_grep_action(argument_parser::base_parser& parser) {
}
int main() {
std::vector<std::string> fake_args = { "-g", "add", "-f", "src/main.cpp" };
std::vector<std::string> fake_args = { "-g", "add", "-f", "src/main.cpp", "-ep", "1,2" };
auto parser = argument_parser::fake_parser{"test", std::move(fake_args)};
auto [file, grep] = make_grep_action(parser);
parser.add_argument("e", "echo", "echoes given variable", echo, false);
parser.add_argument("ep", "echo-point", "echoes given point", echo_point, false);
parser.add_argument("f", "file", "File to grep, required only if using grep", file, false);
parser.add_argument("g", "grep", "Grep pattern, required only if using grep", grep, false);
parser.add_argument("c", "cat", "Prints the content of the file", cat, false);