From ce383e08985954a877cea72028143dad00c41529 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Tue, 11 Nov 2025 08:49:45 +0100 Subject: [PATCH 1/7] Refactor: change function signatures --- include/argparse.hpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index dd89f1a..6b7a52c 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1044,9 +1044,9 @@ namespace argparse } } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void { - m_value = m_base.get_default(); + value = base.get_default(); } private: @@ -1124,9 +1124,9 @@ namespace argparse } } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void { - m_value = m_base.get_default(); + value = base.get_default(); } private: @@ -1156,9 +1156,9 @@ namespace argparse } } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void { - m_value = false; + value = false; } private: @@ -1188,9 +1188,9 @@ namespace argparse } } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void { - m_value = true; + value = true; } private: @@ -1216,9 +1216,9 @@ namespace argparse { } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void { - m_value = false; + value = false; } private: @@ -1243,9 +1243,9 @@ namespace argparse { } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void { - m_value = false; + value = false; } private: @@ -1281,9 +1281,9 @@ namespace argparse } } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void { - m_value = m_base.get_default(); + value = base.get_default(); } private: @@ -1338,9 +1338,9 @@ namespace argparse } } - auto assign_non_present_value() const -> void + auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void { - m_value = m_base.get_default(); + value = base.get_default(); } private: @@ -1602,7 +1602,7 @@ namespace argparse auto assign_non_present_value() -> void { - std::visit([&](auto const & action) { action.assign_non_present_value(); }, m_action); + std::visit([&](auto const & action) { action.assign_non_present_value(*this, m_value); }, m_action); } static auto get_consumable(Tokens & tokens) From bab18ffb101f226c62d78196bbac42d76c9f464a Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Tue, 11 Nov 2025 08:56:36 +0100 Subject: [PATCH 2/7] Refactor: change function signatures --- include/argparse.hpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 6b7a52c..98b2418 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1036,11 +1036,11 @@ namespace argparse } } - auto check_errors(std::string_view value, std::ranges::view auto tokens) const -> void + auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto tokens) const -> void { - if (!m_base.has_nargs() && value.empty() && tokens.empty()) + if (!base.has_nargs() && value.empty() && tokens.empty()) { - throw parsing_error(std::format("argument {}: expected one argument", m_base.get_joined_names())); + throw parsing_error(std::format("argument {}: expected one argument", base.get_joined_names())); } } @@ -1116,11 +1116,11 @@ namespace argparse m_value = m_base.get_const(); } - auto check_errors(std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", m_base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); } } @@ -1148,11 +1148,11 @@ namespace argparse m_value = true; } - auto check_errors(std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", m_base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); } } @@ -1180,11 +1180,11 @@ namespace argparse m_value = false; } - auto check_errors(std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", m_base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); } } @@ -1212,7 +1212,7 @@ namespace argparse throw HelpRequested(); } - auto check_errors(std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentBase const & /* base */, std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void { } @@ -1239,7 +1239,7 @@ namespace argparse throw VersionRequested(); } - auto check_errors(std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentBase const & /* base */, std::string_view /* value */, std::ranges::view auto /* tokens */) const -> void { } @@ -1273,11 +1273,11 @@ namespace argparse } } - auto check_errors(std::string_view value, std::ranges::view auto /* tokens */) const -> void + auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void { if (!value.empty()) { - throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", m_base.get_joined_names(), value)); + throw parsing_error(std::format("argument {}: ignored explicit argument '{}'", base.get_joined_names(), value)); } } @@ -1330,11 +1330,11 @@ namespace argparse } } - auto check_errors(std::string_view value, std::ranges::view auto tokens) const -> void + auto check_errors(ArgumentBase const & base, 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", m_base.get_joined_names())); + throw parsing_error(std::format("argument {}: expected one argument", base.get_joined_names())); } } @@ -1597,7 +1597,7 @@ namespace argparse auto check_errors(std::string_view value, std::ranges::view auto tokens) const -> void { - std::visit([&](auto const & action) { action.check_errors(value, tokens); }, m_action); + std::visit([&](auto const & action) { action.check_errors(*this, value, tokens); }, m_action); } auto assign_non_present_value() -> void From 2d70a1a66e0822548739e9b113479d1c2b0225c1 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Tue, 11 Nov 2025 08:59:56 +0100 Subject: [PATCH 3/7] Cleanup: add const qualifier --- include/argparse.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 98b2418..fb10a42 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1044,7 +1044,7 @@ namespace argparse } } - auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void { value = base.get_default(); } @@ -1124,7 +1124,7 @@ namespace argparse } } - auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void { value = base.get_default(); } @@ -1156,7 +1156,7 @@ namespace argparse } } - auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void { value = false; } @@ -1188,7 +1188,7 @@ namespace argparse } } - auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void { value = true; } @@ -1216,7 +1216,7 @@ namespace argparse { } - auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void { value = false; } @@ -1243,7 +1243,7 @@ namespace argparse { } - auto assign_non_present_value(ArgumentBase & /* base */, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & /* base */, std::any & value) const -> void { value = false; } @@ -1281,7 +1281,7 @@ namespace argparse } } - auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void { value = base.get_default(); } @@ -1338,7 +1338,7 @@ namespace argparse } } - auto assign_non_present_value(ArgumentBase & base, std::any & value) const -> void + auto assign_non_present_value(ArgumentBase const & base, std::any & value) const -> void { value = base.get_default(); } From 244115f9b6bce4ab5681918fd0fb9092352ac82b Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Tue, 11 Nov 2025 10:46:24 +0100 Subject: [PATCH 4/7] Cleanup: add const qualifiers --- include/argparse.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index fb10a42..82e887d 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -761,7 +761,7 @@ namespace argparse return join(m_options.choices | std::views::transform([&](auto const & choice) { return m_options.type_handler->to_string(choice); }), separator); } - auto parse_arguments(std::ranges::view auto tokens) -> std::any + auto parse_arguments(std::ranges::view auto tokens) const -> std::any { auto const values = consume_tokens(tokens); return m_options.type_handler->transform(values); @@ -946,7 +946,7 @@ namespace argparse return m_impl.get_action(); } - auto parse_arguments(std::ranges::view auto tokens) -> std::any + auto parse_arguments(std::ranges::view auto tokens) const -> std::any { return m_impl.parse_arguments(tokens); } From 9fa88cc1ef598f73e940aafd362a136e3cd33ae2 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Tue, 11 Nov 2025 10:47:24 +0100 Subject: [PATCH 5/7] Refactor: change function signatures --- include/argparse.hpp | 92 ++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 82e887d..4ef54af 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1010,28 +1010,28 @@ namespace argparse { } - auto perform(std::string const & value, std::ranges::view auto tokens) const -> void + auto perform(ArgumentBase const & base, std::any & value, std::string const & val, std::ranges::view auto tokens) const -> void { - if (m_base.has_nargs()) + if (base.has_nargs()) { - if (m_base.has_nargs_number()) + if (base.has_nargs_number()) { - parse_arguments_number(tokens); + parse_arguments_number(base, value, tokens); } else { - parse_arguments_option(tokens); + parse_arguments_option(base, value, tokens); } } else { - if (value.empty()) + if (val.empty()) { - m_value = m_base.consume_token(tokens.front()); + value = base.consume_token(tokens.front()); } else { - m_value = m_base.process_token(value); + value = base.process_token(val); } } } @@ -1050,47 +1050,47 @@ namespace argparse } private: - auto parse_arguments_number(std::ranges::view auto tokens) const -> void + auto parse_arguments_number(ArgumentBase const & base, std::any & value, std::ranges::view auto tokens) const -> void { auto const nargs_number = m_base.get_nargs_number(); - auto const values = m_base.consume_tokens(tokens | std::views::take(nargs_number)); + auto const values = base.consume_tokens(tokens | std::views::take(nargs_number)); if (values.size() < nargs_number) { - throw parsing_error(std::format("argument {}: expected {} argument{}", m_base.get_joined_names(), std::to_string(nargs_number), nargs_number > 1 ? "s" : "")); + throw parsing_error(std::format("argument {}: expected {} argument{}", base.get_joined_names(), std::to_string(nargs_number), nargs_number > 1 ? "s" : "")); } - m_value = m_base.get_transformed(values); + value = base.get_transformed(values); } - auto parse_arguments_option(std::ranges::view auto tokens) const -> void + auto parse_arguments_option(ArgumentBase const & base, std::any & value, std::ranges::view auto tokens) const -> void { - switch (m_base.get_nargs_option()) + switch (base.get_nargs_option()) { case zero_or_one: { if (!tokens.empty()) { - m_value = m_base.consume_token(tokens.front()); + value = base.consume_token(tokens.front()); } else { - m_value = m_base.get_const(); + value = base.get_const(); } break; } case zero_or_more: { - m_value = m_base.parse_arguments(tokens); + value = base.parse_arguments(tokens); break; } case one_or_more: { - if (auto const values = m_base.consume_tokens(tokens); !values.empty()) + if (auto const values = base.consume_tokens(tokens); !values.empty()) { - m_value = m_base.get_transformed(values); + value = base.get_transformed(values); } else { - throw parsing_error(std::format("argument {}: expected at least one argument", m_base.get_joined_names())); + throw parsing_error(std::format("argument {}: expected at least one argument", base.get_joined_names())); } break; } @@ -1111,9 +1111,9 @@ namespace argparse { } - auto perform(std::string const & /* value */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentBase const & base, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { - m_value = m_base.get_const(); + value = base.get_const(); } auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void @@ -1143,9 +1143,9 @@ namespace argparse { } - auto perform(std::string const & /* value */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { - m_value = true; + value = true; } auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void @@ -1175,9 +1175,9 @@ namespace argparse { } - auto perform(std::string const & /* value */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { - m_value = false; + value = false; } auto check_errors(ArgumentBase const & base, std::string_view value, std::ranges::view auto /* tokens */) const -> void @@ -1206,9 +1206,9 @@ namespace argparse { } - auto perform(std::string const & /* value */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { - m_value = true; + value = true; throw HelpRequested(); } @@ -1233,9 +1233,9 @@ namespace argparse { } - auto perform(std::string const & /* value */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { - m_value = true; + value = true; throw VersionRequested(); } @@ -1261,15 +1261,15 @@ namespace argparse { } - auto perform(std::string const & /* value */, std::ranges::view auto /* tokens */) const -> void + auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { - if (!m_value.has_value()) + if (!value.has_value()) { - m_value = 1; + value = 1; } else { - ++std::any_cast(m_value); + ++std::any_cast(value); } } @@ -1300,32 +1300,32 @@ namespace argparse { } - auto perform(std::string const & value, std::ranges::view auto tokens) const -> void + auto perform(ArgumentBase const & base, std::any & value, std::string const & val, std::ranges::view auto tokens) const -> void { - if (value.empty()) + if (val.empty()) { - if (!m_value.has_value()) + if (!value.has_value()) { - auto const values = m_base.consume_tokens(tokens | std::views::take(1)); - m_value = m_base.get_transformed(values); + auto const values = base.consume_tokens(tokens | std::views::take(1)); + value = base.get_transformed(values); } else { - auto const val = m_base.consume_token(tokens.front()); - m_base.append_value(val, m_value); + auto const v = base.consume_token(tokens.front()); + base.append_value(v, value); } } else { if (!m_value.has_value()) { - auto const values = m_base.consume_tokens(std::views::single(Token{value})); - m_value = m_base.get_transformed(values); + auto const values = base.consume_tokens(std::views::single(Token{val})); + value = base.get_transformed(values); } else { - auto const val = m_base.process_token(value); - m_base.append_value(val, m_value); + auto const v = base.process_token(val); + base.append_value(v, value); } } } @@ -1502,7 +1502,7 @@ namespace argparse private: auto perform_action(std::string const & value, std::ranges::view auto tokens) -> void { - std::visit([&](auto const & action) { action.perform(value, tokens); }, m_action); + std::visit([&](auto const & action) { action.perform(*this, m_value, value, tokens); }, m_action); } auto has_arg(auto it) const -> std::string_view From b09238b59ee383c0a8a612822d4838c2e8174978 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Tue, 11 Nov 2025 10:53:23 +0100 Subject: [PATCH 6/7] Fix access --- include/argparse.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 4ef54af..35fe86d 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1052,7 +1052,7 @@ namespace argparse private: auto parse_arguments_number(ArgumentBase const & base, std::any & value, std::ranges::view auto tokens) const -> void { - auto const nargs_number = m_base.get_nargs_number(); + auto const nargs_number = base.get_nargs_number(); auto const values = base.consume_tokens(tokens | std::views::take(nargs_number)); if (values.size() < nargs_number) { @@ -1317,7 +1317,7 @@ namespace argparse } else { - if (!m_value.has_value()) + if (!value.has_value()) { auto const values = base.consume_tokens(std::views::single(Token{val})); value = base.get_transformed(values); From 8fc728d71cdff70ee90844037fc6afb0d48d9081 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Tue, 11 Nov 2025 10:53:53 +0100 Subject: [PATCH 7/7] Refactor: remove members from Action classes --- include/argparse.hpp | 94 +++++--------------------------------------- 1 file changed, 9 insertions(+), 85 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 35fe86d..2878397 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1004,12 +1004,6 @@ namespace argparse class StoreAction { public: - StoreAction(ArgumentBase & base, std::any & value) - : m_base(base) - , m_value(value) - { - } - auto perform(ArgumentBase const & base, std::any & value, std::string const & val, std::ranges::view auto tokens) const -> void { if (base.has_nargs()) @@ -1096,21 +1090,11 @@ namespace argparse } } } - - private: - ArgumentBase & m_base; - std::any & m_value; }; class StoreConstAction { public: - StoreConstAction(ArgumentBase & base, std::any & value) - : m_base(base) - , m_value(value) - { - } - auto perform(ArgumentBase const & base, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = base.get_const(); @@ -1128,21 +1112,11 @@ namespace argparse { value = base.get_default(); } - - private: - ArgumentBase & m_base; - std::any & m_value; }; class StoreTrueAction { public: - StoreTrueAction(ArgumentBase & base, std::any & value) - : m_base(base) - , m_value(value) - { - } - auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = true; @@ -1160,21 +1134,11 @@ namespace argparse { value = false; } - - private: - ArgumentBase & m_base; - std::any & m_value; }; class StoreFalseAction { public: - StoreFalseAction(ArgumentBase & base, std::any & value) - : m_base(base) - , m_value(value) - { - } - auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = false; @@ -1192,20 +1156,11 @@ namespace argparse { value = true; } - - private: - ArgumentBase & m_base; - std::any & m_value; }; class HelpAction { public: - explicit HelpAction(std::any & value) - : m_value(value) - { - } - auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = true; @@ -1220,19 +1175,11 @@ namespace argparse { value = false; } - - private: - std::any & m_value; }; class VersionAction { public: - explicit VersionAction(std::any & value) - : m_value(value) - { - } - auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { value = true; @@ -1247,20 +1194,11 @@ namespace argparse { value = false; } - - private: - std::any & m_value; }; class CountAction { public: - CountAction(ArgumentBase & base, std::any & value) - : m_base(base) - , m_value(value) - { - } - auto perform(ArgumentBase const & /* base */, std::any & value, std::string const & /* val */, std::ranges::view auto /* tokens */) const -> void { if (!value.has_value()) @@ -1285,21 +1223,11 @@ namespace argparse { value = base.get_default(); } - - private: - ArgumentBase & m_base; - std::any & m_value; }; class AppendAction { public: - AppendAction(ArgumentBase & base, std::any & value) - : m_base(base) - , m_value(value) - { - } - auto perform(ArgumentBase const & base, std::any & value, std::string const & val, std::ranges::view auto tokens) const -> void { if (val.empty()) @@ -1342,10 +1270,6 @@ namespace argparse { value = base.get_default(); } - - private: - ArgumentBase & m_base; - std::any & m_value; }; class PositionalArgument final : public ArgumentBase @@ -1634,23 +1558,23 @@ namespace argparse switch (get_action()) { case store: - return StoreAction(*this, m_value); + return StoreAction(); case store_true: - return StoreTrueAction(*this, m_value); + return StoreTrueAction(); case store_false: - return StoreFalseAction(*this, m_value); + return StoreFalseAction(); case store_const: - return StoreConstAction(*this, m_value); + return StoreConstAction(); case help: - return HelpAction(m_value); + return HelpAction(); case version: - return VersionAction(m_value); + return VersionAction(); case count: - return CountAction(*this, m_value); + return CountAction(); case append: - return AppendAction(*this, m_value); + return AppendAction(); default: - return StoreAction(*this, m_value); + return StoreAction(); } }