summaryrefslogtreecommitdiff
path: root/examples/tab_completion.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2020-02-13 22:34:22 -0500
committerGitHub <noreply@github.com>2020-02-13 22:34:22 -0500
commit013b9e0a2c75e17f8aa0e0f7cbe50d84d2f657d8 (patch)
treee3cc4c27ed21a2e1d01caae0bda4ea51c44e59f6 /examples/tab_completion.py
parentad0e2ae0d0d426fe08353fd82d1f9ff051be9108 (diff)
parent7fe5cf7c1ab7d7a68991d1aeebaa338f7c5d2fd0 (diff)
downloadcmd2-git-013b9e0a2c75e17f8aa0e0f7cbe50d84d2f657d8.tar.gz
Merge pull request #887 from python-cmd2/completion_docs
Completion docs
Diffstat (limited to 'examples/tab_completion.py')
-rwxr-xr-xexamples/tab_completion.py81
1 files changed, 0 insertions, 81 deletions
diff --git a/examples/tab_completion.py b/examples/tab_completion.py
deleted file mode 100755
index 1a25238f..00000000
--- a/examples/tab_completion.py
+++ /dev/null
@@ -1,81 +0,0 @@
-#!/usr/bin/env python
-# coding=utf-8
-"""
-A simple example demonstrating how to use flag and index based tab-completion functions
-For argparse-based tab completion, see tab_autocompletion.py
-"""
-import argparse
-
-import cmd2
-
-# List of strings used with flag and index based completion functions
-food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
-sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
-
-
-class TabCompleteExample(cmd2.Cmd):
- """ Example cmd2 application where we a base command which has a couple subcommands."""
-
- def __init__(self):
- super().__init__()
-
- 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)
-
- @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)
-
-
-if __name__ == '__main__':
- import sys
- app = TabCompleteExample()
- sys.exit(app.cmdloop())