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

1.. _exception-handling-exit-codes:23Exception Handling and Exit Codes4==================================56.. currentmodule:: click78Click internally uses exceptions to signal various error conditions that9the user of the application might have caused.  Primarily this is things10like incorrect usage.1112.. contents::13    :depth: 114    :local:1516Where are Errors Handled?17-------------------------1819Click's main error handling is happening in :meth:`Command.main`.  In20there it handles all subclasses of :exc:`ClickException` as well as the21standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions.  The22latter are internally translated into an :exc:`Abort`.2324The logic applied is the following:25261.  If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it27    as :exc:`Abort`.282.  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`.313.  If an :exc:`Abort` exception is raised print the string ``Aborted!``32    to standard error and exit the program with exit code ``1``.334.  If it goes through well, exit the program with exit code ``0``.3435What 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.808182.. _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=[])