From 661b7b94ccf0d16aee645e2535833e01e4bc8bcf Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 26 May 2017 21:27:35 +0200 Subject: [PATCH] reset optionals/positionals on new parse --- ostd/argparse.hh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/ostd/argparse.hh b/ostd/argparse.hh index 21cba63..424c86e 100644 --- a/ostd/argparse.hh +++ b/ostd/argparse.hh @@ -379,6 +379,11 @@ protected: } } + /** @brief Resets the usage counter. */ + void reset() { + p_used = 0; + } + private: void validate_req(arg_value req) const noexcept { switch (req) { @@ -463,6 +468,11 @@ struct arg_positional: arg_argument { return p_used; } + /** @brief Resets the usage flag. */ + void reset() { + p_used = false; + } + protected: arg_positional() = delete; @@ -1039,15 +1049,23 @@ public: std::size_t npos = 0; bool has_rest = false; for_each([&has_rest, &npos](auto const &arg) { + if (arg.type() == arg_type::OPTIONAL) { + const_cast( + static_cast(arg) + ).reset(); + } if (arg.type() != arg_type::POSITIONAL) { return true; } auto const &desc = static_cast(arg); + const_cast(desc).reset(); if (desc.needs_value() == arg_value::REST) { has_rest = true; - return false; + return true; + } + if (!has_rest) { + ++npos; } - ++npos; return true; }, true, true); bool allow_optional = true; @@ -1510,7 +1528,8 @@ struct default_help_formatter { allopt.clear(); allpos.clear(); - p_parser.for_each([&write_help, &out, &allopt, &allpos + p_parser.for_each([ + &write_help, &out, &allopt, &allpos ](auto const &arg) { if (arg.type() != arg_type::GROUP) { return true;