-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Argparse: improve parse_known_args() doc #106235
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
Comments
The behavior is not a bug as it is documented. The parse_known_args entry has an example something like yours with interleaved known and unknowns and all are processed and assigned to the namespace or unknown tuple. I presume that this example intentionally makes this point.
Perhaps you are implicitly making a feature request, but I do not know argparse well enough to know if what you want is already possible or not. |
Ooh, I misread the example, which indeed makes this behavior clear. Thank you for the explanation! The text of the documentation only talks about “remaining arguments”, making it seem that it means “the rest after some point” and not “any unknown ones anywhere”. Argparse actually supports what I want it to do, using In that case, I guess I'm making a documentation improvement request. :-) My suggested change would be amendment of the last sentence as follows: “Instead, it skips over any unknown arguments in the command line, only parsing the known ones, and returns a two item tuple containing the populated namespace and the list of skipped argument strings.” Perhaps I would also add another paragraph as follows: And maybe also the following: |
Can you create a pull request? |
Yes, I'll get to it in a few days. |
Documentation for Note that In any case, parsing tries to handle all strings, putting ones it does not recognize in the namespace under the "UNRECOGNIZED" dest. It does not have a 'short-circuit' option, which would stop parsing the rest. One hallmark of argparse parsing is it tries to handle arguments, at least the optionals, in what ever order the end user provides. Besides |
… "remaining" (pythonGH-126921) (cherry picked from commit a425141) Co-authored-by: Savannah Bailey <savannahostrowski@gmail.com>
… "remaining" (pythonGH-126921) (cherry picked from commit a425141) Co-authored-by: Savannah Bailey <savannahostrowski@gmail.com>
Uh oh!
There was an error while loading. Please reload this page.
Bug report
When the
ArgumentParser.parse_known_args()
method is used to parse a parameter withaction='append'
set, the parameter will be parsed in the whole argument list, including arguments in the unknown section.Example code showing the issue
Expected output
Parsing stops at the first unknown arg (
STOP
) and all following arguments are left untouched, as they might have to be parsed by a different parser elsewhere.Actual output
All instances of
--foo
are parsed, including the ones following the first unknown arg.Your environment
Linked PRs
parse_known_args
documentation by removing "remaining" #126921parse_known_args
documentation by removing "remaining" (GH-126921) #134913parse_known_args
documentation by removing "remaining" (GH-126921) #134914The text was updated successfully, but these errors were encountered: