Skip to content

Commit 7b4e59c

Browse files
authored
Document help page exit codes (#3122)
2 parents a2623d0 + c150f4d commit 7b4e59c

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

docs/command-line-reference.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local: true
1010
---
1111
```
1212

13+
(exit-codes)=
1314
## Exit Codes
1415

1516
When a command is executed from the command line, then an exit code is return. The exit code, also called exit status or exit status code, is a positive integer that tells you whether the command executed with or without errors.
@@ -44,5 +45,6 @@ To access the exit code, execute the command, then do the following depending:
4445
.. code-block:: text
4546
4647
> echo %ERRORLEVEL%
47-
4848
```
49+
50+
For Click specific behavior on exit codes, see {ref}`exception-handling-exit-codes`.

docs/exceptions.rst

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
Exception Handling
2-
==================
1+
.. _exception-handling-exit-codes:
2+
3+
Exception Handling and Exit Codes
4+
==================================
35

46
.. currentmodule:: click
57

68
Click internally uses exceptions to signal various error conditions that
79
the user of the application might have caused. Primarily this is things
810
like incorrect usage.
911

12+
.. contents::
13+
:depth: 1
14+
:local:
15+
1016
Where are Errors Handled?
1117
-------------------------
1218

@@ -71,3 +77,50 @@ The following common subclasses exist:
7177
the parameter name if possible.
7278
* :exc:`FileError` this is an error that is raised by the
7379
: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

Comments
 (0)