Skip to content

Commit 6cd031d

Browse files
committed
Added tab completion example
1 parent 06a84fd commit 6cd031d

2 files changed

Lines changed: 76 additions & 7 deletions

File tree

examples/tab_completion.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""A simple example demonstrating how to use flag and index based tab-completion functions
4+
"""
5+
import argparse
6+
import functools
7+
8+
import cmd2
9+
from cmd2 import with_argparser, with_argument_list, flag_based_complete, index_based_complete
10+
11+
# List of strings used with flag and index based completion functions
12+
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
13+
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football']
14+
15+
# Dictionary used with flag based completion functions
16+
flag_dict = \
17+
{
18+
'-f': food_item_strs, # Tab-complete food items after -f flag in command line
19+
'--food': food_item_strs, # Tab-complete food items after --food flag in command line
20+
'-s': sport_item_strs, # Tab-complete sport items after -s flag in command line
21+
'--sport': sport_item_strs, # Tab-complete sport items after --sport flag in command line
22+
}
23+
24+
# Dictionary used with index based completion functions
25+
index_dict = \
26+
{
27+
1: food_item_strs, # Tab-complete food items at index 1 in command line
28+
2: sport_item_strs, # Tab-complete sport items at index 2 in command line
29+
}
30+
31+
32+
class TabCompleteExample(cmd2.Cmd):
33+
""" Example cmd2 application where we a base command which has a couple subcommands."""
34+
35+
def __init__(self):
36+
cmd2.Cmd.__init__(self)
37+
38+
add_item_parser = argparse.ArgumentParser()
39+
add_item_group = add_item_parser.add_mutually_exclusive_group()
40+
add_item_group.add_argument('-f', '--food', help='Adds food item')
41+
add_item_group.add_argument('-s', '--sport', help='Adds sport item')
42+
43+
@with_argparser(add_item_parser)
44+
def do_add_item(self, args):
45+
"""Add item command help"""
46+
if args.food:
47+
add_item = args.food
48+
elif args.sport:
49+
add_item = args.sport
50+
else:
51+
add_item = 'no items'
52+
53+
self.poutput("You added {}".format(add_item))
54+
55+
# Add flag-based tab-completion to add_item command
56+
complete_add_item = functools.partial(flag_based_complete, flag_dict=flag_dict)
57+
58+
@with_argument_list
59+
def do_list_item(self, args):
60+
"""List item command help"""
61+
self.poutput("You listed {}".format(args))
62+
63+
# Add index-based tab-completion to list_item command
64+
complete_list_item = functools.partial(index_based_complete, index_dict=index_dict)
65+
66+
67+
if __name__ == '__main__':
68+
app = TabCompleteExample()
69+
app.cmdloop()

tests/test_completion.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ def test_path_completion_directories_only(request):
391391

392392
# List of strings used with flag and index based completion functions
393393
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
394-
sports_equipment_strs = ['Bat', 'Basket', 'Basketball', 'Football']
394+
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football']
395395

396396
# Dictionary used with flag based completion functions
397-
flag_dict = {'-f': food_item_strs, '-s': sports_equipment_strs}
397+
flag_dict = {'-f': food_item_strs, '-s': sport_item_strs}
398398

399399
def test_flag_based_completion_single_end():
400400
text = 'Pi'
@@ -439,7 +439,7 @@ def test_flag_based_default_completer(request):
439439
assert flag_based_complete(text, line, begidx, endidx, flag_dict, path_complete) == ['conftest.py ']
440440

441441
# Dictionary used with index based completion functions
442-
index_dict = {1: food_item_strs, 2: sports_equipment_strs}
442+
index_dict = {1: food_item_strs, 2: sport_item_strs}
443443

444444
def test_index_based_completion_single_end():
445445
text = 'Foo'
@@ -462,7 +462,7 @@ def test_index_based_completion_multiple():
462462
line = 'command Pizza '
463463
endidx = len(line)
464464
begidx = endidx - len(text)
465-
assert index_based_complete(text, line, begidx, endidx, index_dict) == sorted(sports_equipment_strs)
465+
assert index_based_complete(text, line, begidx, endidx, index_dict) == sorted(sport_item_strs)
466466

467467
def test_index_based_completion_nomatch():
468468
text = 'q'
@@ -494,9 +494,9 @@ def test_parseline_command_and_args(cmd2_app):
494494
def test_parseline_emptyline(cmd2_app):
495495
line = ''
496496
command, args, out_line = cmd2_app.parseline(line)
497-
assert command == None
498-
assert args == None
499-
assert line == out_line
497+
assert command is None
498+
assert args is None
499+
assert line is out_line
500500

501501
def test_parseline_strips_line(cmd2_app):
502502
line = ' help history '

0 commit comments

Comments
 (0)