Skip to content

Commit 191f94a

Browse files
committed
Overhauling tab completion examples
1 parent ad0e2ae commit 191f94a

File tree

6 files changed

+40
-284
lines changed

6 files changed

+40
-284
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,14 @@ Instructions for implementing each feature follow.
175175
- See the [paged_output.py](https://github.com/python-cmd2/cmd2/blob/master/examples/paged_output.py) example for a simple use case
176176
- 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
177177
- `flag_based_complete` helper method for tab completion based on a particular flag preceding the token being completed
178-
- See the [tab_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/tab_completion.py) example for a demonstration of how to use this feature
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
179179
- `index_based_complete` helper method for tab completion based on a fixed position in the input string
180-
- See the [tab_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/tab_completion.py) example for a demonstration of how to use this feature
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
181181
- `basic_complete` helper method for tab completion against a list
182182
- `delimiter_complete` helper method for tab completion against a list but each match is split on a delimiter
183-
- See the [tab_autocompletion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion.py) example for a demonstration of how to use this feature
184-
- `cmd2` in combination with `argparse` also provide several advanced capabilities for automatic tab-completion
185-
- See the [tab_autocompletion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion.py) example for more info
183+
- 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
184+
- `cmd2` in combination with `argparse` also provide several advanced capabilities for automatic tab completion
185+
- See the [argparse_completion.py](https://github.com/python-cmd2/cmd2/blob/master/examples/argparse_completion.py) example for more info
186186

187187
- Multi-line commands
188188

docs/features/argument_processing.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,10 @@ You may add multiple layers of subcommands for your command. ``cmd2`` will
328328
automatically traverse and tab-complete subcommands for all commands using
329329
argparse.
330330

331-
See the subcommands_ and tab_autocompletion_ example to learn more about how to
331+
See the subcommands_ example to learn more about how to
332332
use subcommands in your ``cmd2`` application.
333333

334334
.. _subcommands: https://github.com/python-cmd2/cmd2/blob/master/examples/subcommands.py
335-
.. _tab_autocompletion: https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion.py
336335

337336

338337
Argparse Extensions

docs/features/completion.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ implementing the ``do_foo`` method. To enable path completion for the ``foo``
1717
command, then add a line of code similar to the following to your class which
1818
inherits from ``cmd2.Cmd``::
1919

20-
complete_foo = self.path_complete
20+
complete_foo = cmd2.Cmd.path_complete
2121

2222
This will effectively define the ``complete_foo`` readline completer method in
2323
your class and make it utilize the same path completion logic as the built-in
@@ -47,9 +47,9 @@ parameters to ``argparse.ArgumentParser.add_argument()``
4747
- ``completer_function`` / ``completer_method``
4848

4949
See the arg_decorators_ or colors_ example for a demonstration of how to
50-
use the ``choices`` parameter. See the tab_autocompletion_ example for a
50+
use the ``choices`` parameter. See the argparse_completion_ example for a
5151
demonstration of how to use the ``choices_function`` and ``choices_method``
52-
parameters. See the arg_decorators_ or tab_autocompletion_ example for a
52+
parameters. See the arg_decorators_ or argparse_completion_ example for a
5353
demonstration of how to use the ``completer_method`` parameter.
5454

5555
When tab-completing flags and/or argument values for a ``cmd2`` command using
@@ -60,7 +60,7 @@ displayed to help the user.
6060

6161
.. _arg_decorators: https://github.com/python-cmd2/cmd2/blob/master/examples/arg_decorators.py
6262
.. _colors: https://github.com/python-cmd2/cmd2/blob/master/examples/colors.py
63-
.. _tab_autocompletion: https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion.py
63+
.. _argparse_completion: https://github.com/python-cmd2/cmd2/blob/master/examples/argparse_completion.py
6464

6565

6666
CompletionItem For Providing Extra Context
@@ -76,5 +76,5 @@ or ``completion_method``.
7676
.. autoclass:: cmd2.argparse_custom.CompletionItem
7777
:members:
7878

79-
See the tab_autocompletion_ example or the implementation of the built-in
79+
See the argparse_completion_ example or the implementation of the built-in
8080
**set** command for demonstration of how this is used.
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
#!/usr/bin/env python
22
# coding=utf-8
33
"""
4-
A simple example demonstrating how to use flag and index based tab-completion functions
5-
For argparse-based tab completion, see tab_autocompletion.py
4+
A simple example demonstrating how to enable tab completion by assigning a completer function to do_* commands.
5+
This also demonstrates capabilities of the following completer methods included with cmd2:
6+
- flag_based_complete
7+
- index_based_complete
8+
- delimiter_completer
9+
10+
For an example enabling tab completion with argparse, see argparse_completion.py
611
"""
712
import argparse
13+
import functools
814

915
import cmd2
1016

11-
# List of strings used with flag and index based completion functions
17+
# List of strings used with completion functions
1218
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
1319
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
1420

21+
file_strs = \
22+
[
23+
'/home/user/file.db',
24+
'/home/user/file space.db',
25+
'/home/user/another.db',
26+
'/home/other user/maps.db',
27+
'/home/other user/tests.db'
28+
]
29+
1530

1631
class TabCompleteExample(cmd2.Cmd):
1732
""" Example cmd2 application where we a base command which has a couple subcommands."""
18-
1933
def __init__(self):
2034
super().__init__()
2135

36+
# The add_item command uses flag_based_complete
2237
add_item_parser = argparse.ArgumentParser()
2338
add_item_group = add_item_parser.add_mutually_exclusive_group()
2439
add_item_group.add_argument('-f', '--food', help='Adds food item')
@@ -58,6 +73,7 @@ def complete_add_item(self, text, line, begidx, endidx):
5873

5974
return self.flag_based_complete(text, line, begidx, endidx, flag_dict=flag_dict)
6075

76+
# The list_item command uses index_based_complete
6177
@cmd2.with_argument_list
6278
def do_list_item(self, args):
6379
"""List item command help"""
@@ -74,6 +90,14 @@ def complete_list_item(self, text, line, begidx, endidx):
7490

7591
return self.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
7692

93+
# The file_list command uses delimiter_complete
94+
def do_file_list(self, statement: cmd2.Statement):
95+
"""List files entered on command line"""
96+
self.poutput("You selected: {}".format(statement.args))
97+
98+
# Use a partialmethod to set arguments to delimiter_complete
99+
complete_file_list = functools.partialmethod(cmd2.Cmd.delimiter_complete, match_against=file_strs, delimiter='/')
100+
77101

78102
if __name__ == '__main__':
79103
import sys

examples/python_scripting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def do_cd(self, arglist):
8787

8888
# Enable tab completion for cd command
8989
def complete_cd(self, text, line, begidx, endidx):
90+
# Tab complete only directories
9091
return self.path_complete(text, line, begidx, endidx, path_filter=os.path.isdir)
9192

9293
dir_parser = argparse.ArgumentParser()

0 commit comments

Comments
 (0)