From a2f66ffef805c03874ae1d40bbfe7c66340c70ff Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 18:24:06 +0100 Subject: [PATCH 1/9] Refactor: extract view to local variable --- include/argparse.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index f0189fb6..4f8ebb72 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -860,6 +860,7 @@ namespace argparse if (auto [found, name] = has_arg(it); found) { auto const value = consume_name(it, name); + auto consumable_args = std::ranges::subrange(std::next(it), consumable.end()); switch (m_options.action) { @@ -868,18 +869,18 @@ namespace argparse { if (has_nargs_number()) { - parse_arguments_number(std::ranges::subrange(std::next(it), consumable.end())); + parse_arguments_number(consumable_args); } else { - parse_arguments_option(std::ranges::subrange(std::next(it), consumable.end())); + parse_arguments_option(consumable_args); } } else { if (value.empty()) { - if (auto nit = std::next(it); (nit == consumable.end()) || nit->m_token.starts_with("-")) + if (auto nit = consumable_args.begin(); (nit == consumable_args.end()) || nit->m_token.starts_with("-")) { throw parsing_error(std::format("argument {}: expected one argument", join(get_names(), "/"))); } From 514dd815a23325cf76c5c58a66e35ffaabea73f4 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 18:33:18 +0100 Subject: [PATCH 2/9] Refactor: simplify code --- include/argparse.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/argparse.h b/include/argparse.h index 4f8ebb72..4423d798 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -880,7 +880,7 @@ namespace argparse { if (value.empty()) { - if (auto nit = consumable_args.begin(); (nit == consumable_args.end()) || nit->m_token.starts_with("-")) + if (auto nit = consumable_args.begin(); consumable_args.empty() || nit->m_token.starts_with("-")) { throw parsing_error(std::format("argument {}: expected one argument", join(get_names(), "/"))); } From d2f22c2807c3f515274b638d449cb8adbb05a6b0 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 18:41:21 +0100 Subject: [PATCH 3/9] Refactor: simplify code --- include/argparse.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index 4423d798..55c6713f 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -880,13 +880,13 @@ namespace argparse { if (value.empty()) { - if (auto nit = consumable_args.begin(); consumable_args.empty() || nit->m_token.starts_with("-")) + if (consumable_args.empty() || consumable_args.front().m_token.starts_with("-")) { throw parsing_error(std::format("argument {}: expected one argument", join(get_names(), "/"))); } else { - m_value = consume_arg(*nit); + m_value = consume_arg(consumable_args.front()); } } else From 3096d24a52ce5b78d9b316d0cb7bc25559268c04 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 18:59:32 +0100 Subject: [PATCH 4/9] Refactor: move args filtering into view --- include/argparse.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index 55c6713f..a275ed72 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -860,7 +860,8 @@ namespace argparse if (auto [found, name] = has_arg(it); found) { auto const value = consume_name(it, name); - auto consumable_args = std::ranges::subrange(std::next(it), consumable.end()); + auto consumable_args = std::ranges::subrange(std::next(it), consumable.end()) + | std::views::take_while([](auto const & token) { return !token.m_token.starts_with("-"); }); switch (m_options.action) { @@ -880,7 +881,7 @@ namespace argparse { if (value.empty()) { - if (consumable_args.empty() || consumable_args.front().m_token.starts_with("-")) + if (consumable_args.empty()) { throw parsing_error(std::format("argument {}: expected one argument", join(get_names(), "/"))); } @@ -1006,7 +1007,7 @@ namespace argparse { case zero_or_one: { - if (args.empty() || args.front().m_token.starts_with("-")) + if (args.empty()) { m_value = m_options.const_; } @@ -1117,7 +1118,7 @@ namespace argparse auto count_args(std::ranges::view auto args) const -> std::size_t { - return std::ranges::distance(args | std::views::take_while([](auto const & arg) { return !arg.m_token.starts_with('-'); })); + return std::ranges::distance(args); } auto consume_arg(Token & arg) const -> std::any From df21e1d24e975edb523d73462ec9b4073fc8fa8d Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 19:10:34 +0100 Subject: [PATCH 5/9] Refactor: remove no longer needed arguments counting --- include/argparse.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index a275ed72..a8955824 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -1019,8 +1019,7 @@ namespace argparse } case zero_or_more: { - auto const args_number = count_args(args); - parse_arguments(args | std::views::take(args_number)); + parse_arguments(args); break; } case one_or_more: From 492ab0c760a6468122b7315107bf8d25ff065cc7 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 19:22:40 +0100 Subject: [PATCH 6/9] Refactor: remove arguments counting --- include/argparse.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index a8955824..209b1e97 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -993,12 +993,12 @@ namespace argparse auto parse_arguments_number(std::ranges::view auto args) -> void { auto const nargs_number = get_nargs_number(); - auto const args_number = count_args(args); - if (args_number < nargs_number) + auto const values = consume_args(args | std::views::take(nargs_number)); + if (values.size() < nargs_number) { throw parsing_error(std::format("argument {}: expected {} argument{}", join(get_names(), "/"), std::to_string(nargs_number), nargs_number > 1 ? "s" : "")); } - parse_arguments(args | std::views::take(nargs_number)); + m_value = m_options.type_handler->transform(values); } auto parse_arguments_option(std::ranges::view auto args) -> void From d27c6d9f57011f3832b222de2935bd66b71592be Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 19:28:22 +0100 Subject: [PATCH 7/9] Refactor: remove arguments counting --- include/argparse.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index 209b1e97..816a8364 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -1024,12 +1024,12 @@ namespace argparse } case one_or_more: { - auto const args_number = count_args(args); - if (args_number == 0) + auto const values = consume_args(args); + if (values.empty()) { throw parsing_error(std::format("argument {}: expected at least one argument", join(get_names(), "/"))); } - parse_arguments(args | std::views::take(args_number)); + m_value = m_options.type_handler->transform(values); break; } } From 42577bd8360a4381c208c8aad6db5ae5c3390907 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 19:35:19 +0100 Subject: [PATCH 8/9] Cleanup: remove no longer used function --- include/argparse.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index 816a8364..f7630194 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -1115,11 +1115,6 @@ namespace argparse } } - auto count_args(std::ranges::view auto args) const -> std::size_t - { - return std::ranges::distance(args); - } - auto consume_arg(Token & arg) const -> std::any { arg.m_consumed = true; From bc44ae760bf1e0bb16234ba912074ba307fcf994 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 7 Mar 2025 19:37:53 +0100 Subject: [PATCH 9/9] Cleanup: add const qualifiers --- include/argparse.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index f7630194..3f95a627 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -774,7 +774,7 @@ namespace argparse private: auto parse_arguments(std::ranges::view auto args) -> void { - auto values = consume_args(args); + auto const values = consume_args(args); m_value = m_options.type_handler->transform(values); } @@ -802,7 +802,7 @@ namespace argparse } case one_or_more: { - auto values = consume_args(args); + auto const values = consume_args(args); if (!values.empty()) { m_value = m_options.type_handler->transform(values); @@ -1037,7 +1037,7 @@ namespace argparse auto parse_arguments(std::ranges::view auto args) -> void { - auto values = consume_args(args); + auto const values = consume_args(args); m_value = m_options.type_handler->transform(values); }