feat(parser): add platform-specific parsers and argument conventions
Implement platform-specific parsers for Windows, Linux, and macOS Add GNU argument convention implementations Create fake parser for testing purposes Update CMakeLists to include new headers
This commit is contained in:
105
src/main.cpp
105
src/main.cpp
@@ -1,32 +1,103 @@
|
||||
#include "../include/argument_parser.hpp"
|
||||
#include "../include/linux_parser.hpp"
|
||||
#include "../include/gnu_argument_convention.hpp"
|
||||
#include <initializer_list>
|
||||
#include "argument_parser.hpp"
|
||||
#include "fake_parser.hpp"
|
||||
#include <argparse>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using namespace argument_parser::conventions;
|
||||
|
||||
int main() {
|
||||
auto parametered_action = argument_parser::helpers::make_parametered_action_ptr<std::string>([](std::string const& test) {
|
||||
std::cout << test << std::endl;
|
||||
});
|
||||
const std::initializer_list<argument_parser::conventions::convention const* const> conventions = {
|
||||
&gnu_argument_convention,
|
||||
&gnu_equal_argument_convention
|
||||
};
|
||||
|
||||
auto parser = argument_parser::linux_parser{};
|
||||
parser.add_argument("e", "echo", "echoes given variable", *parametered_action, false);
|
||||
parser.add_argument("re", "required_echo", "required echo", *parametered_action, true);
|
||||
const auto echo = argument_parser::helpers::make_parametered_action<std::string>([](std::string const& text) {
|
||||
std::cout << text << std::endl;
|
||||
});
|
||||
|
||||
std::initializer_list<argument_parser::conventions::convention const* const> conventions = {
|
||||
&gnu_argument_convention,
|
||||
&gnu_equal_argument_convention
|
||||
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()) {
|
||||
throw std::runtime_error("Could not open file");
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
std::cout << line << std::endl;
|
||||
}
|
||||
|
||||
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_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);
|
||||
}
|
||||
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();
|
||||
};
|
||||
|
||||
parser.add_argument("h", "help", "Displays this help text.", argument_parser::helpers::make_non_parametered_action([&parser, conventions]{
|
||||
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 };
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::vector<std::string> fake_args = { "-g", "add", "-f", "src/main.cpp" };
|
||||
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("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);
|
||||
parser.add_argument("h", "help", "Displays this help text.", argument_parser::helpers::make_non_parametered_action([&parser]{
|
||||
parser.display_help(conventions);
|
||||
}), false);
|
||||
|
||||
parser.handle_arguments(conventions);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user