Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • 5
    Actually you can already do that. simply catch SystemExit and replace sys.stderr with some other object. Commented Feb 6, 2013 at 12:01
  • 1
    Depending on what you want to do, it may be better to modify the argument itself. Is the argument to --foo optional? Can you define a custom action to handle it? These may be cleaner actions than overriding argparse's decision to exit. Commented Feb 6, 2013 at 13:01
  • Python 3.9 added a exit_on_error parameter to ArgumentParser(), but it does not (yet?) throw an error for a missing required option nor an unrecognized option. See bugs.python.org/issue41255. Commented Oct 26, 2021 at 23:50