summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xexamples/tab_completion.py69
-rw-r--r--tests/test_completion.py14
2 files changed, 76 insertions, 7 deletions
diff --git a/examples/tab_completion.py b/examples/tab_completion.py
new file mode 100755
index 00000000..400775a8
--- /dev/null
+++ b/examples/tab_completion.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+# coding=utf-8
+"""A simple example demonstrating how to use flag and index based tab-completion functions
+"""
+import argparse
+import functools
+
+import cmd2
+from cmd2 import with_argparser, with_argument_list, flag_based_complete, index_based_complete
+
+# List of strings used with flag and index based completion functions
+food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
+sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football']
+
+# Dictionary used with flag based completion functions
+flag_dict = \
+ {
+ '-f': food_item_strs, # Tab-complete food items after -f flag in command line
+ '--food': food_item_strs, # Tab-complete food items after --food flag in command line
+ '-s': sport_item_strs, # Tab-complete sport items after -s flag in command line
+ '--sport': sport_item_strs, # Tab-complete sport items after --sport flag in command line
+ }
+
+# Dictionary used with index based completion functions
+index_dict = \
+ {
+ 1: food_item_strs, # Tab-complete food items at index 1 in command line
+ 2: sport_item_strs, # Tab-complete sport items at index 2 in command line
+ }
+
+
+class TabCompleteExample(cmd2.Cmd):
+ """ Example cmd2 application where we a base command which has a couple subcommands."""
+
+ def __init__(self):
+ cmd2.Cmd.__init__(self)
+
+ add_item_parser = argparse.ArgumentParser()
+ add_item_group = add_item_parser.add_mutually_exclusive_group()
+ add_item_group.add_argument('-f', '--food', help='Adds food item')
+ add_item_group.add_argument('-s', '--sport', help='Adds sport item')
+
+ @with_argparser(add_item_parser)
+ def do_add_item(self, args):
+ """Add item command help"""
+ if args.food:
+ add_item = args.food
+ elif args.sport:
+ add_item = args.sport
+ else:
+ add_item = 'no items'
+
+ self.poutput("You added {}".format(add_item))
+
+ # Add flag-based tab-completion to add_item command
+ complete_add_item = functools.partial(flag_based_complete, flag_dict=flag_dict)
+
+ @with_argument_list
+ def do_list_item(self, args):
+ """List item command help"""
+ self.poutput("You listed {}".format(args))
+
+ # Add index-based tab-completion to list_item command
+ complete_list_item = functools.partial(index_based_complete, index_dict=index_dict)
+
+
+if __name__ == '__main__':
+ app = TabCompleteExample()
+ app.cmdloop()
diff --git a/tests/test_completion.py b/tests/test_completion.py
index bdeae2e2..dcebd5f6 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -391,10 +391,10 @@ def test_path_completion_directories_only(request):
# List of strings used with flag and index based completion functions
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
-sports_equipment_strs = ['Bat', 'Basket', 'Basketball', 'Football']
+sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football']
# Dictionary used with flag based completion functions
-flag_dict = {'-f': food_item_strs, '-s': sports_equipment_strs}
+flag_dict = {'-f': food_item_strs, '-s': sport_item_strs}
def test_flag_based_completion_single_end():
text = 'Pi'
@@ -439,7 +439,7 @@ def test_flag_based_default_completer(request):
assert flag_based_complete(text, line, begidx, endidx, flag_dict, path_complete) == ['conftest.py ']
# Dictionary used with index based completion functions
-index_dict = {1: food_item_strs, 2: sports_equipment_strs}
+index_dict = {1: food_item_strs, 2: sport_item_strs}
def test_index_based_completion_single_end():
text = 'Foo'
@@ -462,7 +462,7 @@ def test_index_based_completion_multiple():
line = 'command Pizza '
endidx = len(line)
begidx = endidx - len(text)
- assert index_based_complete(text, line, begidx, endidx, index_dict) == sorted(sports_equipment_strs)
+ assert index_based_complete(text, line, begidx, endidx, index_dict) == sorted(sport_item_strs)
def test_index_based_completion_nomatch():
text = 'q'
@@ -494,9 +494,9 @@ def test_parseline_command_and_args(cmd2_app):
def test_parseline_emptyline(cmd2_app):
line = ''
command, args, out_line = cmd2_app.parseline(line)
- assert command == None
- assert args == None
- assert line == out_line
+ assert command is None
+ assert args is None
+ assert line is out_line
def test_parseline_strips_line(cmd2_app):
line = ' help history '