diff options
| author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-04-23 18:19:08 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-23 18:19:08 -0400 |
| commit | 9c7bbfa5bdbf8c8d81c0cd3c3cd5179d700de0b2 (patch) | |
| tree | 33169eca113b07a891ef4fcdb70a4bb409b5b4a4 /docs | |
| parent | ecf154f1b13e41644fefd3db624aae1d8c1e0d2a (diff) | |
| parent | 502fdff29b755bec83808352db8777e6e1057757 (diff) | |
| download | cmd2-git-9c7bbfa5bdbf8c8d81c0cd3c3cd5179d700de0b2.tar.gz | |
Merge pull request #929 from python-cmd2/completion_docs
Completion docs
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/features/completion.rst | 62 |
1 files changed, 59 insertions, 3 deletions
diff --git a/docs/features/completion.rst b/docs/features/completion.rst index 3a2c50e0..77a51136 100644 --- a/docs/features/completion.rst +++ b/docs/features/completion.rst @@ -34,10 +34,65 @@ similar to the following to your class which inherits from :class:`cmd2.Cmd`:: complete_bar = functools.partialmethod(cmd2.Cmd.path_complete, path_filter=os.path.isdir) -Tab Completion Using Argparse Decorators +Included Tab Completion Functions +--------------------------------- +``cmd2`` provides the following tab completion functions + +- :attr:`cmd2.utils.basic_complete` - helper method for tab completion against + a list +- :attr:`cmd2.Cmd.path_complete` - helper method provides flexible tab + completion of file system paths + + - See the paged_output_ example for a simple use case + - See the python_scripting_ example for a more full-featured use case + +- :attr:`cmd2.Cmd.delimiter_complete` - helper method for tab completion + against a list but each match is split on a delimiter + + - See the basic_completion_ example for a demonstration of how to use + this feature + +- :attr:`cmd2.Cmd.flag_based_complete` - helper method for tab completion based + on a particular flag preceding the token being completed +- :attr:`cmd2.Cmd.index_based_complete` - helper method for tab completion + based on a fixed position in the input string + + - See the basic_completion_ example for a demonstration of how to use these + features + - ``flag_based_complete()`` and ``index_based_complete()`` are basic + methods and should only be used if you are not familiar with argparse. + The recommended approach for tab completing positional tokens and flags + is to use argparse-based_ completion. + +.. _paged_output: https://github.com/python-cmd2/cmd2/blob/master/examples/paged_output.py +.. _python_scripting: https://github.com/python-cmd2/cmd2/blob/master/examples/python_scripting.py +.. _basic_completion: https://github.com/python-cmd2/cmd2/blob/master/examples/basic_completion.py + + +Raising Exceptions During Completion +------------------------------------ +There are times when an error occurs while tab completing and a message needs +to be reported to the user. These include the following example cases: + +- Reading a database to retrieve a tab completion data set failed +- A previous command line argument that determines the data set being completed + is invalid +- Tab completion hints + +``cmd2`` provides the :class:`cmd2.utils.CompletionError` exception class for +this capability. If an error occurs in which it is more desirable to display +a message than a stack trace, then raise a ``CompletionError``. By default, the +message displays in red like an error. However, ``CompletionError`` has a +member called ``apply_style``. Set this False if the error style should not be +applied. For instance, ``ArgparseCompleter`` sets it to False when displaying +completion hints. + +.. _argparse-based: + +Tab Completion Using argparse Decorators ---------------------------------------- -When using one the Argparse-based :ref:`api/decorators:cmd2.decorators`, +When using one the argparse-based :ref:`api/decorators:cmd2.decorators`, ``cmd2`` provides automatic tab completion of flag names. 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 For More Information -------------------- -See :mod:`cmd2.argparse_custom` for more details. +See :mod:`cmd2.argparse_custom` for a more detailed discussion of argparse +completion. |
