diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-13 10:46:42 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-13 10:46:42 -0500 |
commit | c3e3c1c595d65ec4c5fa2c9dac88ffa30cf4738e (patch) | |
tree | faee34d5ce00faa6ff188f19fde2e9d1a0c9f94d /examples | |
parent | 191f94abda1c4d565ea5b2dd1bd66e346db3b51b (diff) | |
download | cmd2-git-c3e3c1c595d65ec4c5fa2c9dac88ffa30cf4738e.tar.gz |
Removed use of argparse from basic completion example
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/basic_completion.py | 76 |
1 files changed, 30 insertions, 46 deletions
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()) |