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:
@@ -7,4 +7,12 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_Release ${CMAKE_CURRENT_SOURCE_DIR}/bin/release)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_Debug ${CMAKE_CURRENT_SOURCE_DIR}/bin/debug)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
include_directories(include)
|
||||
include_directories(include/parser)
|
||||
include_directories(include/conventions)
|
||||
include_directories(include/conventions/implementations)
|
||||
include_directories(include/parser/platform_headers)
|
||||
|
||||
add_executable(test src/main.cpp)
|
||||
27
include/argparse
Normal file
27
include/argparse
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#ifndef ARGPARSE_HPP
|
||||
#define ARGPARSE_HPP
|
||||
#include <argument_parser.hpp>
|
||||
#
|
||||
#ifdef __linux__
|
||||
#include <linux_parser.hpp>
|
||||
namespace argument_parser {
|
||||
using parser = linux_parser;
|
||||
}
|
||||
#elif __APPLE__
|
||||
#include <macos_parser.hpp>
|
||||
namespace argument_parser {
|
||||
using parser = macos_parser;
|
||||
}
|
||||
#elif _WIN32
|
||||
#include <windows_parser.hpp>
|
||||
namespace argument_parser {
|
||||
using parser = windows_parser;
|
||||
}
|
||||
#else
|
||||
#error "Unsupported platform"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <base_convention.hpp>
|
||||
#include <gnu_argument_convention.hpp>
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "base_convention.hpp"
|
||||
#include <base_convention.hpp>
|
||||
#include <atomic>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <type_traits>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace argument_parser {
|
||||
namespace function {
|
||||
template<typename fn>
|
||||
@@ -501,6 +502,8 @@ namespace argument_parser {
|
||||
|
||||
friend class linux_parser;
|
||||
friend class windows_parser;
|
||||
friend class macos_parser;
|
||||
friend class fake_parser;
|
||||
};
|
||||
}
|
||||
|
||||
29
include/parser/fake_parser.hpp
Normal file
29
include/parser/fake_parser.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef FAKE_PARSER_HPP
|
||||
#define FAKE_PARSER_HPP
|
||||
|
||||
#include <argument_parser.hpp>
|
||||
#include <crt_externs.h>
|
||||
#include <initializer_list>
|
||||
#include <string>
|
||||
|
||||
namespace argument_parser {
|
||||
class fake_parser : public base_parser {
|
||||
public:
|
||||
fake_parser(std::string const& program_name, std::vector<std::string> const& arguments) {
|
||||
this->program_name = program_name;
|
||||
parsed_arguments = arguments;
|
||||
}
|
||||
|
||||
fake_parser(std::string const& program_name, std::vector<std::string>&& arguments) {
|
||||
this->program_name = program_name;
|
||||
parsed_arguments = std::move(arguments);
|
||||
}
|
||||
|
||||
fake_parser(std::string const& program_name, std::initializer_list<std::string> const& arguments) :
|
||||
fake_parser(program_name, std::vector<std::string>(arguments)) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#ifdef __linux__
|
||||
#ifndef LINUX_PARSER_HPP
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include "argument_parser.hpp"
|
||||
#include <argument_parser.hpp>
|
||||
|
||||
namespace argument_parser {
|
||||
class linux_parser : public base_parser {
|
||||
27
include/parser/platform_headers/macos_parser.hpp
Normal file
27
include/parser/platform_headers/macos_parser.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#ifdef __APPLE__
|
||||
#ifndef MACOS_PARSER_HPP
|
||||
#define MACOS_PARSER_HPP
|
||||
|
||||
#include <argument_parser.hpp>
|
||||
#include <crt_externs.h>
|
||||
#include <string>
|
||||
|
||||
namespace argument_parser {
|
||||
class macos_parser : public base_parser {
|
||||
public:
|
||||
macos_parser() {
|
||||
int argc = *_NSGetArgc();
|
||||
char **argv = *_NSGetArgv();
|
||||
if (argc > 0 && argv != nullptr && argv[0] != nullptr) {
|
||||
program_name = argv[0];
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (argv[i] != nullptr) parsed_arguments.emplace_back(argv[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -2,7 +2,7 @@
|
||||
#ifdef _WIN32
|
||||
#ifndef WINDOWS_PARSER_HPP
|
||||
|
||||
#include "argument_parser.hpp"
|
||||
#include <argument_parser.hpp>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
@@ -11,7 +11,9 @@ namespace argument_parser {
|
||||
public:
|
||||
windows_parser() {
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
using parser = windows_parser;
|
||||
}
|
||||
|
||||
#endif
|
||||
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