|
1 | | -Exception Handling |
2 | | -================== |
| 1 | +.. _exception-handling-exit-codes: |
| 2 | + |
| 3 | +Exception Handling and Exit Codes |
| 4 | +================================== |
3 | 5 |
|
4 | 6 | .. currentmodule:: click |
5 | 7 |
|
6 | 8 | Click internally uses exceptions to signal various error conditions that |
7 | 9 | the user of the application might have caused. Primarily this is things |
8 | 10 | like incorrect usage. |
9 | 11 |
|
| 12 | +.. contents:: |
| 13 | + :depth: 1 |
| 14 | + :local: |
| 15 | + |
10 | 16 | Where are Errors Handled? |
11 | 17 | ------------------------- |
12 | 18 |
|
@@ -71,3 +77,50 @@ The following common subclasses exist: |
71 | 77 | the parameter name if possible. |
72 | 78 | * :exc:`FileError` this is an error that is raised by the |
73 | 79 | :exc:`FileType` if Click encounters issues opening the file. |
| 80 | + |
| 81 | + |
| 82 | +.. _help-page-exit-codes: |
| 83 | + |
| 84 | +Help Pages and Exit Codes |
| 85 | +-------------------------- |
| 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 | + pass |
| 96 | + |
| 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 | + pass |
| 118 | + |
| 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=[]) |
0 commit comments