Skip to content
Myr

exceptions.md

This file

Path

docs/exceptions.md

136 lines · 4.1 KiB · Markdown

Last changed by

[pre-commit.ci lite] apply automatic fixespre-commit-ci-lite[bot]before adoption1a1cdcb

History of this file

Take it

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 1why, one step back
1(exception-handling-exit-codes)=

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 2why, one step back
2

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 3why, one step back
3# Exception Handling and Exit Codes

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 4why, one step back
4

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 5why, one step back
5```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 6why, one step back
6.. currentmodule:: click

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 7why, one step back
7```

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 8–9why, one step back
8
9Click internally uses exceptions to signal various error conditions that

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 10why, one step back
10the user of the application might have caused. Primarily this is things

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 11–12why, one step back
11like incorrect usage.
12

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 13–16why, one step back
13```{contents}
14:depth: 115:local:16```

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 17why, one step back
17

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 18why, one step back
18## Where are Errors Handled?

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 19why, one step back
19

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 20–23why, one step back
20Click's main error handling is happening in {meth}`Command.main`. In
21there it handles all subclasses of {exc}`ClickException` as well as the22standard {exc}`EOFError` and {exc}`KeyboardInterrupt` exceptions. The23latter are internally translated into an {exc}`Abort`.

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 24–26why, one step back
24
25The logic applied is the following:26

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 27–34why, one step back
271. If an {exc}`EOFError` or {exc}`KeyboardInterrupt` happens, reraise it
28 as {exc}`Abort`.292. If a {exc}`ClickException` is raised, invoke the30 {meth}`ClickException.show` method on it to display it and then exit31 the program with {attr}`ClickException.exit_code`.323. If an {exc}`Abort` exception is raised print the string ``Aborted!``33 to standard error and exit the program with exit code ``1``.344. If it goes through well, exit the program with exit code ``0``.

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 35why, one step back
35

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 36why, one step back
36## What if I Don't Want That?

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 37why, one step back
37

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 38–40why, one step back
38Generally you always have the option to invoke the {meth}`Command.invoke`
39method yourself. For instance if you have a {class}`Command` you can40invoke it manually like this:

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 41why, one step back
41

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 42–46why, one step back
42```python
43ctx = command.make_context("command-name", ["args", "go", "here"])44with ctx:45 result = command.invoke(ctx)46```

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 47–50why, one step back
47
48In this case exceptions will not be handled at all and bubbled up as you49would expect.50

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 51why, one step back
51Starting with Click 3.0 you can also use the {meth}`Command.main` method

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 52why, one step back
52but disable the standalone mode which will do two things: disable

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 53why, one step back
53exception handling and disable the implicit {func}`sys.exit` at the end.

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 54why, one step back
54

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 55why, one step back
55So you can do something like this:

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 56why, one step back
56

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 57–62why, one step back
57```python
58command.main(59 ["command-name", "args", "go", "here"],60 standalone_mode=False,61)62```

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 63why, one step back
63

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 64why, one step back
64## Which Exceptions Exist?

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 65why, one step back
65

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 66–67why, one step back
66Click has two exception bases: {exc}`ClickException` which is raised for
67all exceptions that Click wants to signal to the user and {exc}`Abort`

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 68–69why, one step back
68which is used to instruct Click to abort the execution.
69

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 70–71why, one step back
70A {exc}`ClickException` has a {meth}`ClickException.show` method which
71can render an error message to stderr or the given file object. If you

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 72–76why, one step back
72want to use the exception yourself for doing something check the API docs
73about what else they provide.7475The following common subclasses exist:76

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 77–84why, one step back
77- {exc}`UsageError` to inform the user that something went wrong.
78- {exc}`BadParameter` to inform the user that something went wrong with79 a specific parameter. These are often handled internally in Click and80 augmented with extra information if possible. For instance if those81 are raised from a callback Click will automatically augment it with82 the parameter name if possible.83- {exc}`FileError` this is an error that is raised by the84 {class}`FileType` if Click encounters issues opening the file.

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 85why, one step back
85

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 86why, one step back
86(help-page-exit-codes)=

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 87why, one step back
87

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 88why, one step back
88## Help Pages and Exit Codes

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

line 89why, one step back
89

switch format from rest to mystdenuowebbefore adoptiond9b37c7

lines 90–93why, one step back
90Triggering the a help page intentionally (by passing in ``--help``)
91returns exit code 0. If a help page is displayed due to incorrect user92input, the program returns exit code 2. See {ref}`exit-codes` for more93general information.

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 94–96why, one step back
94
95For clarity, here is an example.96

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 97why, one step back
97```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 98–116why, one step back
98.. click:example::
99100 @click.group('printer_group')101 def printer_group():102 pass103104 @printer_group.command('printer')105 @click.option('--this')106 def printer(this):107 if this:108 click.echo(this)109110.. click:run::111 invoke(printer_group, args=['--help'])112113The above invocation returns exit code 0.114115.. click:run::116 invoke(printer_group, args=[])

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 117why, one step back
117```

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 118–120why, one step back
118
119The 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.120

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 121why, one step back
121```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoptionfb3a0cd

lines 122–135why, one step back
122.. click:example::
123124 @click.group('printer_group', no_args_is_help=False)125 def printer_group():126 pass127128 @printer_group.command('printer')129 @click.option('--this')130 def printer(this):131 if this:132 click.echo(this)133134.. click:run::135 invoke(printer_group, args=[])

switch format from rest to mystdenuowebbefore adoptiond9b37c7

line 136why, one step back
136```