summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-03-24 18:15:27 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-03-24 18:15:27 -0400
commitefd3b2cbe243dc520dab4146fae014c19447af53 (patch)
tree104b14c63e8ac352b30e62232bd83bf45b219f92
parent40f8a84dc0177097c8c0f97e91a8b514d1540c0f (diff)
downloadcmd2-git-efd3b2cbe243dc520dab4146fae014c19447af53.tar.gz
Made subcommand specific completion work with Python 2 in the examples
-rwxr-xr-xcmd2.py15
-rwxr-xr-xexamples/subcommands.py10
-rwxr-xr-xexamples/tab_completion.py50
3 files changed, 39 insertions, 36 deletions
diff --git a/cmd2.py b/cmd2.py
index 439c6e38..3249d398 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1911,14 +1911,14 @@ class Cmd(cmd.Cmd):
except AttributeError:
compfunc = self.completedefault
- # If there are subcommands, then try completing those if the cursor is in
- # the token at index 1, otherwise default to using compfunc
subcommands = self.get_subcommands(command)
if subcommands is not None:
+ # Since there are subcommands, then try completing those if the cursor is in
+ # the token at index 1, otherwise default to using compfunc
index_dict = {1: subcommands}
- compfunc = functools.partialmethod(self.index_based_complete,
- index_dict=index_dict,
- all_else=compfunc)
+ compfunc = functools.partial(self.index_based_complete,
+ index_dict=index_dict,
+ all_else=compfunc)
# A valid command was not entered
else:
@@ -2904,10 +2904,11 @@ Usage: Usage: unalias [-a] name [name ...]
To make sure these functions get called, set the tab-completer for the print function
in a similar fashion to what follows where base is the name of the root command (print)
- complete_print = functools.partialmethod(cmd2.Cmd.cmd_with_subs_completer, base='print')
+ def complete_print(self, text, line, begidx, endidx):
+ return self.cmd_with_subs_completer(text, line, begidx, endidx, base='print')
When the subcommand's completer is called, this function will have stripped off all content from the
- beginning of he command line before the subcommand, meaning the line parameter always starts with the
+ beginning of the command line before the subcommand, meaning the line parameter always starts with the
subcommand name and the index parameters reflect this change.
For instance, the command "print names -d 2" becomes "names -d 2"
diff --git a/examples/subcommands.py b/examples/subcommands.py
index bc82b548..0cc529a7 100755
--- a/examples/subcommands.py
+++ b/examples/subcommands.py
@@ -11,7 +11,7 @@ import functools
import sys
import cmd2
-from cmd2 import with_argparser, index_based_complete
+from cmd2 import with_argparser
class SubcommandsExample(cmd2.Cmd):
@@ -38,7 +38,7 @@ class SubcommandsExample(cmd2.Cmd):
""" Adds tab completion to base sport subcommand """
sports = ['Football', 'Hockey', 'Soccer', 'Baseball']
index_dict = {1: sports}
- return index_based_complete(text, line, begidx, endidx, index_dict)
+ return self.index_based_complete(text, line, begidx, endidx, index_dict)
# create the top-level parser for the base command
base_parser = argparse.ArgumentParser(prog='base')
@@ -71,10 +71,8 @@ class SubcommandsExample(cmd2.Cmd):
# No subcommand was provided, so call help
self.do_help('base')
- # functools.partialmethod was added in Python 3.4
- if sys.version_info >= (3, 4):
- # This makes sure correct tab completion functions are called based on the selected subcommand
- complete_base = functools.partialmethod(cmd2.Cmd.cmd_with_subs_completer, base='base')
+ def complete_base(self, text, line, begidx, endidx):
+ return self.cmd_with_subs_completer(text, line, begidx, endidx, base='base')
if __name__ == '__main__':
diff --git a/examples/tab_completion.py b/examples/tab_completion.py
index 6c16e63b..93d6c0ef 100755
--- a/examples/tab_completion.py
+++ b/examples/tab_completion.py
@@ -3,34 +3,14 @@
"""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, path_complete
+from cmd2 import with_argparser, with_argument_list
# 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
- '-o': path_complete, # Tab-complete using path_complete function after -o flag in command line
- '--other': path_complete, # Tab-complete using path_complete function after --other 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
- 3: path_complete, # Tab-complete using path_complete function at index 3 in command line
- }
-
class TabCompleteExample(cmd2.Cmd):
""" Example cmd2 application where we a base command which has a couple subcommands."""
@@ -59,7 +39,23 @@ class TabCompleteExample(cmd2.Cmd):
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)
+ def complete_add_item(self, text, line, begidx, endidx):
+ flag_dict = \
+ {
+ # Tab-complete food items after -f and --food flags in command line
+ '-f': food_item_strs,
+ '--food': food_item_strs,
+
+ # Tab-complete sport items after -s and --sport flags in command line
+ '-s': sport_item_strs,
+ '--sport': sport_item_strs,
+
+ # Tab-complete using path_complete function after -o and --other flags in command line
+ '-o': self.path_complete,
+ '--other': self.path_complete,
+ }
+
+ return self.flag_based_complete(text, line, begidx, endidx, flag_dict=flag_dict)
@with_argument_list
def do_list_item(self, args):
@@ -67,7 +63,15 @@ class TabCompleteExample(cmd2.Cmd):
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)
+ def complete_list_item(self, text, line, begidx, endidx):
+ 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
+ 3: self.path_complete, # Tab-complete using path_complete function at index 3 in command line
+ }
+
+ return self.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
if __name__ == '__main__':