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:
2025-10-03 23:56:54 +04:00
parent ef0a0c06f8
commit ebbf3983de
10 changed files with 190 additions and 23 deletions

27
include/argparse Normal file
View 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>

View File

@@ -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;
};
}

View 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

View File

@@ -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 {

View 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

View File

@@ -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