Skip to content

Commit 056ea31

Browse files
committed
Updated documentation with info on tab completion of file system paths
1 parent 63d5be9 commit 056ea31

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ cmd2 provides the following features, in addition to those already existing in c
3535
- Pipe output to shell commands with `|`
3636
- Simple transcript-based application testing
3737
- Unicode character support (*Python 3 only*)
38-
- Path completion for ``edit``, ``load``, ``save``, and ``shell`` commands
39-
- Integrated Python scripting capability via ``pyscript`` and ``py``
38+
- Tab completion of file system paths for ``edit``, ``load``, ``pyscript``, ``save``, and ``shell`` commands
39+
- Integrated Python scripting capability via ``pyscript`` and ``py`` commands
40+
- (Optional) Embedded IPython shell integration via optional opt-in ``ipy`` command
4041

4142
Instructions for implementing each feature follow.
4243

docs/freefeatures.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,34 @@ expressions.
323323
app = App(transcript_files=['exampleSession.txt'])
324324
app.cmdloop()
325325

326+
327+
Tab-Completion
328+
==============
329+
330+
``cmd2`` adds tab-completion of file system paths for all built-in commands where it makes sense, including:
331+
332+
- ``edit``
333+
- ``load``
334+
- ``pyscript``
335+
- ``save``
336+
- ``shell``
337+
338+
``cmd2`` also adds tab-completion of shell commands to the ``shell`` command.
339+
340+
Additionally, it is trivial to add identical file system path completion to your own custom commands. Suppose you
341+
have defined a custom command ``foo`` by implementing the ``do_foo`` method. To enable path completion for the ``foo``
342+
command, then add a line of code similar to the following to your class which inherits from ``cmd2.Cmd``::
343+
344+
# Assuming you have an "import cmd2" somewhere at the top
345+
complete_foo = cmd2.Cmd.path_complete
346+
347+
This will effectively define the ``complete_foo`` readline completer method in your class and make it utilize the same
348+
path completion logic as the built-in commands.
349+
350+
The build-in logic allows for a few more advanced path completion capabilities, such as cases where you only want to
351+
match directories. Suppose you have a custom command ``bar`` implemented by the ``do_bar`` method. YOu can enable
352+
path completion of directories only for this command by adding a line of code similar to the following to your class
353+
which inherits from ``cmd2.Cmd``::
354+
355+
# Make sure you have an "import functools" somewhere at the top
356+
complete_bar = functools.partialmethod(cmd2.Cmd.path_complete, dir_only=True)

docs/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ The basic use of ``cmd2`` is identical to that of cmd_.
3535

3636
The tab-completion feature provided by cmd_ relies on underlying capability provided by GNU readline or an
3737
equivalent library. Linux distros will almost always come with the required library installed.
38-
For Mac OS X, we recommend using the `Homebrew <https://brew.sh>`_ package manager to install the ``readline`` package.
38+
For macOS, we recommend using the `Homebrew <https://brew.sh>`_ package manager to install the ``readline`` package;
39+
alternatively for macOS the ``conda`` package manager that comes with the Anaconda Python distro can be used to
40+
install ``readline`` (preferably from conda-forge).
3941
For Windows, we recommend installing the `pyreadline <https://pypi.python.org/pypi/pyreadline>`_ Python module.
4042

4143
Resources

0 commit comments

Comments
 (0)