Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MJKirk opened this issue Nov 26, 2024 · 4 comments
Open
Labels
docs Documentation in the Doc dir

Comments

@MJKirk
Copy link

MJKirk commented Nov 26, 2024

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".

@MJKirk MJKirk added the docs Documentation in the Doc dir label Nov 26, 2024
MJKirk added a commit to MJKirk/eos that referenced this issue Nov 26, 2024
The environment variable can't be used as a default directly (see my issue python/cpython#127296) thus the later checks
MJKirk added a commit to eos/eos that referenced this issue Nov 26, 2024
The environment variable can't be used as a default directly (see my issue python/cpython#127296) thus the later checks
@ericvsmith
Copy link
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".

@terryjreedy
Copy link
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.

@MJKirk
Copy link
Author

MJKirk commented Nov 27, 2024

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 👍

@rhettinger
Copy link
Contributor

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

+1 for this edit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: No status
Status: Todo
Development

No branches or pull requests

4 participants