Skip to content

Commit 5fd8e00

Browse files
committed
Documented CompletionError class
1 parent ecf154f commit 5fd8e00

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

cmd2/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def str_to_bool(val: str) -> bool:
7474

7575
class CompletionError(Exception):
7676
"""
77-
Raised during tab completion operations to report any sort of error you want printed by the ArgparseCompleter
78-
This can also be used just to display a message, even if it's not an error. ArgparseCompleter raises
79-
CompletionErrors to display tab completion hints and sets apply_style to False so hints aren't colored
80-
like error text.
77+
Raised during tab completion operations to report any sort of error you want printed. This can also be used
78+
just to display a message, even if it's not an error. For instance, ArgparseCompleter raises CompletionErrors
79+
to display tab completion hints and sets apply_style to False so hints aren't colored like error text.
8180
8281
Example use cases
82+
8383
- Reading a database to retrieve a tab completion data set failed
8484
- A previous command line argument that determines the data set being completed is invalid
8585
- Tab completion hints

docs/features/completion.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ similar to the following to your class which inherits from :class:`cmd2.Cmd`::
3434
complete_bar = functools.partialmethod(cmd2.Cmd.path_complete, path_filter=os.path.isdir)
3535

3636

37+
Raising exceptions during completion
38+
------------------------------------
39+
There are times when tab completion fails and a message needs to be reported to
40+
the user. These include the following example cases:
41+
42+
- Reading a database to retrieve a tab completion data set failed
43+
- A previous command line argument that determines the data set being completed
44+
is invalid
45+
- Tab completion hints
46+
47+
``cmd2`` provides the :class:`cmd2.utils.CompletionError` exception class for
48+
this capability. If an error occurs in which it is more desirable to display
49+
a message than a stack trace, then raise a ``CompletionError``. By default, the
50+
message displays in red like an error. However, ``CompletionError`` has a
51+
member called ``apply_style``. Set this False if the error style should not be
52+
applied. For instance, ``ArgparseCompleter`` sets it to False when displaying
53+
completion hints.
54+
3755
Tab Completion Using Argparse Decorators
3856
----------------------------------------
3957

0 commit comments

Comments
 (0)