feat(argument_parser): add vector parsing support for int and string types
Implement parser traits for vector<int> and vector<string> to handle comma-separated values Add corresponding argument handling in main to demonstrate usage
This commit is contained in:
57
src/main.cpp
57
src/main.cpp
@@ -3,6 +3,7 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <vector>
|
||||
|
||||
using namespace argument_parser::conventions;
|
||||
|
||||
@@ -30,6 +31,42 @@ struct argument_parser::parsing_traits::parser_trait<std::regex> {
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct argument_parser::parsing_traits::parser_trait<std::vector<int>> {
|
||||
static std::vector<int> parse(const std::string& input) {
|
||||
std::vector<int> result;
|
||||
std::stringstream ss(input);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, ',')) {
|
||||
result.push_back(std::stoi(item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct argument_parser::parsing_traits::parser_trait<std::vector<std::string>> {
|
||||
static std::vector<std::string> parse(const std::string& input) {
|
||||
std::vector<std::string> result;
|
||||
auto copyInput = input;
|
||||
if (input.starts_with("[")) {
|
||||
copyInput = input.substr(1, input.size());
|
||||
if (copyInput.ends_with("]")) {
|
||||
copyInput = copyInput.substr(0, copyInput.size() - 1);
|
||||
} else {
|
||||
throw std::runtime_error("Invalid vector<string> format. Expected closing ']'.");
|
||||
}
|
||||
}
|
||||
std::stringstream ss(copyInput);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, ',')) {
|
||||
result.push_back(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
const std::initializer_list<argument_parser::conventions::convention const* const> conventions = {
|
||||
&gnu_argument_convention,
|
||||
&gnu_equal_argument_convention
|
||||
@@ -91,6 +128,10 @@ int main() {
|
||||
}), false);
|
||||
|
||||
parser.add_argument<Point>("p", "point", "Test point", false);
|
||||
|
||||
parser.add_argument<std::vector<int>>("t", "test", "Test vector<int>", false);
|
||||
parser.add_argument<std::vector<std::string>>("ts", "test-strings", "Test vector<string>", false);
|
||||
|
||||
parser.handle_arguments(conventions);
|
||||
|
||||
auto filename = parser.get_optional<std::string>("file");
|
||||
@@ -106,6 +147,22 @@ int main() {
|
||||
parser.display_help(conventions);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
auto test = parser.get_optional<std::vector<int>>("test");
|
||||
if (test) {
|
||||
for (auto const& item : test.value()) {
|
||||
std::cout << item << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
auto test_strings = parser.get_optional<std::vector<std::string>>("test-strings");
|
||||
if (test_strings) {
|
||||
for (auto const& item : test_strings.value()) {
|
||||
std::cout << item << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user