reset optionals/positionals on new parse

master
Daniel Kolesa 2017-05-26 21:27:35 +02:00
parent ad5429369b
commit 661b7b94cc
1 changed files with 22 additions and 3 deletions

View File

@ -379,6 +379,11 @@ protected:
} }
} }
/** @brief Resets the usage counter. */
void reset() {
p_used = 0;
}
private: private:
void validate_req(arg_value req) const noexcept { void validate_req(arg_value req) const noexcept {
switch (req) { switch (req) {
@ -463,6 +468,11 @@ struct arg_positional: arg_argument {
return p_used; return p_used;
} }
/** @brief Resets the usage flag. */
void reset() {
p_used = false;
}
protected: protected:
arg_positional() = delete; arg_positional() = delete;
@ -1039,15 +1049,23 @@ public:
std::size_t npos = 0; std::size_t npos = 0;
bool has_rest = false; bool has_rest = false;
for_each([&has_rest, &npos](auto const &arg) { for_each([&has_rest, &npos](auto const &arg) {
if (arg.type() == arg_type::OPTIONAL) {
const_cast<arg_optional &>(
static_cast<arg_optional const &>(arg)
).reset();
}
if (arg.type() != arg_type::POSITIONAL) { if (arg.type() != arg_type::POSITIONAL) {
return true; return true;
} }
auto const &desc = static_cast<arg_positional const &>(arg); auto const &desc = static_cast<arg_positional const &>(arg);
const_cast<arg_positional &>(desc).reset();
if (desc.needs_value() == arg_value::REST) { if (desc.needs_value() == arg_value::REST) {
has_rest = true; has_rest = true;
return false; return true;
}
if (!has_rest) {
++npos;
} }
++npos;
return true; return true;
}, true, true); }, true, true);
bool allow_optional = true; bool allow_optional = true;
@ -1510,7 +1528,8 @@ struct default_help_formatter {
allopt.clear(); allopt.clear();
allpos.clear(); allpos.clear();
p_parser.for_each([&write_help, &out, &allopt, &allpos p_parser.for_each([
&write_help, &out, &allopt, &allpos
](auto const &arg) { ](auto const &arg) {
if (arg.type() != arg_type::GROUP) { if (arg.type() != arg_type::GROUP) {
return true; return true;