refactor(argument_parser): simplify grep implementation and use regex
Replace shared_ptr state management with direct function call Add regex support for pattern matching Remove unused test_store argument
This commit is contained in:
104
src/main.cpp
104
src/main.cpp
@@ -2,8 +2,7 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <regex>
|
||||
|
||||
using namespace argument_parser::conventions;
|
||||
|
||||
@@ -24,6 +23,13 @@ struct argument_parser::parsing_traits::parser_trait<Point> {
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct argument_parser::parsing_traits::parser_trait<std::regex> {
|
||||
static std::regex parse(const std::string& input) {
|
||||
return std::regex(input);
|
||||
}
|
||||
};
|
||||
|
||||
const std::initializer_list<argument_parser::conventions::convention const* const> conventions = {
|
||||
&gnu_argument_convention,
|
||||
&gnu_equal_argument_convention
|
||||
@@ -51,88 +57,54 @@ const auto cat = argument_parser::helpers::make_parametered_action<std::string>(
|
||||
file.close();
|
||||
});
|
||||
|
||||
auto make_grep_action(argument_parser::base_parser& parser) {
|
||||
std::shared_ptr<std::string> filename = std::make_shared<std::string>("");
|
||||
std::shared_ptr<bool> grep_requested = std::make_shared<bool>(false);
|
||||
std::shared_ptr<bool> grep_called = std::make_shared<bool>(false);
|
||||
std::shared_ptr<std::string> pattern = std::make_shared<std::string>();
|
||||
auto grep(argument_parser::base_parser& parser, std::string const& filename, std::regex const& pattern) {
|
||||
if (filename.empty()) {
|
||||
std::cerr << "Missing filename" << std::endl;
|
||||
parser.display_help(conventions);
|
||||
exit(-1);
|
||||
}
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Could not open file: \"" << filename << '"' << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
auto grep_impl = [&parser] (std::string const& filename, std::string const& pattern) {
|
||||
if (filename.empty()) {
|
||||
std::cerr << "Missing filename" << std::endl;
|
||||
parser.display_help(conventions);
|
||||
exit(-1);
|
||||
for (std::string line; std::getline(file, line);) {
|
||||
if (std::regex_search(line, pattern)) {
|
||||
std::cout << line << std::endl;
|
||||
}
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Could not open file: \"" << filename << '"' << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
if (line.find(pattern) != std::string::npos) {
|
||||
std::cout << line << std::endl;
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
};
|
||||
}
|
||||
|
||||
const auto filename_action = argument_parser::helpers::make_parametered_action<std::string>([filename, grep_called, grep_requested, &grep_impl, pattern](std::string const& file_name) {
|
||||
std::ifstream file(file_name);
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("Could not open file");
|
||||
}
|
||||
*filename = file_name;
|
||||
file.close();
|
||||
if (*grep_requested && !(*grep_called)) {
|
||||
grep_impl(file_name, *pattern);
|
||||
*grep_called = true;
|
||||
}
|
||||
});
|
||||
|
||||
const auto grep = argument_parser::helpers::make_parametered_action<std::string>([filename, &parser, &grep_impl, grep_requested, grep_called, pattern](std::string const& p) {
|
||||
*grep_requested = true;
|
||||
if (!filename || filename -> empty()) {
|
||||
*grep_called = false;
|
||||
*pattern = p;
|
||||
return;
|
||||
}
|
||||
grep_impl(*filename, p);
|
||||
*grep_called = true;
|
||||
});
|
||||
|
||||
return std::pair { filename_action, grep };
|
||||
file.close();
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto parser = argument_parser::parser{};
|
||||
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<std::string>("f", "file", "File to grep, required only if using grep", false);
|
||||
parser.add_argument<std::regex>("g", "grep", "Grep pattern, required only if using grep", false);
|
||||
parser.add_argument("c", "cat", "Prints the content of the file", cat, false);
|
||||
parser.add_argument("h", "help", "Displays this help text.", argument_parser::helpers::make_non_parametered_action([&parser]{
|
||||
parser.display_help(conventions);
|
||||
}), false);
|
||||
|
||||
parser.add_argument<std::string>("t", "test_store", "Test store", false);
|
||||
parser.add_argument<Point>("p", "point", "Test point", false);
|
||||
parser.handle_arguments(conventions);
|
||||
|
||||
auto store = parser.get_optional<std::string>("test_store");
|
||||
if (store.has_value()) {
|
||||
std::cout << "Stored value: " << store.value() << std::endl;
|
||||
} else {
|
||||
std::cout << "No stored value." << std::endl;
|
||||
}
|
||||
|
||||
auto point = parser.get_optional<Point>("point");
|
||||
if (point.has_value()) {
|
||||
std::cout << "Stored point: " << point.value().x << ", " << point.value().y << std::endl;
|
||||
} else {
|
||||
std::cout << "No stored point." << std::endl;
|
||||
auto filename = parser.get_optional<std::string>("file");
|
||||
auto pattern = parser.get_optional<std::regex>("grep");
|
||||
if (filename && pattern) {
|
||||
grep(parser, filename.value(), pattern.value());
|
||||
} else if (filename) {
|
||||
std::cerr << "Missing grep pattern" << std::endl;
|
||||
parser.display_help(conventions);
|
||||
exit(-1);
|
||||
} else if (pattern) {
|
||||
std::cerr << "Missing filename" << std::endl;
|
||||
parser.display_help(conventions);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user