diff --git a/include/argparse.hpp b/include/argparse.hpp index 1435e25..7aa2355 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -997,6 +997,11 @@ namespace argparse return false; } + auto get_impl() const -> ArgumentImpl const & + { + return m_impl; + } + private: ArgumentImpl m_impl; }; @@ -1004,87 +1009,87 @@ namespace argparse class StoreAction { public: - auto perform(ArgumentBase const & base, std::any & value, std::function name_for_error, std::string const & val, std::ranges::view auto tokens) const -> void + auto perform(ArgumentImpl const & impl, std::any & value, std::function name_for_error, std::string const & val, std::ranges::view auto tokens) const -> void { - if (base.has_nargs()) + if (impl.has_nargs()) { - if (base.has_nargs_number()) + if (impl.has_nargs_number()) { - parse_arguments_number(base, value, name_for_error, tokens); + parse_arguments_number(impl, value, name_for_error, tokens); } else { - parse_arguments_option(base, value, name_for_error, tokens); + parse_arguments_option(impl, value, name_for_error, tokens); } } else { if (val.empty()) { - value = base.consume_token(tokens.front(), name_for_error); + value = impl.consume_token(tokens.front(), name_for_error); } else { - value = base.process_token(val, name_for_error); + value = impl.process_token(val, name_for_error); } } } - auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto tokens) const -> void + auto check_errors(ArgumentImpl const & impl, std::string_view value, std::ranges::view auto tokens) const -> void { - if (!base.has_nargs() && value.empty() && tokens.empty()) + if (!impl.has_nargs() && value.empty() && tokens.empty()) { - throw parsing_error(std::format("argument {}: expected one argument", base.get_joined_names())); + throw parsing_error(std::format("argument {}: expected one argument", impl.get_joined_names())); } } - auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & impl, std::any & value) const -> void { - value = base.get_default(); + value = impl.get_default(); } private: - auto parse_arguments_number(ArgumentBase const & base, std::any & value, std::function name_for_error, std::ranges::view auto tokens) const -> void + auto parse_arguments_number(ArgumentImpl const & impl, std::any & value, std::function name_for_error, std::ranges::view auto tokens) const -> void { - auto const nargs_number = base.get_nargs_number(); - auto const values = base.consume_tokens(tokens | std::views::take(nargs_number), name_for_error); + auto const nargs_number = impl.get_nargs_number(); + auto const values = impl.consume_tokens(tokens | std::views::take(nargs_number), name_for_error); if (values.size() < nargs_number) { - throw parsing_error(std::format("argument {}: expected {} argument{}", base.get_joined_names(), std::to_string(nargs_number), nargs_number > 1 ? "s" : "")); + throw parsing_error(std::format("argument {}: expected {} argument{}", impl.get_joined_names(), std::to_string(nargs_number), nargs_number > 1 ? "s" : "")); } - value = base.get_transformed(values); + value = impl.get_transformed(values); } - auto parse_arguments_option(ArgumentBase const & base, std::any & value, std::function name_for_error, std::ranges::view auto tokens) const -> void + auto parse_arguments_option(ArgumentImpl const & impl, std::any & value, std::function name_for_error, std::ranges::view auto tokens) const -> void { - switch (base.get_nargs_option()) + switch (impl.get_nargs_option()) { case zero_or_one: { if (!tokens.empty()) { - value = base.consume_token(tokens.front(), name_for_error); + value = impl.consume_token(tokens.front(), name_for_error); } else { - value = base.get_const(); + value = impl.get_const(); } break; } case zero_or_more: { - value = base.parse_arguments(tokens, name_for_error); + value = impl.parse_arguments(tokens, name_for_error); break; } case one_or_more: { - if (auto const values = base.consume_tokens(tokens, name_for_error); !values.empty()) + if (auto const values = impl.consume_tokens(tokens, name_for_error); !values.empty()) { - value = base.get_transformed(values); + value = impl.get_transformed(values); } else { - throw parsing_error(std::format("argument {}: expected at least one argument", base.get_joined_names())); + throw parsing_error(std::format("argument {}: expected at least one argument", impl.get_joined_names())); } break; } @@ -1095,42 +1100,42 @@ namespace argparse class StoreConstAction { public: - auto perform(ArgumentBase const & base, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentImpl const & impl, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { - value = base.get_const(); + value = impl.get_const(); } - auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentImpl const & impl, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", impl.get_joined_names(), value)); } } - auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & impl, std::any & value) const -> void { - value = base.get_default(); + value = impl.get_default(); } }; class StoreTrueAction { public: - auto perform(ArgumentBase const & /* base */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentImpl const & /* impl */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = true; } - auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentImpl const & impl, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", impl.get_joined_names(), value)); } } - auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & /* impl */, std::any & value) const -> void { value = false; } @@ -1139,20 +1144,20 @@ namespace argparse class StoreFalseAction { public: - auto perform(ArgumentBase const & /* base */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentImpl const & /* impl */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = false; } - auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentImpl const & impl, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", impl.get_joined_names(), value)); } } - auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & /* impl */, std::any & value) const -> void { value = true; } @@ -1161,17 +1166,17 @@ namespace argparse class HelpAction { public: - auto perform(ArgumentBase const & /* base */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentImpl const & /* impl */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = true; throw HelpRequested(); } - auto check_errors(ArgumentBase const & /* base */, std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentImpl const & /* impl */, std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void { } - auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & /* impl */, std::any & value) const -> void { value = false; } @@ -1180,17 +1185,17 @@ namespace argparse class VersionAction { public: - auto perform(ArgumentBase const & /* base */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentImpl const & /* impl */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = true; throw VersionRequested(); } - auto check_errors(ArgumentBase const & /* base */, std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentImpl const & /* impl */, std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void { } - auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & /* impl */, std::any & value) const -> void { value = false; } @@ -1199,7 +1204,7 @@ namespace argparse class CountAction { public: - auto perform(ArgumentBase const & /* base */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentImpl const & /* impl */, std::any & value, std::function /* name_for_error */, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { if (!value.has_value()) { @@ -1211,64 +1216,64 @@ namespace argparse } } - auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentImpl const & impl, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", impl.get_joined_names(), value)); } } - auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & impl, std::any & value) const -> void { - value = base.get_default(); + value = impl.get_default(); } }; class AppendAction { public: - auto perform(ArgumentBase const & base, std::any & value, std::function name_for_error, std::string const & val, std::ranges::view auto tokens) const -> void + auto perform(ArgumentImpl const & impl, std::any & value, std::function name_for_error, std::string const & val, std::ranges::view auto tokens) const -> void { if (val.empty()) { if (!value.has_value()) { - auto const values = base.consume_tokens(tokens | std::views::take(1), name_for_error); - value = base.get_transformed(values); + auto const values = impl.consume_tokens(tokens | std::views::take(1), name_for_error); + value = impl.get_transformed(values); } else { - auto const v = base.consume_token(tokens.front(), name_for_error); - base.append_value(v, value); + auto const v = impl.consume_token(tokens.front(), name_for_error); + impl.append_value(v, value); } } else { if (!value.has_value()) { - auto const values = base.consume_tokens(std::views::single(Token{val}), name_for_error); - value = base.get_transformed(values); + auto const values = impl.consume_tokens(std::views::single(Token{val}), name_for_error); + value = impl.get_transformed(values); } else { - auto const v = base.process_token(val, name_for_error); - base.append_value(v, value); + auto const v = impl.process_token(val, name_for_error); + impl.append_value(v, value); } } } - auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto tokens) const -> void + auto check_errors(ArgumentImpl const & impl, std::string_view value, std::ranges::view auto tokens) const -> void { if (value.empty() && tokens.empty()) { - throw parsing_error(std::format("argument {}: expected one argument", base.get_joined_names())); + throw parsing_error(std::format("argument {}: expected one argument", impl.get_joined_names())); } } - auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentImpl const & impl, std::any & value) const -> void { - value = base.get_default(); + value = impl.get_default(); } }; @@ -1430,7 +1435,7 @@ namespace argparse private: auto perform_action(std::string const & value, std::ranges::view auto tokens) -> void { - std::visit([&](auto const & action) { action.perform(*this, m_value, get_name_for_error(), value, tokens); }, m_action); + std::visit([&](auto const & action) { action.perform(get_impl(), m_value, get_name_for_error(), value, tokens); }, m_action); } auto has_arg(auto it) const -> std::string_view @@ -1525,12 +1530,12 @@ namespace argparse auto check_errors(std::string_view value, std::ranges::view auto tokens) const -> void { - std::visit([&](auto const & action) { action.check_errors(*this, value, tokens); }, m_action); + std::visit([&](auto const & action) { action.check_errors(get_impl(), value, tokens); }, m_action); } auto assign_non_present_value() -> void { - std::visit([&](auto const & action) { action.assign_non_present_value(*this, m_value); }, m_action); + std::visit([&](auto const & action) { action.assign_non_present_value(get_impl(), m_value); }, m_action); } static auto get_consumable(Tokens & tokens)