switch format from rest to myst
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by denuoweb · committed by denuoweb · parent fb3a0cd · Browse files · 1 file·+63 added−53 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+63 added−53 removed
- other:+63 added−53 removed
Provenance
- Commit
d9b37c7- Tree
d3ced97- Author
denuoweb <jaron@rosenau.info>- Committer
denuoweb <jaron@rosenau.info>- Parent
fb3a0cd
1 file changed+63 added−53 removed
modified:docs/exceptions.md+63 added-53 removed
11234345678991011121213141314151617161718191920212220212223242526262728293031323327282930313233343535363637383940383940414242434443444546474849504951525153545355565556575859606162635859646561626667686965667071727374757672737475767778797778798081828384858687828889848590919293948788959697989910010111 unmodified lines1131141151161171181191201211221231241256 unmodified lines132133134135136.. _exception-handling-exit-codes:(exception-handling-exit-codes)=
Exception Handling and Exit Codes==================================# Exception Handling and Exit Codes
```{eval-rst}.. currentmodule:: click```
Click internally uses exceptions to signal various error conditions thatthe user of the application might have caused. Primarily this is thingsthe user of the application might have caused. Primarily this is thingslike incorrect usage.
.. contents:: :depth: 1 :local:```{contents}:depth: 1:local:```
Where are Errors Handled?-------------------------## Where are Errors Handled?
Click's main error handling is happening in :meth:`Command.main`. Inthere it handles all subclasses of :exc:`ClickException` as well as thestandard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. Thelatter are internally translated into an :exc:`Abort`.Click's main error handling is happening in {meth}`Command.main`. Inthere it handles all subclasses of {exc}`ClickException` as well as thestandard {exc}`EOFError` and {exc}`KeyboardInterrupt` exceptions. Thelatter are internally translated into an {exc}`Abort`.
The logic applied is the following:
1. If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it as :exc:`Abort`.2. If a :exc:`ClickException` is raised, invoke the :meth:`ClickException.show` method on it to display it and then exit the program with :attr:`ClickException.exit_code`.3. If an :exc:`Abort` exception is raised print the string ``Aborted!`` to standard error and exit the program with exit code ``1``.4. If it goes through well, exit the program with exit code ``0``.1. If an {exc}`EOFError` or {exc}`KeyboardInterrupt` happens, reraise it as {exc}`Abort`.2. If a {exc}`ClickException` is raised, invoke the {meth}`ClickException.show` method on it to display it and then exit the program with {attr}`ClickException.exit_code`.3. If an {exc}`Abort` exception is raised print the string ``Aborted!`` to standard error and exit the program with exit code ``1``.4. If it goes through well, exit the program with exit code ``0``.
What if I don't want that?--------------------------## What if I Don't Want That?
Generally you always have the option to invoke the :meth:`invoke` methodyourself. For instance if you have a :class:`Command` you can invoke itmanually like this::Generally you always have the option to invoke the {meth}`Command.invoke`method yourself. For instance if you have a {class}`Command` you caninvoke it manually like this:
```python ctx = command.make_context('command-name', ['args', 'go', 'here']) with ctx: result = command.invoke(ctx)ctx = command.make_context("command-name", ["args", "go", "here"])with ctx: result = command.invoke(ctx)```
In this case exceptions will not be handled at all and bubbled up as youwould expect.
Starting with Click 3.0 you can also use the :meth:`Command.main` methodStarting with Click 3.0 you can also use the {meth}`Command.main` methodbut disable the standalone mode which will do two things: disableexception handling and disable the implicit :meth:`sys.exit` at the end.exception handling and disable the implicit {func}`sys.exit` at the end.
So you can do something like this::So you can do something like this:
command.main(['command-name', 'args', 'go', 'here'], standalone_mode=False)```pythoncommand.main( ["command-name", "args", "go", "here"], standalone_mode=False,)```
Which Exceptions Exist?-----------------------## Which Exceptions Exist?
Click has two exception bases: :exc:`ClickException` which is raised forall exceptions that Click wants to signal to the user and :exc:`Abort`Click has two exception bases: {exc}`ClickException` which is raised forall exceptions that Click wants to signal to the user and {exc}`Abort`which is used to instruct Click to abort the execution.
A :exc:`ClickException` has a :meth:`~ClickException.show` method whichcan render an error message to stderr or the given file object. If youA {exc}`ClickException` has a {meth}`ClickException.show` method whichcan render an error message to stderr or the given file object. If youwant to use the exception yourself for doing something check the API docsabout what else they provide.
The following common subclasses exist:
* :exc:`UsageError` to inform the user that something went wrong.* :exc:`BadParameter` to inform the user that something went wrong with a specific parameter. These are often handled internally in Click and augmented with extra information if possible. For instance if those are raised from a callback Click will automatically augment it with the parameter name if possible.* :exc:`FileError` this is an error that is raised by the :exc:`FileType` if Click encounters issues opening the file.- {exc}`UsageError` to inform the user that something went wrong.- {exc}`BadParameter` to inform the user that something went wrong with a specific parameter. These are often handled internally in Click and augmented with extra information if possible. For instance if those are raised from a callback Click will automatically augment it with the parameter name if possible.- {exc}`FileError` this is an error that is raised by the {class}`FileType` if Click encounters issues opening the file.
(help-page-exit-codes)=
.. _help-page-exit-codes:## Help Pages and Exit Codes
Help Pages and Exit Codes--------------------------Triggering the a help page intentionally (by passing in ``--help``)returns exit code 0. If a help page is displayed due to incorrect userinput, the program returns exit code 2. See {ref}`exit-codes` for moregeneral information.
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.
For clarity, here is an example.
```{eval-rst}.. click:example::
@click.group('printer_group') def printer_group():11 unmodified linesThe above invocation returns exit code 0.
.. click:run:: invoke(printer_group, args=[])```
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.
```{eval-rst}.. click:example::
@click.group('printer_group', no_args_is_help=False) def printer_group():6 unmodified lines click.echo(this)
.. click:run:: invoke(printer_group, args=[])```No newline at end of file