Skip to content

bpo-37908: Add an example of ArgumentParser.exit() #15455

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

Merged
merged 11 commits into from
Sep 12, 2019
11 changes: 10 additions & 1 deletion Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,16 @@ Exiting methods
.. method:: ArgumentParser.exit(status=0, message=None)

This method terminates the program, exiting with the specified *status*
and, if given, it prints a *message* before that.
and, if given, it prints a *message* before that. The user can override
this method to handle these steps differently::

import sys

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two lines of space?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one line,

import sys

class ErrorCatchingArgumentParser(argparse.ArgumentParser):
    pass

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check some other examples in rst, single line is fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet the rst indentation is wrong (single space)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JulienPalard And i think it is confusing some users too :(.
I found some code examples in rst using >>> and the others don't using >>> like this example. it is confusing too(i don't have time to look the details.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocks with '>>>' are here to demostrate an interactive session (when the responses are important), like:

>>> 1.2 - 1.0
0.19999999999999996

The ones with no '>>>' are to demonstrate code (implementation of classes, functions, ...) when there is typically no result, like:

def foo(x, y):
    return 5 * x - 3 * y

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks, Julien Palard. First time to know the difference.

class ErrorCatchingArgumentParser(argparse.ArgumentParser):
def exit(self, status=0, message=None)
if status:
print(f'Exiting because of an error: {message}')
sys.exit(status)

.. method:: ArgumentParser.error(message)

Expand Down