Skip to content

Commit 9c7bbfa

Browse files
authored
Merge pull request #929 from python-cmd2/completion_docs
Completion docs
2 parents ecf154f + 502fdff commit 9c7bbfa

File tree

5 files changed

+80
-25
lines changed

5 files changed

+80
-25
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,18 @@ Instructions for implementing each feature follow.
171171
- And also provide help hints for values associated with these flags
172172
- Experiment with the [argprint.py](https://github.com/python-cmd2/cmd2/blob/master/examples/arg_print.py) example
173173
using the **oprint** and **pprint** commands to get a feel for how this works
174+
- `basic_complete` helper method for tab completion against a list
174175
- `path_complete` helper method provides flexible tab completion of file system paths
175176
- See the [paged_output.py](https://github.com/python-cmd2/cmd2/blob/master/examples/paged_output.py) example for a simple use case
176177
- See the [python_scripting.py](https://github.com/python-cmd2/cmd2/blob/master/examples/python_scripting.py) example for a more full-featured use case
177-
- `flag_based_complete` helper method for tab completion based on a particular flag preceding the token being completed
178-
- See the [basic_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/basic_completion.py) example for a demonstration of how to use this feature
179-
- `index_based_complete` helper method for tab completion based on a fixed position in the input string
180-
- See the [basic_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/basic_completion.py) example for a demonstration of how to use this feature
181-
- `basic_complete` helper method for tab completion against a list
182178
- `delimiter_complete` helper method for tab completion against a list but each match is split on a delimiter
183179
- See the [basic_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/basic_completion.py) example for a demonstration of how to use this feature
180+
- `flag_based_complete` helper method for tab completion based on a particular flag preceding the token being completed
181+
- `index_based_complete` helper method for tab completion based on a fixed position in the input string
182+
- See the [basic_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/basic_completion.py) example for a demonstration of how to use these features
183+
- `flag_based_complete()` and `index_based_complete()` are basic methods and should only be used if you are not
184+
familiar with argparse. The recommended approach for tab completing positional tokens and flags is to use
185+
argparse-based completion
184186
- `cmd2` in combination with `argparse` also provide several advanced capabilities for automatic tab completion
185187
- See the [argparse_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/argparse_completion.py) example for more info
186188

cmd2/argparse_custom.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,15 @@ def my_completer_method(self, text, line, begidx, endidx, arg_tokens)
188188
189189
**Patched argparse functions**
190190
191-
argparse._ActionsContainer.add_argument - adds arguments related to tab
192-
completion and enables nargs range
193-
parsing See _add_argument_wrapper for
194-
more details on these argument
195-
196-
argparse.ArgumentParser._get_nargs_pattern - adds support to for nargs ranges
197-
See _get_nargs_pattern_wrapper for
198-
more details
199-
200-
argparse.ArgumentParser._match_argument - adds support to for nargs ranges See
201-
_match_argument_wrapper for more
202-
details
191+
``argparse._ActionsContainer.add_argument`` - adds arguments related to tab
192+
completion and enables nargs range parsing. See _add_argument_wrapper for
193+
more details on these arguments.
194+
195+
``argparse.ArgumentParser._get_nargs_pattern`` - adds support to for nargs
196+
ranges. See _get_nargs_pattern_wrapper for more details.
197+
198+
``argparse.ArgumentParser._match_argument`` - adds support to for nargs ranges.
199+
See _match_argument_wrapper for more details.
203200
"""
204201

205202
import argparse

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: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,65 @@ 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-
Tab Completion Using Argparse Decorators
37+
Included Tab Completion Functions
38+
---------------------------------
39+
``cmd2`` provides the following tab completion functions
40+
41+
- :attr:`cmd2.utils.basic_complete` - helper method for tab completion against
42+
a list
43+
- :attr:`cmd2.Cmd.path_complete` - helper method provides flexible tab
44+
completion of file system paths
45+
46+
- See the paged_output_ example for a simple use case
47+
- See the python_scripting_ example for a more full-featured use case
48+
49+
- :attr:`cmd2.Cmd.delimiter_complete` - helper method for tab completion
50+
against a list but each match is split on a delimiter
51+
52+
- See the basic_completion_ example for a demonstration of how to use
53+
this feature
54+
55+
- :attr:`cmd2.Cmd.flag_based_complete` - helper method for tab completion based
56+
on a particular flag preceding the token being completed
57+
- :attr:`cmd2.Cmd.index_based_complete` - helper method for tab completion
58+
based on a fixed position in the input string
59+
60+
- See the basic_completion_ example for a demonstration of how to use these
61+
features
62+
- ``flag_based_complete()`` and ``index_based_complete()`` are basic
63+
methods and should only be used if you are not familiar with argparse.
64+
The recommended approach for tab completing positional tokens and flags
65+
is to use argparse-based_ completion.
66+
67+
.. _paged_output: https://github.com/python-cmd2/cmd2/blob/master/examples/paged_output.py
68+
.. _python_scripting: https://github.com/python-cmd2/cmd2/blob/master/examples/python_scripting.py
69+
.. _basic_completion: https://github.com/python-cmd2/cmd2/blob/master/examples/basic_completion.py
70+
71+
72+
Raising Exceptions During Completion
73+
------------------------------------
74+
There are times when an error occurs while tab completing and a message needs
75+
to be reported to the user. These include the following example cases:
76+
77+
- Reading a database to retrieve a tab completion data set failed
78+
- A previous command line argument that determines the data set being completed
79+
is invalid
80+
- Tab completion hints
81+
82+
``cmd2`` provides the :class:`cmd2.utils.CompletionError` exception class for
83+
this capability. If an error occurs in which it is more desirable to display
84+
a message than a stack trace, then raise a ``CompletionError``. By default, the
85+
message displays in red like an error. However, ``CompletionError`` has a
86+
member called ``apply_style``. Set this False if the error style should not be
87+
applied. For instance, ``ArgparseCompleter`` sets it to False when displaying
88+
completion hints.
89+
90+
.. _argparse-based:
91+
92+
Tab Completion Using argparse Decorators
3893
----------------------------------------
3994

40-
When using one the Argparse-based :ref:`api/decorators:cmd2.decorators`,
95+
When using one the argparse-based :ref:`api/decorators:cmd2.decorators`,
4196
``cmd2`` provides automatic tab completion of flag names.
4297

4398
Tab completion of argument values can be configured by using one of five
@@ -80,4 +135,5 @@ See the argparse_completion_ example or the implementation of the built-in
80135
For More Information
81136
--------------------
82137

83-
See :mod:`cmd2.argparse_custom` for more details.
138+
See :mod:`cmd2.argparse_custom` for a more detailed discussion of argparse
139+
completion.

examples/basic_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
A simple example demonstrating how to enable tab completion by assigning a completer function to do_* commands.
55
This also demonstrates capabilities of the following completer features included with cmd2:
66
- CompletionError exceptions
7-
- delimiter_completer()
7+
- delimiter_complete()
88
- flag_based_complete() (see note below)
99
- index_based_complete() (see note below)
1010

0 commit comments

Comments
 (0)