Skip to content

[BUG] Flag-like parameters do not store implicit values for nargs(0, 1) #404

@Jacobfaib

Description

@Jacobfaib
bool value = false;

parser.add_argument("--foo").store_into(value).nargs(0, 1);
parser.parse_args({"prog", "--foo"});

assert(value); // fails

This pattern is useful if you want to support "rich" boolean flags:

--flag   // flag is true
--flag 0 // flag is false
--flag 1 // flag is true
// ..etc. 

It seems like the bug is in Argument::consume():

if (num_args_max == 0) {
  if (!dry_run) {
    m_values.emplace_back(m_implicit_value);
    ...
    return start;
  }
}

Here num_args_max == 1, but the else branch does not do m_values.emplace_back(m_implicit_value) when it finds there are no more arguments. The same bug also happens when

parser.parse_args({"prog", "--foo", "--some-other-arg", "value"});

foo is not filled with the implicit value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions