From 191f94abda1c4d565ea5b2dd1bd66e346db3b51b Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 12 Feb 2020 15:47:29 -0500 Subject: Overhauling tab completion examples --- examples/basic_completion.py | 105 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 examples/basic_completion.py (limited to 'examples/basic_completion.py') diff --git a/examples/basic_completion.py b/examples/basic_completion.py new file mode 100755 index 00000000..4baec16c --- /dev/null +++ b/examples/basic_completion.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# coding=utf-8 +""" +A simple example demonstrating how to enable tab completion by assigning a completer function to do_* commands. +This also demonstrates capabilities of the following completer methods included with cmd2: +- flag_based_complete +- index_based_complete +- delimiter_completer + +For an example enabling tab completion with argparse, see argparse_completion.py +""" +import argparse +import functools + +import cmd2 + +# List of strings used with completion functions +food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato'] +sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] + +file_strs = \ + [ + '/home/user/file.db', + '/home/user/file space.db', + '/home/user/another.db', + '/home/other user/maps.db', + '/home/other user/tests.db' + ] + + +class TabCompleteExample(cmd2.Cmd): + """ Example cmd2 application where we a base command which has a couple subcommands.""" + def __init__(self): + super().__init__() + + # The add_item command uses flag_based_complete + 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') + add_item_group.add_argument('-o', '--other', help='Adds other item') + + @cmd2.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 + elif args.other: + add_item = args.other + else: + add_item = 'no items' + + self.poutput("You added {}".format(add_item)) + + # Add flag-based tab-completion to add_item command + 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) + + # The list_item command uses index_based_complete + @cmd2.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 + 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) + + # The file_list command uses delimiter_complete + def do_file_list(self, statement: cmd2.Statement): + """List files entered on command line""" + self.poutput("You selected: {}".format(statement.args)) + + # Use a partialmethod to set arguments to delimiter_complete + complete_file_list = functools.partialmethod(cmd2.Cmd.delimiter_complete, match_against=file_strs, delimiter='/') + + +if __name__ == '__main__': + import sys + app = TabCompleteExample() + sys.exit(app.cmdloop()) -- cgit v1.2.1 From c3e3c1c595d65ec4c5fa2c9dac88ffa30cf4738e Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 13 Feb 2020 10:46:42 -0500 Subject: Removed use of argparse from basic completion example --- examples/basic_completion.py | 76 +++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 46 deletions(-) (limited to 'examples/basic_completion.py') diff --git a/examples/basic_completion.py b/examples/basic_completion.py index 4baec16c..3750c2c6 100755 --- a/examples/basic_completion.py +++ b/examples/basic_completion.py @@ -7,9 +7,8 @@ This also demonstrates capabilities of the following completer methods included - index_based_complete - delimiter_completer -For an example enabling tab completion with argparse, see argparse_completion.py +For an example integrating tab completion with argparse, see argparse_completion.py """ -import argparse import functools import cmd2 @@ -18,6 +17,7 @@ import cmd2 food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato'] sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] +# This data is used to demonstrate delimiter_complete file_strs = \ [ '/home/user/file.db', @@ -28,34 +28,20 @@ file_strs = \ ] -class TabCompleteExample(cmd2.Cmd): - """ Example cmd2 application where we a base command which has a couple subcommands.""" - def __init__(self): - super().__init__() - - # The add_item command uses flag_based_complete - 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') - add_item_group.add_argument('-o', '--other', help='Adds other item') - - @cmd2.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 - elif args.other: - add_item = args.other - else: - add_item = 'no items' - - self.poutput("You added {}".format(add_item)) - - # Add flag-based tab-completion to add_item command - def complete_add_item(self, text, line, begidx, endidx): +class BasicCompletion(cmd2.Cmd): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def do_flag_based(self, statement: cmd2.Statement): + """Tab completes arguments based on a preceding flag using flag_based_complete + -f, --food [completes food items] + -s, --sport [completes sports] + -p, --path [completes local file system paths] + """ + self.poutput("Args: {}".format(statement.args)) + + def complete_flag_based(self, text, line, begidx, endidx): + """Completion function for do_flag_based""" flag_dict = \ { # Tab-complete food items after -f and --food flags in command line @@ -66,21 +52,19 @@ class TabCompleteExample(cmd2.Cmd): '-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, + # Tab-complete using path_complete function after -p and --path flags in command line + '-p': self.path_complete, + '--path': self.path_complete, } return self.flag_based_complete(text, line, begidx, endidx, flag_dict=flag_dict) - # The list_item command uses index_based_complete - @cmd2.with_argument_list - def do_list_item(self, args): - """List item command help""" - self.poutput("You listed {}".format(args)) + def do_index_based(self, statement: cmd2.Statement): + """Tab completes first 3 arguments using index_based_complete""" + self.poutput("Args: {}".format(statement.args)) - # Add index-based tab-completion to list_item command - def complete_list_item(self, text, line, begidx, endidx): + def complete_index_based(self, text, line, begidx, endidx): + """Completion function for do_index_based""" index_dict = \ { 1: food_item_strs, # Tab-complete food items at index 1 in command line @@ -90,16 +74,16 @@ class TabCompleteExample(cmd2.Cmd): return self.index_based_complete(text, line, begidx, endidx, index_dict=index_dict) - # The file_list command uses delimiter_complete - def do_file_list(self, statement: cmd2.Statement): - """List files entered on command line""" - self.poutput("You selected: {}".format(statement.args)) + def do_delimiter_complete(self, statement: cmd2.Statement): + """Tab completes files from a list using delimiter_complete""" + self.poutput("Args: {}".format(statement.args)) # Use a partialmethod to set arguments to delimiter_complete - complete_file_list = functools.partialmethod(cmd2.Cmd.delimiter_complete, match_against=file_strs, delimiter='/') + complete_delimiter_complete = functools.partialmethod(cmd2.Cmd.delimiter_complete, + match_against=file_strs, delimiter='/') if __name__ == '__main__': import sys - app = TabCompleteExample() + app = BasicCompletion() sys.exit(app.cmdloop()) -- cgit v1.2.1 From 41019de4b55f42d17149c29d380358dc38347948 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 13 Feb 2020 13:39:53 -0500 Subject: Removed dash from 'tab complete' string --- examples/basic_completion.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'examples/basic_completion.py') diff --git a/examples/basic_completion.py b/examples/basic_completion.py index 3750c2c6..f21f75fc 100755 --- a/examples/basic_completion.py +++ b/examples/basic_completion.py @@ -44,15 +44,15 @@ class BasicCompletion(cmd2.Cmd): """Completion function for do_flag_based""" flag_dict = \ { - # Tab-complete food items after -f and --food flags in command line + # 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 + # 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 -p and --path flags in command line + # Tab complete using path_complete function after -p and --path flags in command line '-p': self.path_complete, '--path': self.path_complete, } @@ -67,9 +67,9 @@ class BasicCompletion(cmd2.Cmd): """Completion function for do_index_based""" 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 + 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) -- cgit v1.2.1 From 19312f442be58590f4373e2455c4ee14e3397174 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 13 Feb 2020 16:43:23 -0500 Subject: Updated documentation --- examples/basic_completion.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'examples/basic_completion.py') diff --git a/examples/basic_completion.py b/examples/basic_completion.py index f21f75fc..615f949d 100755 --- a/examples/basic_completion.py +++ b/examples/basic_completion.py @@ -3,14 +3,14 @@ """ A simple example demonstrating how to enable tab completion by assigning a completer function to do_* commands. This also demonstrates capabilities of the following completer methods included with cmd2: -- flag_based_complete -- index_based_complete - delimiter_completer +- flag_based_complete (see note below) +- index_based_complete (see note below) -For an example integrating tab completion with argparse, see argparse_completion.py +flag_based_complete() and index_based_complete() are basic methods and should only be used if you are not +familiar with argparse. The recommended approach for tab completing positional tokens and flags is to use +argparse-based completion. For an example integrating tab completion with argparse, see argparse_completion.py """ -import functools - import cmd2 # List of strings used with completion functions -- cgit v1.2.1 From 7fe5cf7c1ab7d7a68991d1aeebaa338f7c5d2fd0 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 13 Feb 2020 18:37:50 -0500 Subject: Fixed missing import --- examples/basic_completion.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples/basic_completion.py') diff --git a/examples/basic_completion.py b/examples/basic_completion.py index 615f949d..e021828b 100755 --- a/examples/basic_completion.py +++ b/examples/basic_completion.py @@ -11,6 +11,8 @@ flag_based_complete() and index_based_complete() are basic methods and should on familiar with argparse. The recommended approach for tab completing positional tokens and flags is to use argparse-based completion. For an example integrating tab completion with argparse, see argparse_completion.py """ +import functools + import cmd2 # List of strings used with completion functions -- cgit v1.2.1