Skip to content

Argparse: "default" interacts incorrectly/non-intuitively with action="count" #127296

Closed

Description

@MJKirk

Very similar to issue #110131 - the documentation is not clear on how default and count interact.

Consider

parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='count', default=3)
parser.parse_args(['-v'])

My intuition, and how I read the documentation, is that you should end up with verbose=1, but actually you get verbose=4.
I haven't checked the source code, but I assume under the hood something similar to append/extend is happening.

My take is that it would be better for default to really act as a default, and get overwritten rather than added to by the passed arguments. But if not, the documentation should be changed to something similar to what is written for action="append".

Linked PRs

Activity

  1. ericvsmith commented on Nov 26, 2024

    @ericvsmith
    Member

    I'm not sure that's a great assumption. I think of "-v" in this case as "increment the current value by one", and here the current value initially would be the default.

    In any event, this can't be changed due to backward compatibility, although I guess we could add another option to control this behavior. Something along the lines of "initial value to be used if this option ever processed", and you'd use 0 in this case.

    Good job finding #110131! I agree it's basically the same, except for action="count".

  2. terryjreedy commented on Nov 26, 2024

    @terryjreedy
    Member

    Behavior same as sum([1], 3) == 4, with parameter names default and start being synonyms. Perhaps add "The count starts with the default when the latter is not 0" . Note that start can be negative (same for default?), so that the result is the deficit or surplus with respect to start. I don't know if this is a sensible usage for argparse.

  3. MJKirk commented on Nov 27, 2024

    @MJKirk
    Author

    I can see for backwards compatibility reasons the argument name default cannot be changed, but it seems something like initial would convey the behaviour better.

    "The count starts with the default when the latter is not 0"

    Something like this would make the documentation clearer 👍

  4. rhettinger commented on Nov 27, 2024

    @rhettinger
    Contributor

    "The count starts with the default when the latter is not 0"

    +1 for this edit.

  5. ericvsmith commented on Nov 29, 2024

    @ericvsmith
    Member

    +1 for this edit.

    Agreed. And if we do this here, we should also do it for #110131.

  6. added 2 commits that reference this issue on Sep 5, 2026
  7. added 2 commits that reference this issue on Sep 5, 2026
  8. added a commit that references this issue on Sep 7, 2026
  9. added a commit that references this issue on Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions