Repository navigation
argparse.ArgumentParser.parses_args does not honor exit_on_error=False when given unrecognized arguments #121018
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 26, 2024 - added a commit that references this issue
on Jun 26, 2024 - added 3 commits that reference this issue
on Jun 26, 2024 Actually, this is more complex issue.
error()is called in many other places, and it does not honorexit_on_error=False.In most cases it can be replaced with
raise ArgumentError. It is caught at higher level anderror()is called ifexit_on_erroris true.In some cases ValueError may be more appropriate exception than ArgumentError, but this is another issue.
Reacted by blhsingActually, this is more complex issue.
error()is called in many other places, and it does not honorexit_on_error=False.In most cases it can be replaced with
raise ArgumentError. It is caught at higher level anderror()is called ifexit_on_erroris true.In some cases ValueError may be more appropriate exception than ArgumentError, but this is another issue.
Ah yes good catch. I think
ArgumentErroris meant to be raised by invalid arguments given by the end user. In cases where the error is caused by an improper setup of a parser or an option from the developer, aValueErrorwould indeed be more appropriate.On second thought, I think it may be cleaner to simply put the
if self.exit_on_error: ... else: raise ArgumentErrorlogics directly in theerrormethod, and callself.erroronly when it is caused by the end user.- added a commit that references this issue
on Jun 28, 2024 There are already open issues for this bug: #85427 and #103498. It is my fault that I did not recognize duplicates.
Ah you're right. I forgot to search for prior bug reports before filing mine. Sorry.
Yes, putting the logic directly in the error method could simplify some cases, but we want
exit_on_error=Falseto work even with user-overriddenerrormethod.Good point. Will keep the current approach then. Thanks!
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDoc issues
Bug report
Bug description:
As reported in Discourse, even though the documentation on the
exit_on_errorargument forargparse.ArgumentParsersays:The
parse_argsmethod would still exit with error info when there are unrecognized arguments:which outputs:
CPython versions tested on:
3.12, CPython main branch
Operating systems tested on:
Linux, Windows
Linked PRs