rename extension from .rst to .md
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by denuoweb · committed by denuoweb · parent ea70da4 · Browse files · 2 files·+126 added−126 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+126 added−126 removed
- other:+126 added−126 removed
Provenance
- Commit
fb3a0cd- Tree
6084223- Author
denuoweb <jaron@rosenau.info>- Committer
denuoweb <jaron@rosenau.info>- Parent
ea70da4
2 files changed+126 added−126 removed
added:docs/exceptions.md+126 added
1+.. _exception-handling-exit-codes:2+3+Exception Handling and Exit Codes4+==================================5+6+.. currentmodule:: click7+8+Click internally uses exceptions to signal various error conditions that9+the user of the application might have caused. Primarily this is things10+like incorrect usage.11+12+.. contents::13+ :depth: 114+ :local:15+16+Where are Errors Handled?17+-------------------------18+19+Click's main error handling is happening in :meth:`Command.main`. In20+there it handles all subclasses of :exc:`ClickException` as well as the21+standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. The22+latter are internally translated into an :exc:`Abort`.23+24+The logic applied is the following:25+26+1. If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it27+ as :exc:`Abort`.28+2. If a :exc:`ClickException` is raised, invoke the29+ :meth:`ClickException.show` method on it to display it and then exit30+ the program with :attr:`ClickException.exit_code`.31+3. If an :exc:`Abort` exception is raised print the string ``Aborted!``32+ to standard error and exit the program with exit code ``1``.33+4. If it goes through well, exit the program with exit code ``0``.34+35+What if I don't want that?36+--------------------------37+38+Generally you always have the option to invoke the :meth:`invoke` method39+yourself. For instance if you have a :class:`Command` you can invoke it40+manually like this::41+42+ ctx = command.make_context('command-name', ['args', 'go', 'here'])43+ with ctx:44+ result = command.invoke(ctx)45+46+In this case exceptions will not be handled at all and bubbled up as you47+would expect.48+49+Starting with Click 3.0 you can also use the :meth:`Command.main` method50+but disable the standalone mode which will do two things: disable51+exception handling and disable the implicit :meth:`sys.exit` at the end.52+53+So you can do something like this::54+55+ command.main(['command-name', 'args', 'go', 'here'],56+ standalone_mode=False)57+58+Which Exceptions Exist?59+-----------------------60+61+Click has two exception bases: :exc:`ClickException` which is raised for62+all exceptions that Click wants to signal to the user and :exc:`Abort`63+which is used to instruct Click to abort the execution.64+65+A :exc:`ClickException` has a :meth:`~ClickException.show` method which66+can render an error message to stderr or the given file object. If you67+want to use the exception yourself for doing something check the API docs68+about what else they provide.69+70+The following common subclasses exist:71+72+* :exc:`UsageError` to inform the user that something went wrong.73+* :exc:`BadParameter` to inform the user that something went wrong with74+ a specific parameter. These are often handled internally in Click and75+ augmented with extra information if possible. For instance if those76+ are raised from a callback Click will automatically augment it with77+ the parameter name if possible.78+* :exc:`FileError` this is an error that is raised by the79+ :exc:`FileType` if Click encounters issues opening the file.80+81+82+.. _help-page-exit-codes:83+84+Help Pages and Exit Codes85+--------------------------86+87+Triggering the a help page intentionally (by passing in ``--help``) returns exit code 0. If a help page is displayed due to incorrect user input, the program returns exit code 2. See :ref:`exit-codes` for more general information.88+89+For clarity, here is an example.90+91+.. click:example::92+93+ @click.group('printer_group')94+ def printer_group():95+ pass96+97+ @printer_group.command('printer')98+ @click.option('--this')99+ def printer(this):100+ if this:101+ click.echo(this)102+103+.. click:run::104+ invoke(printer_group, args=['--help'])105+106+The above invocation returns exit code 0.107+108+.. click:run::109+ invoke(printer_group, args=[])110+111+The above invocation returns exit code 2 since the user invoked the command incorrectly. However, since this is such a common error when first using a command, Click invokes the help page for the user. To see that `printer-group` is an invalid invocation, turn `no_args_is_help` off.112+113+.. click:example::114+115+ @click.group('printer_group', no_args_is_help=False)116+ def printer_group():117+ pass118+119+ @printer_group.command('printer')120+ @click.option('--this')121+ def printer(this):122+ if this:123+ click.echo(this)124+125+.. click:run::126+ invoke(printer_group, args=[])deleted:docs/exceptions.rst-126 removed
1-.. _exception-handling-exit-codes:2-3-Exception Handling and Exit Codes4-==================================5-6-.. currentmodule:: click7-8-Click internally uses exceptions to signal various error conditions that9-the user of the application might have caused. Primarily this is things10-like incorrect usage.11-12-.. contents::13- :depth: 114- :local:15-16-Where are Errors Handled?17--------------------------18-19-Click's main error handling is happening in :meth:`Command.main`. In20-there it handles all subclasses of :exc:`ClickException` as well as the21-standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. The22-latter are internally translated into an :exc:`Abort`.23-24-The logic applied is the following:25-26-1. If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it27- as :exc:`Abort`.28-2. If a :exc:`ClickException` is raised, invoke the29- :meth:`ClickException.show` method on it to display it and then exit30- the program with :attr:`ClickException.exit_code`.31-3. If an :exc:`Abort` exception is raised print the string ``Aborted!``32- to standard error and exit the program with exit code ``1``.33-4. If it goes through well, exit the program with exit code ``0``.34-35-What if I don't want that?36---------------------------37-38-Generally you always have the option to invoke the :meth:`invoke` method39-yourself. For instance if you have a :class:`Command` you can invoke it40-manually like this::41-42- ctx = command.make_context('command-name', ['args', 'go', 'here'])43- with ctx:44- result = command.invoke(ctx)45-46-In this case exceptions will not be handled at all and bubbled up as you47-would expect.48-49-Starting with Click 3.0 you can also use the :meth:`Command.main` method50-but disable the standalone mode which will do two things: disable51-exception handling and disable the implicit :meth:`sys.exit` at the end.52-53-So you can do something like this::54-55- command.main(['command-name', 'args', 'go', 'here'],56- standalone_mode=False)57-58-Which Exceptions Exist?59------------------------60-61-Click has two exception bases: :exc:`ClickException` which is raised for62-all exceptions that Click wants to signal to the user and :exc:`Abort`63-which is used to instruct Click to abort the execution.64-65-A :exc:`ClickException` has a :meth:`~ClickException.show` method which66-can render an error message to stderr or the given file object. If you67-want to use the exception yourself for doing something check the API docs68-about what else they provide.69-70-The following common subclasses exist:71-72-* :exc:`UsageError` to inform the user that something went wrong.73-* :exc:`BadParameter` to inform the user that something went wrong with74- a specific parameter. These are often handled internally in Click and75- augmented with extra information if possible. For instance if those76- are raised from a callback Click will automatically augment it with77- the parameter name if possible.78-* :exc:`FileError` this is an error that is raised by the79- :exc:`FileType` if Click encounters issues opening the file.80-81-82-.. _help-page-exit-codes:83-84-Help Pages and Exit Codes85---------------------------86-87-Triggering the a help page intentionally (by passing in ``--help``) returns exit code 0. If a help page is displayed due to incorrect user input, the program returns exit code 2. See :ref:`exit-codes` for more general information.88-89-For clarity, here is an example.90-91-.. click:example::92-93- @click.group('printer_group')94- def printer_group():95- pass96-97- @printer_group.command('printer')98- @click.option('--this')99- def printer(this):100- if this:101- click.echo(this)102-103-.. click:run::104- invoke(printer_group, args=['--help'])105-106-The above invocation returns exit code 0.107-108-.. click:run::109- invoke(printer_group, args=[])110-111-The above invocation returns exit code 2 since the user invoked the command incorrectly. However, since this is such a common error when first using a command, Click invokes the help page for the user. To see that `printer-group` is an invalid invocation, turn `no_args_is_help` off.112-113-.. click:example::114-115- @click.group('printer_group', no_args_is_help=False)116- def printer_group():117- pass118-119- @printer_group.command('printer')120- @click.option('--this')121- def printer(this):122- if this:123- click.echo(this)124-125-.. click:run::126- invoke(printer_group, args=[])