mirror of
https://github.com/sametersoylu/argument-parser.git
synced 2026-05-28 20:08:10 +00:00
chore: refactor class names.
feat: introduce parser settings for controlling the exit behavior.
This commit is contained in:
@@ -42,7 +42,7 @@ template <typename T> struct parser_trait<std::vector<T>> {
|
|||||||
using namespace argument_parser::v2::flags;
|
using namespace argument_parser::v2::flags;
|
||||||
|
|
||||||
auto main() -> int {
|
auto main() -> int {
|
||||||
argument_parser::v2::parser parser(true);
|
argument_parser::v2::parser parser({.should_exit_on_help = false});
|
||||||
|
|
||||||
new_argument()
|
new_argument()
|
||||||
.positional("count")
|
.positional("count")
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ namespace argument_parser::builder {
|
|||||||
reference,
|
reference,
|
||||||
accumulate,
|
accumulate,
|
||||||
count,
|
count,
|
||||||
nonparametered_action,
|
action_no_param,
|
||||||
parametered_action
|
action_with_param
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class extra_capability : unsigned { Store = static_cast<unsigned>(v2_flag::Reference) + 1, Flag };
|
enum class extra_capability : unsigned { Store = static_cast<unsigned>(v2_flag::Reference) + 1, Flag };
|
||||||
@@ -321,9 +321,9 @@ namespace argument_parser::builder {
|
|||||||
argument<builder_mask::remove(current_mask, builder_mask::value_mode_group), non_type>;
|
argument<builder_mask::remove(current_mask, builder_mask::value_mode_group), non_type>;
|
||||||
|
|
||||||
next_argument next{*this};
|
next_argument next{*this};
|
||||||
next.m_action = std::make_shared<argument_parser::non_parametered_action>(
|
next.m_action = std::make_shared<argument_parser::action_no_param>(
|
||||||
std::function<void()>(std::forward<Callable>(handler)));
|
std::function<void()>(std::forward<Callable>(handler)));
|
||||||
next.m_value_mode = value_mode::nonparametered_action;
|
next.m_value_mode = value_mode::action_no_param;
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,9 +338,9 @@ namespace argument_parser::builder {
|
|||||||
using next_argument = argument<builder_mask::remove(current_mask, builder_mask::value_mode_group), T>;
|
using next_argument = argument<builder_mask::remove(current_mask, builder_mask::value_mode_group), T>;
|
||||||
|
|
||||||
next_argument next{*this};
|
next_argument next{*this};
|
||||||
next.m_action = std::make_shared<argument_parser::parametered_action<T>>(
|
next.m_action = std::make_shared<argument_parser::action_with_param<T>>(
|
||||||
std::function<void(const T &)>(std::forward<Callable>(handler)));
|
std::function<void(const T &)>(std::forward<Callable>(handler)));
|
||||||
next.m_value_mode = value_mode::parametered_action;
|
next.m_value_mode = value_mode::action_with_param;
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,8 +352,8 @@ namespace argument_parser::builder {
|
|||||||
case value_mode::flag:
|
case value_mode::flag:
|
||||||
build_flag(parser);
|
build_flag(parser);
|
||||||
return;
|
return;
|
||||||
case value_mode::nonparametered_action:
|
case value_mode::action_no_param:
|
||||||
build_nonparametered_action(parser);
|
build_action_with_no_param(parser);
|
||||||
return;
|
return;
|
||||||
case value_mode::store:
|
case value_mode::store:
|
||||||
if constexpr (!std::is_same_v<store_type, non_type>) {
|
if constexpr (!std::is_same_v<store_type, non_type>) {
|
||||||
@@ -374,9 +374,9 @@ namespace argument_parser::builder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case value_mode::parametered_action:
|
case value_mode::action_with_param:
|
||||||
if constexpr (!std::is_same_v<store_type, non_type>) {
|
if constexpr (!std::is_same_v<store_type, non_type>) {
|
||||||
build_parametered_action(parser);
|
build_action_with_param(parser);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -535,9 +535,9 @@ namespace argument_parser::builder {
|
|||||||
parser.template add_argument<store_type>(pairs);
|
parser.template add_argument<store_type>(pairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto build_parametered_action(argument_parser::v2::base_parser &parser) const -> void {
|
auto build_action_with_param(argument_parser::v2::base_parser &parser) const -> void {
|
||||||
auto const *typed_action =
|
auto const *typed_action =
|
||||||
dynamic_cast<argument_parser::parametered_action<store_type> const *>(m_action.get());
|
dynamic_cast<argument_parser::action_with_param<store_type> const *>(m_action.get());
|
||||||
if (typed_action == nullptr) {
|
if (typed_action == nullptr) {
|
||||||
throw std::logic_error("Stored action is not compatible with the requested parameter type.");
|
throw std::logic_error("Stored action is not compatible with the requested parameter type.");
|
||||||
}
|
}
|
||||||
@@ -547,15 +547,14 @@ namespace argument_parser::builder {
|
|||||||
parser.template add_argument<store_type>(pairs);
|
parser.template add_argument<store_type>(pairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto build_nonparametered_action(argument_parser::v2::base_parser &parser) const -> void {
|
auto build_action_with_no_param(argument_parser::v2::base_parser &parser) const -> void {
|
||||||
auto const *nonparametered_action =
|
auto const *nonparametered_action = dynamic_cast<argument_parser::action_no_param const *>(m_action.get());
|
||||||
dynamic_cast<argument_parser::non_parametered_action const *>(m_action.get());
|
|
||||||
if (nonparametered_action == nullptr) {
|
if (nonparametered_action == nullptr) {
|
||||||
throw std::logic_error("Stored action is not a non-parametered action.");
|
throw std::logic_error("Stored action is not a non-parametered action.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_positional()) {
|
if (is_positional()) {
|
||||||
auto wrapped_action = argument_parser::helpers::make_parametered_action<std::string>(
|
auto wrapped_action = argument_parser::helpers::make_action<std::string>(
|
||||||
[action = *nonparametered_action](std::string const &) { action.invoke(); });
|
[action = *nonparametered_action](std::string const &) { action.invoke(); });
|
||||||
auto pairs = make_typed_pairs<std::string>();
|
auto pairs = make_typed_pairs<std::string>();
|
||||||
pairs[argument_parser::v2::flags::Action] = wrapped_action;
|
pairs[argument_parser::v2::flags::Action] = wrapped_action;
|
||||||
|
|||||||
@@ -104,9 +104,9 @@ namespace argument_parser {
|
|||||||
[[nodiscard]] virtual std::unique_ptr<action_base> clone() const = 0;
|
[[nodiscard]] virtual std::unique_ptr<action_base> clone() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T> class parametered_action : public action_base {
|
template <typename T> class action_with_param : public action_base {
|
||||||
public:
|
public:
|
||||||
explicit parametered_action(std::function<void(const T &)> const &handler) : handler(handler) {}
|
explicit action_with_param(std::function<void(const T &)> const &handler) : handler(handler) {}
|
||||||
using parameter_type = T;
|
using parameter_type = T;
|
||||||
void invoke(const T &arg) const {
|
void invoke(const T &arg) const {
|
||||||
handler(arg);
|
handler(arg);
|
||||||
@@ -149,16 +149,16 @@ namespace argument_parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::unique_ptr<action_base> clone() const override {
|
[[nodiscard]] std::unique_ptr<action_base> clone() const override {
|
||||||
return std::make_unique<parametered_action<T>>(handler);
|
return std::make_unique<action_with_param<T>>(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::function<void(const T &)> handler;
|
std::function<void(const T &)> handler;
|
||||||
};
|
};
|
||||||
|
|
||||||
class non_parametered_action : public action_base {
|
class action_no_param : public action_base {
|
||||||
public:
|
public:
|
||||||
explicit non_parametered_action(std::function<void()> const &handler) : handler(handler) {}
|
explicit action_no_param(std::function<void()> const &handler) : handler(handler) {}
|
||||||
|
|
||||||
void invoke() const override {
|
void invoke() const override {
|
||||||
handler();
|
handler();
|
||||||
@@ -177,7 +177,7 @@ namespace argument_parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::unique_ptr<action_base> clone() const override {
|
[[nodiscard]] std::unique_ptr<action_base> clone() const override {
|
||||||
return std::make_unique<non_parametered_action>(handler);
|
return std::make_unique<action_no_param>(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -230,16 +230,27 @@ namespace argument_parser {
|
|||||||
};
|
};
|
||||||
|
|
||||||
namespace helpers {
|
namespace helpers {
|
||||||
template <typename T>
|
template <typename T> static action_with_param<T> make_action(std::function<void(const T &)> const &function) {
|
||||||
static parametered_action<T> make_parametered_action(std::function<void(const T &)> const &function) {
|
return action_with_param<T>(function);
|
||||||
return parametered_action<T>(function);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static non_parametered_action make_non_parametered_action(std::function<void()> const &function) {
|
static action_no_param make_action(std::function<void()> const &function) {
|
||||||
return non_parametered_action(function);
|
return action_no_param(function);
|
||||||
}
|
}
|
||||||
} // namespace helpers
|
} // namespace helpers
|
||||||
|
|
||||||
|
struct parser_settings {
|
||||||
|
bool should_exit_on_error = true;
|
||||||
|
bool should_exit_on_missing_required = true;
|
||||||
|
bool should_exit_on_unknown_argument = true;
|
||||||
|
bool should_exit_on_help = true;
|
||||||
|
|
||||||
|
bool ignore_unknown_arguments = false;
|
||||||
|
bool ignore_errors = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr static inline parser_settings no_exit{false, false, false, false};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Base class for parsing arguments from the command line.
|
* @brief Base class for parsing arguments from the command line.
|
||||||
*
|
*
|
||||||
@@ -251,7 +262,7 @@ namespace argument_parser {
|
|||||||
public:
|
public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void add_argument(std::string const &short_arg, std::string const &long_arg, std::string const &help_text,
|
void add_argument(std::string const &short_arg, std::string const &long_arg, std::string const &help_text,
|
||||||
parametered_action<T> const &action, bool required) {
|
action_with_param<T> const &action, bool required) {
|
||||||
base_add_argument(short_arg, long_arg, help_text, action, required);
|
base_add_argument(short_arg, long_arg, help_text, action, required);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,7 +273,7 @@ namespace argument_parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void add_argument(std::string const &short_arg, std::string const &long_arg, std::string const &help_text,
|
void add_argument(std::string const &short_arg, std::string const &long_arg, std::string const &help_text,
|
||||||
non_parametered_action const &action, bool required) {
|
action_no_param const &action, bool required) {
|
||||||
base_add_argument(short_arg, long_arg, help_text, action, required);
|
base_add_argument(short_arg, long_arg, help_text, action, required);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,7 +284,7 @@ namespace argument_parser {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void add_positional_argument(std::string const &name, std::string const &help_text,
|
void add_positional_argument(std::string const &name, std::string const &help_text,
|
||||||
parametered_action<T> const &action, bool required,
|
action_with_param<T> const &action, bool required,
|
||||||
std::optional<int> position = std::nullopt) {
|
std::optional<int> position = std::nullopt) {
|
||||||
base_add_positional_argument(name, help_text, action, required, position);
|
base_add_positional_argument(name, help_text, action, required, position);
|
||||||
}
|
}
|
||||||
@@ -286,7 +297,7 @@ namespace argument_parser {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void add_positional_accumulator(std::string const &name, std::string const &help_text,
|
void add_positional_accumulator(std::string const &name, std::string const &help_text,
|
||||||
parametered_action<T> const &action, bool required,
|
action_with_param<T> const &action, bool required,
|
||||||
std::optional<int> position = std::nullopt) {
|
std::optional<int> position = std::nullopt) {
|
||||||
base_add_positional_argument(name, help_text, action, required, position, true);
|
base_add_positional_argument(name, help_text, action, required, position, true);
|
||||||
}
|
}
|
||||||
@@ -335,7 +346,12 @@ namespace argument_parser {
|
|||||||
|
|
||||||
void on_complete(std::function<void(base_parser const &)> const &handler, bool to_start);
|
void on_complete(std::function<void(base_parser const &)> const &handler, bool to_start);
|
||||||
|
|
||||||
|
void set_settings(parser_settings const &settings) {
|
||||||
|
m_settings = settings;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
parser_settings m_settings;
|
||||||
struct found_argument {
|
struct found_argument {
|
||||||
std::string key;
|
std::string key;
|
||||||
argument arg;
|
argument arg;
|
||||||
@@ -343,15 +359,12 @@ namespace argument_parser {
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool test_conventions(std::initializer_list<conventions::convention const *const> convention_types,
|
bool test_conventions(std::initializer_list<conventions::convention const *const> convention_types,
|
||||||
std::vector<found_argument> &found_arguments,
|
std::vector<found_argument> &found_arguments, std::optional<argument> &found_help,
|
||||||
std::optional<argument> &found_help, std::vector<std::string>::iterator &it,
|
std::vector<std::string>::iterator &it, std::stringstream &error_stream);
|
||||||
std::stringstream &error_stream);
|
|
||||||
void extract_arguments(std::initializer_list<conventions::convention const *const> convention_types,
|
void extract_arguments(std::initializer_list<conventions::convention const *const> convention_types,
|
||||||
std::vector<found_argument> &found_arguments,
|
std::vector<found_argument> &found_arguments, std::optional<argument> &found_help);
|
||||||
std::optional<argument> &found_help);
|
|
||||||
|
|
||||||
void invoke_arguments(std::vector<found_argument> &found_arguments,
|
void invoke_arguments(std::vector<found_argument> &found_arguments, std::optional<argument> const &found_help);
|
||||||
std::optional<argument> const &found_help);
|
|
||||||
void enforce_creation_thread() const;
|
void enforce_creation_thread() const;
|
||||||
|
|
||||||
void assert_argument_not_exist(std::string const &short_arg, std::string const &long_arg) const;
|
void assert_argument_not_exist(std::string const &short_arg, std::string const &long_arg) const;
|
||||||
@@ -379,13 +392,12 @@ namespace argument_parser {
|
|||||||
assert_argument_not_exist(short_arg, long_arg);
|
assert_argument_not_exist(short_arg, long_arg);
|
||||||
int id = id_counter.fetch_add(1);
|
int id = id_counter.fetch_add(1);
|
||||||
if constexpr (std::is_same_v<StoreType, void>) {
|
if constexpr (std::is_same_v<StoreType, void>) {
|
||||||
auto action =
|
auto action = helpers::make_action([id, this] { stored_arguments[id] = std::any{true}; });
|
||||||
helpers::make_non_parametered_action([id, this] { stored_arguments[id] = std::any{true}; });
|
|
||||||
argument arg(id, short_arg + "|" + long_arg, action);
|
argument arg(id, short_arg + "|" + long_arg, action);
|
||||||
set_argument_status(required, help_text, arg);
|
set_argument_status(required, help_text, arg);
|
||||||
place_argument(id, arg, short_arg, long_arg);
|
place_argument(id, arg, short_arg, long_arg);
|
||||||
} else {
|
} else {
|
||||||
auto action = helpers::make_parametered_action<StoreType>(
|
auto action = helpers::make_action<StoreType>(
|
||||||
[id, this](StoreType const &value) { stored_arguments[id] = std::any{value}; });
|
[id, this](StoreType const &value) { stored_arguments[id] = std::any{value}; });
|
||||||
argument arg(id, short_arg + "|" + long_arg, action);
|
argument arg(id, short_arg + "|" + long_arg, action);
|
||||||
set_argument_status(required, help_text, arg);
|
set_argument_status(required, help_text, arg);
|
||||||
@@ -412,7 +424,7 @@ namespace argument_parser {
|
|||||||
std::optional<int> position = std::nullopt) {
|
std::optional<int> position = std::nullopt) {
|
||||||
assert_positional_not_exist(name);
|
assert_positional_not_exist(name);
|
||||||
int id = id_counter.fetch_add(1);
|
int id = id_counter.fetch_add(1);
|
||||||
auto action = helpers::make_parametered_action<StoreType>(
|
auto action = helpers::make_action<StoreType>(
|
||||||
[id, this](StoreType const &value) { stored_arguments[id] = std::any{value}; });
|
[id, this](StoreType const &value) { stored_arguments[id] = std::any{value}; });
|
||||||
argument arg(id, name, action);
|
argument arg(id, name, action);
|
||||||
set_argument_status(required, help_text, arg);
|
set_argument_status(required, help_text, arg);
|
||||||
|
|||||||
18
src/headers/parser/exceptions.hpp
Normal file
18
src/headers/parser/exceptions.hpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef ARGUMENT_PARSER_EXCEPTIONS_HPP
|
||||||
|
#define ARGUMENT_PARSER_EXCEPTIONS_HPP
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace argument_parser {
|
||||||
|
namespace parser {
|
||||||
|
class unknown_argument_exception : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
unknown_argument_exception(const std::string &message) : std::runtime_error(message) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace parser
|
||||||
|
} // namespace argument_parser
|
||||||
|
|
||||||
|
#endif // ARGUMENT_PARSER_EXCEPTIONS_HPP
|
||||||
@@ -57,15 +57,15 @@ namespace argument_parser::v2 {
|
|||||||
|
|
||||||
class base_parser : argument_parser::base_parser {
|
class base_parser : argument_parser::base_parser {
|
||||||
public:
|
public:
|
||||||
template <typename T> using typed_flag_value = std::variant<std::string, parametered_action<T>, bool, int, T *>;
|
template <typename T> using typed_flag_value = std::variant<std::string, action_with_param<T>, bool, int, T *>;
|
||||||
using non_typed_flag_value = std::variant<std::string, non_parametered_action, bool, int>;
|
using non_typed_flag_value = std::variant<std::string, action_no_param, bool, int>;
|
||||||
|
|
||||||
template <typename T> using typed_argument_pair = std::pair<add_argument_flags, typed_flag_value<T>>;
|
template <typename T> using typed_argument_pair = std::pair<add_argument_flags, typed_flag_value<T>>;
|
||||||
using non_typed_argument_pair = std::pair<add_argument_flags, non_typed_flag_value>;
|
using non_typed_argument_pair = std::pair<add_argument_flags, non_typed_flag_value>;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void add_argument(std::unordered_map<add_argument_flags, typed_flag_value<T>> const &argument_pairs) {
|
void add_argument(std::unordered_map<add_argument_flags, typed_flag_value<T>> const &argument_pairs) {
|
||||||
add_argument_impl<true, parametered_action<T>, T>(argument_pairs);
|
add_argument_impl<true, action_with_param<T>, T>(argument_pairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void add_argument(std::initializer_list<typed_argument_pair<T>> const &pairs) {
|
template <typename T> void add_argument(std::initializer_list<typed_argument_pair<T>> const &pairs) {
|
||||||
@@ -89,7 +89,7 @@ namespace argument_parser::v2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void add_argument(std::unordered_map<add_argument_flags, non_typed_flag_value> const &argument_pairs) {
|
void add_argument(std::unordered_map<add_argument_flags, non_typed_flag_value> const &argument_pairs) {
|
||||||
add_argument_impl<false, non_parametered_action, void>(argument_pairs);
|
add_argument_impl<false, action_no_param, void>(argument_pairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
argument_parser::base_parser &to_v1() {
|
argument_parser::base_parser &to_v1() {
|
||||||
@@ -122,7 +122,7 @@ namespace argument_parser::v2 {
|
|||||||
void prepare_help_flag(bool should_exit = true) {
|
void prepare_help_flag(bool should_exit = true) {
|
||||||
add_argument({{flags::ShortArgument, "h"},
|
add_argument({{flags::ShortArgument, "h"},
|
||||||
{flags::LongArgument, "help"},
|
{flags::LongArgument, "help"},
|
||||||
{flags::Action, helpers::make_non_parametered_action([this, should_exit] {
|
{flags::Action, helpers::make_action([this, should_exit] {
|
||||||
this->display_help(this->current_conventions());
|
this->display_help(this->current_conventions());
|
||||||
if (should_exit) {
|
if (should_exit) {
|
||||||
std::exit(0);
|
std::exit(0);
|
||||||
@@ -131,6 +131,10 @@ namespace argument_parser::v2 {
|
|||||||
{flags::HelpText, "Prints this help text."}});
|
{flags::HelpText, "Prints this help text."}});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void set_settings(parser_settings const &settings) {
|
||||||
|
base::set_settings(settings);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <bool IsTyped, typename ActionType, typename T, typename ArgsMap>
|
template <bool IsTyped, typename ActionType, typename T, typename ArgsMap>
|
||||||
void add_argument_impl(ArgsMap const &argument_pairs) {
|
void add_argument_impl(ArgsMap const &argument_pairs) {
|
||||||
@@ -224,7 +228,7 @@ namespace argument_parser::v2 {
|
|||||||
|
|
||||||
base::add_argument<typename T::value_type>(
|
base::add_argument<typename T::value_type>(
|
||||||
short_arg, long_arg, help_text,
|
short_arg, long_arg, help_text,
|
||||||
*static_cast<parametered_action<typename T::value_type> *>(&(*action)), required);
|
*static_cast<action_with_param<typename T::value_type> *>(&(*action)), required);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -375,7 +379,7 @@ namespace argument_parser::v2 {
|
|||||||
if constexpr (!std::is_same_v<T, void> && deducers::is_vector_v<T>) {
|
if constexpr (!std::is_same_v<T, void> && deducers::is_vector_v<T>) {
|
||||||
base::add_positional_accumulator<typename T::value_type>(
|
base::add_positional_accumulator<typename T::value_type>(
|
||||||
positional_name, help_text,
|
positional_name, help_text,
|
||||||
*static_cast<parametered_action<typename T::value_type> *>(&(*action)), required, position);
|
*static_cast<action_with_param<typename T::value_type> *>(&(*action)), required, position);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -452,16 +456,15 @@ namespace argument_parser::v2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> std::unique_ptr<action_base> make_reference_action(T *target) {
|
template <typename T> std::unique_ptr<action_base> make_reference_action(T *target) {
|
||||||
return helpers::make_parametered_action<T>([target](T const &value) { *target = value; }).clone();
|
return helpers::make_action<T>([target](T const &value) { *target = value; }).clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Vector> std::unique_ptr<action_base> make_accumulate_ref_action(Vector *target) {
|
template <typename Vector> std::unique_ptr<action_base> make_accumulate_ref_action(Vector *target) {
|
||||||
if constexpr (std::is_same_v<Vector, int>) {
|
if constexpr (std::is_same_v<Vector, int>) {
|
||||||
return helpers::make_non_parametered_action([target]() { *target += 1; }).clone();
|
return helpers::make_action([target]() { *target += 1; }).clone();
|
||||||
} else {
|
} else {
|
||||||
using Value = typename Vector::value_type;
|
using Value = typename Vector::value_type;
|
||||||
return helpers::make_parametered_action<Value>(
|
return helpers::make_action<Value>([target](Value const &value) { target->emplace_back(value); })
|
||||||
[target](Value const &value) { target->emplace_back(value); })
|
|
||||||
.clone();
|
.clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ namespace argument_parser {
|
|||||||
namespace v2 {
|
namespace v2 {
|
||||||
class linux_parser : public v2::base_parser {
|
class linux_parser : public v2::base_parser {
|
||||||
public:
|
public:
|
||||||
linux_parser(bool should_exit = true);
|
linux_parser(parser_settings const &settings = {});
|
||||||
using base_parser::display_help;
|
using base_parser::display_help;
|
||||||
};
|
};
|
||||||
} // namespace v2
|
} // namespace v2
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace argument_parser {
|
|||||||
namespace v2 {
|
namespace v2 {
|
||||||
class macos_parser : public v2::base_parser {
|
class macos_parser : public v2::base_parser {
|
||||||
public:
|
public:
|
||||||
explicit macos_parser(bool should_exit = true);
|
explicit macos_parser(parser_settings const &settings = {});
|
||||||
using base_parser::display_help;
|
using base_parser::display_help;
|
||||||
};
|
};
|
||||||
} // namespace v2
|
} // namespace v2
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace argument_parser {
|
|||||||
namespace v2 {
|
namespace v2 {
|
||||||
class windows_parser : public v2::base_parser {
|
class windows_parser : public v2::base_parser {
|
||||||
public:
|
public:
|
||||||
windows_parser(bool should_exit = true);
|
windows_parser(parser_settings const &settings = {});
|
||||||
using base_parser::display_help;
|
using base_parser::display_help;
|
||||||
};
|
};
|
||||||
} // namespace v2
|
} // namespace v2
|
||||||
|
|||||||
Reference in New Issue
Block a user