Skip to content
Myr

exceptions.rst

This file

Path

docs/exceptions.rst

126 lines · 4.2 KiB

Last changed by

Document help page exit codes (#3122)Rowlando13before adoption7b4e59c

History of this file

Take it

The walk stopped after 200 commits: lines older than c96545f point at it.

Document help page exit codes (#3122)Rowlando13before adoption7b4e59c

line 1why, one step back
1.. _exception-handling-exit-codes:

Merge pull request #2266 from pallets/dependabot/github_actions/actions/cache-3.0.2David Lordbefore adoptionc96545f

line 2why, one step back
2

Document help page exit codes (#3122)Rowlando13before adoption7b4e59c

lines 3–5why, one step back
3Exception Handling and Exit Codes
4==================================5

Merge pull request #2266 from pallets/dependabot/github_actions/actions/cache-3.0.2David Lordbefore adoptionc96545f

lines 6–11why, one step back
6.. currentmodule:: click
78Click internally uses exceptions to signal various error conditions that9the user of the application might have caused. Primarily this is things10like incorrect usage.11

Document help page exit codes (#3122)Rowlando13before adoption7b4e59c

lines 12–15why, one step back
12.. contents::
13 :depth: 114 :local:15

Merge pull request #2266 from pallets/dependabot/github_actions/actions/cache-3.0.2David Lordbefore adoptionc96545f

lines 16–18why, one step back
16Where are Errors Handled?
17-------------------------18

deprecate base classes `BaseCommand` and `MultiCommand` (#2591)David Lordbefore adoptionedcd2dc

line 19why, one step back
19Click's main error handling is happening in :meth:`Command.main`. In

Merge pull request #2266 from pallets/dependabot/github_actions/actions/cache-3.0.2David Lordbefore adoptionc96545f

lines 20–21why, one step back
20there it handles all subclasses of :exc:`ClickException` as well as the
21standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. The

Merge branch '8.1.x'David Lordbefore adoption5e75fe7

line 22why, one step back
22latter are internally translated into an :exc:`Abort`.

Merge pull request #2266 from pallets/dependabot/github_actions/actions/cache-3.0.2David Lordbefore adoptionc96545f

lines 23–27why, one step back
23
24The logic applied is the following:25261. If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it27 as :exc:`Abort`.

Merge branch '8.1.x'David Lordbefore adoption5e75fe7

line 28why, one step back
282. If a :exc:`ClickException` is raised, invoke the

Merge pull request #2266 from pallets/dependabot/github_actions/actions/cache-3.0.2David Lordbefore adoptionc96545f

lines 29–32why, one step back
29 :meth:`ClickException.show` method on it to display it and then exit
30 the program with :attr:`ClickException.exit_code`.313. If an :exc:`Abort` exception is raised print the string ``Aborted!``32 to standard error and exit the program with exit code ``1``.

Merge branch '8.1.x'David Lordbefore adoption5e75fe7

line 33why, one step back
334. If it goes through well, exit the program with exit code ``0``.

Merge pull request #2266 from pallets/dependabot/github_actions/actions/cache-3.0.2David Lordbefore adoptionc96545f

lines 34–79why, one step back
34
35What if I don't want that?36--------------------------3738Generally you always have the option to invoke the :meth:`invoke` method39yourself. For instance if you have a :class:`Command` you can invoke it40manually like this::4142 ctx = command.make_context('command-name', ['args', 'go', 'here'])43 with ctx:44 result = command.invoke(ctx)4546In this case exceptions will not be handled at all and bubbled up as you47would expect.4849Starting with Click 3.0 you can also use the :meth:`Command.main` method50but disable the standalone mode which will do two things: disable51exception handling and disable the implicit :meth:`sys.exit` at the end.5253So you can do something like this::5455 command.main(['command-name', 'args', 'go', 'here'],56 standalone_mode=False)5758Which Exceptions Exist?59-----------------------6061Click has two exception bases: :exc:`ClickException` which is raised for62all exceptions that Click wants to signal to the user and :exc:`Abort`63which is used to instruct Click to abort the execution.6465A :exc:`ClickException` has a :meth:`~ClickException.show` method which66can render an error message to stderr or the given file object. If you67want to use the exception yourself for doing something check the API docs68about what else they provide.6970The following common subclasses exist:7172* :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.

Document help page exit codes (#3122)Rowlando13before adoption7b4e59c

lines 80–126why, one step back
80
8182.. _help-page-exit-codes:8384Help Pages and Exit Codes85--------------------------8687Triggering 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.8889For clarity, here is an example.9091.. click:example::9293 @click.group('printer_group')94 def printer_group():95 pass9697 @printer_group.command('printer')98 @click.option('--this')99 def printer(this):100 if this:101 click.echo(this)102103.. click:run::104 invoke(printer_group, args=['--help'])105106The above invocation returns exit code 0.107108.. click:run::109 invoke(printer_group, args=[])110111The 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.112113.. click:example::114115 @click.group('printer_group', no_args_is_help=False)116 def printer_group():117 pass118119 @printer_group.command('printer')120 @click.option('--this')121 def printer(this):122 if this:123 click.echo(this)124125.. click:run::126 invoke(printer_group, args=[])