mirror of
https://github.com/sametersoylu/argument-parser.git
synced 2026-04-13 03:41:18 +00:00
feat: Introduce a modular argument convention system for GNU and Windows styles, add .clang-format, and update CMake configuration to C++17.
This commit is contained in:
35
src/headers/conventions/base_convention.hpp
Normal file
35
src/headers/conventions/base_convention.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#ifndef BASE_CONVENTION_HPP
|
||||
#define BASE_CONVENTION_HPP
|
||||
|
||||
namespace argument_parser::conventions {
|
||||
enum class argument_type { SHORT, LONG, POSITIONAL, INTERCHANGABLE, ERROR };
|
||||
|
||||
using parsed_argument = std::pair<argument_type, std::string>;
|
||||
|
||||
class base_convention {
|
||||
public:
|
||||
virtual std::string extract_value(std::string const &) const = 0;
|
||||
virtual parsed_argument get_argument(std::string const &) const = 0;
|
||||
virtual bool requires_next_token() const = 0;
|
||||
virtual std::string name() const = 0;
|
||||
virtual std::string short_prec() const = 0;
|
||||
virtual std::string long_prec() const = 0;
|
||||
|
||||
protected:
|
||||
base_convention() = default;
|
||||
~base_convention() = default;
|
||||
};
|
||||
using convention = base_convention;
|
||||
} // namespace argument_parser::conventions
|
||||
|
||||
namespace argument_parser::conventions::helpers {
|
||||
static std::string to_lower(std::string s);
|
||||
|
||||
static std::string to_upper(std::string s);
|
||||
} // namespace argument_parser::conventions::helpers
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
#include "base_convention.hpp"
|
||||
|
||||
#ifndef GNU_ARGUMENT_CONVENTION_HPP
|
||||
#define GNU_ARGUMENT_CONVENTION_HPP
|
||||
|
||||
namespace argument_parser::conventions::implementations {
|
||||
|
||||
class gnu_argument_convention : public base_convention {
|
||||
public:
|
||||
parsed_argument get_argument(std::string const &raw) const override;
|
||||
std::string extract_value(std::string const & /*raw*/) const override;
|
||||
|
||||
bool requires_next_token() const override;
|
||||
|
||||
std::string name() const override;
|
||||
|
||||
std::string short_prec() const override;
|
||||
|
||||
std::string long_prec() const override;
|
||||
|
||||
static gnu_argument_convention instance;
|
||||
|
||||
private:
|
||||
gnu_argument_convention() = default;
|
||||
};
|
||||
|
||||
class gnu_equal_argument_convention : public base_convention {
|
||||
public:
|
||||
parsed_argument get_argument(std::string const &raw) const override;
|
||||
std::string extract_value(std::string const &raw) const override;
|
||||
bool requires_next_token() const override;
|
||||
std::string name() const override;
|
||||
std::string short_prec() const override;
|
||||
std::string long_prec() const override;
|
||||
static gnu_equal_argument_convention instance;
|
||||
|
||||
private:
|
||||
gnu_equal_argument_convention() = default;
|
||||
};
|
||||
|
||||
inline gnu_argument_convention gnu_argument_convention::instance{};
|
||||
inline gnu_equal_argument_convention gnu_equal_argument_convention::instance{};
|
||||
} // namespace argument_parser::conventions::implementations
|
||||
|
||||
namespace argument_parser::conventions {
|
||||
static const implementations::gnu_argument_convention gnu_argument_convention =
|
||||
implementations::gnu_argument_convention::instance;
|
||||
static const implementations::gnu_equal_argument_convention gnu_equal_argument_convention =
|
||||
implementations::gnu_equal_argument_convention::instance;
|
||||
} // namespace argument_parser::conventions
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
#include "base_convention.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
#ifndef WINDOWS_ARGUMENT_CONVENTION_HPP
|
||||
#define WINDOWS_ARGUMENT_CONVENTION_HPP
|
||||
|
||||
#ifndef ALLOW_DASH_FOR_WINDOWS
|
||||
#define ALLOW_DASH_FOR_WINDOWS 1
|
||||
#endif
|
||||
|
||||
namespace argument_parser::conventions::implementations {
|
||||
class windows_argument_convention : public base_convention {
|
||||
public:
|
||||
explicit windows_argument_convention(bool accept_dash = true);
|
||||
|
||||
parsed_argument get_argument(std::string const &raw) const override;
|
||||
|
||||
std::string extract_value(std::string const & /*raw*/) const override;
|
||||
|
||||
bool requires_next_token() const override;
|
||||
|
||||
std::string name() const override;
|
||||
|
||||
std::string short_prec() const override;
|
||||
|
||||
std::string long_prec() const override;
|
||||
static windows_argument_convention instance;
|
||||
|
||||
private:
|
||||
bool accept_dash_;
|
||||
};
|
||||
|
||||
class windows_kv_argument_convention : public base_convention {
|
||||
public:
|
||||
explicit windows_kv_argument_convention(bool accept_dash = true);
|
||||
parsed_argument get_argument(std::string const &raw) const override;
|
||||
std::string extract_value(std::string const &raw) const override;
|
||||
bool requires_next_token() const override;
|
||||
std::string name() const override;
|
||||
std::string short_prec() const override;
|
||||
std::string long_prec() const override;
|
||||
static windows_kv_argument_convention instance;
|
||||
|
||||
private:
|
||||
bool accept_dash_;
|
||||
};
|
||||
|
||||
inline windows_argument_convention windows_argument_convention::instance =
|
||||
windows_argument_convention(bool(ALLOW_DASH_FOR_WINDOWS));
|
||||
inline windows_kv_argument_convention windows_kv_argument_convention::instance =
|
||||
windows_kv_argument_convention(bool(ALLOW_DASH_FOR_WINDOWS));
|
||||
} // namespace argument_parser::conventions::implementations
|
||||
|
||||
namespace argument_parser::conventions {
|
||||
static inline const implementations::windows_argument_convention windows_argument_convention =
|
||||
implementations::windows_argument_convention::instance;
|
||||
static inline const implementations::windows_kv_argument_convention windows_equal_argument_convention =
|
||||
implementations::windows_kv_argument_convention::instance;
|
||||
} // namespace argument_parser::conventions
|
||||
|
||||
#endif // WINDOWS_ARGUMENT_CONVENTION_HPP
|
||||
Reference in New Issue
Block a user