summaryrefslogtreecommitdiff
path: root/examples/tab_autocompletion.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tab_autocompletion.py')
-rwxr-xr-xexamples/tab_autocompletion.py259
1 files changed, 0 insertions, 259 deletions
diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py
index 64bcbb65..4b13b5c3 100755
--- a/examples/tab_autocompletion.py
+++ b/examples/tab_autocompletion.py
@@ -5,7 +5,6 @@ A example usage of the AutoCompleter
"""
import argparse
import functools
-import itertools
from typing import List
import cmd2
@@ -179,11 +178,6 @@ class TabCompleteExample(cmd2.Cmd):
if not args.type:
self.do_help('orig_suggest')
- ###################################################################################
- # The media command demonstrates a completer with multiple layers of subcommands
- # - This example demonstrates how to tag a completion attribute on each action, enabling argument
- # completion without implementing a complete_COMMAND function
-
def _do_vid_media_movies(self, args) -> None:
if not args.command:
self.do_help('media movies')
@@ -268,259 +262,6 @@ class TabCompleteExample(cmd2.Cmd):
# No subcommand was provided, so call help
self.do_help('video')
- ###################################################################################
- # The media command demonstrates a completer with multiple layers of subcommands
- # - This example uses a flat completion lookup dictionary
-
- def _do_media_movies(self, args) -> None:
- if not args.command:
- self.do_help('media movies')
- elif args.command == 'list':
- for movie_id in TabCompleteExample.MOVIE_DATABASE:
- movie = TabCompleteExample.MOVIE_DATABASE[movie_id]
- print('{}\n-----------------------------\n{} ID: {}\nDirector: {}\nCast:\n {}\n\n'
- .format(movie['title'], movie['rating'], movie_id,
- ', '.join(movie['director']),
- '\n '.join(movie['actor'])))
- elif args.command == 'add':
- print('Adding Movie\n----------------\nTitle: {}\nRating: {}\nDirectors: {}\nActors: {}\n\n'
- .format(args.title, args.rating, ', '.join(args.director), ', '.join(args.actor)))
-
- def _do_media_shows(self, args) -> None:
- if not args.command:
- self.do_help('media shows')
-
- elif args.command == 'list':
- for show_id in TabCompleteExample.SHOW_DATABASE:
- show = TabCompleteExample.SHOW_DATABASE[show_id]
- print('{}\n-----------------------------\n{} ID: {}'
- .format(show['title'], show['rating'], show_id))
- for season in show['seasons']:
- ep_list = show['seasons'][season]
- print(' Season {}:\n {}'
- .format(season,
- '\n '.join(ep_list)))
- print()
-
- media_parser = Cmd2ArgParser(prog='media')
-
- media_types_subparsers = media_parser.add_subparsers(title='Media Types', dest='type')
-
- movies_parser = media_types_subparsers.add_parser('movies')
- movies_parser.set_defaults(func=_do_media_movies)
-
- movies_commands_subparsers = movies_parser.add_subparsers(title='Commands', dest='command')
-
- movies_list_parser = movies_commands_subparsers.add_parser('list')
-
- movies_list_parser.add_argument('-t', '--title', help='Title Filter')
- movies_list_parser.add_argument('-r', '--rating', help='Rating Filter', nargs='+',
- choices=ratings_types)
- movies_list_parser.add_argument('-d', '--director', help='Director Filter')
- movies_list_parser.add_argument('-a', '--actor', help='Actor Filter', action='append')
-
- movies_add_parser = movies_commands_subparsers.add_parser('add')
- movies_add_parser.add_argument('title', help='Movie Title')
- movies_add_parser.add_argument('rating', help='Movie Rating', choices=ratings_types)
- movies_add_parser.add_argument('-d', '--director', help='Director', nargs=(1, 2), required=True)
- movies_add_parser.add_argument('actor', help='Actors', nargs=argparse.REMAINDER)
-
- movies_delete_parser = movies_commands_subparsers.add_parser('delete')
- movies_delete_parser.add_argument('movie_id', help='Movie ID', choices_method=instance_query_movie_ids,
- descriptive_header='Title')
-
- movies_load_parser = movies_commands_subparsers.add_parser('load')
- movie_file_action = movies_load_parser.add_argument('movie_file', help='Movie database')
-
- shows_parser = media_types_subparsers.add_parser('shows')
- shows_parser.set_defaults(func=_do_media_shows)
-
- shows_commands_subparsers = shows_parser.add_subparsers(title='Commands', dest='command')
-
- shows_list_parser = shows_commands_subparsers.add_parser('list')
-
- @cmd2.with_category(CAT_AUTOCOMPLETE)
- @cmd2.with_argparser(media_parser)
- def do_media(self, args):
- """Media management command demonstrates multiple layers of sub-commands being handled by AutoCompleter"""
- func = getattr(args, 'func', None)
- if func is not None:
- # Call whatever subcommand function was selected
- func(self, args)
- else:
- # No subcommand was provided, so call help
- self.do_help('media')
-
- # This completer is implemented using a single dictionary to look up completion lists for all layers of
- # subcommands. For each argument, AutoCompleter will search for completion values from the provided
- # arg_choices dict. This requires careful naming of argparse arguments so that there are no unintentional
- # name collisions.
- def complete_media(self, text, line, begidx, endidx):
- """ Adds tab completion to media"""
- choices = {'actor': query_actors, # function
- 'director': TabCompleteExample.static_list_directors, # static list
- 'movie_file': (self.path_complete,)
- }
- completer = argparse_completer.AutoCompleter(TabCompleteExample.media_parser,
- self,
- arg_choices=choices)
-
- tokens, _ = self.tokens_for_completion(line, begidx, endidx)
- results = completer.complete_command(tokens, text, line, begidx, endidx)
-
- return results
-
- ###################################################################################
- # The library command demonstrates a completer with multiple layers of subcommands
- # with different completion results per sub-command
- # - This demonstrates how to build a tree of completion lookups to pass down
- #
- # Only use this method if you absolutely need to as it dramatically
- # increases the complexity and decreases readability.
-
- def _do_library_movie(self, args):
- if not args.type or not args.command:
- self.do_help('library movie')
-
- def _do_library_show(self, args):
- if not args.type:
- self.do_help('library show')
-
- # noinspection PyMethodMayBeStatic
- def _query_movie_database(self):
- return list(set(TabCompleteExample.MOVIE_DATABASE_IDS).difference(set(TabCompleteExample.USER_MOVIE_LIBRARY)))
-
- # noinspection PyMethodMayBeStatic
- def _query_movie_user_library(self):
- return TabCompleteExample.USER_MOVIE_LIBRARY
-
- # noinspection PyMethodMayBeStatic, PyUnusedLocal
- def _filter_library(self, text, line, begidx, endidx, full, exclude=()):
- candidates = list(set(full).difference(set(exclude)))
- return [entry for entry in candidates if entry.startswith(text)]
-
- library_parser = Cmd2ArgParser(prog='library')
-
- library_subcommands = library_parser.add_subparsers(title='Media Types', dest='type')
-
- library_movie_parser = library_subcommands.add_parser('movie')
- library_movie_parser.set_defaults(func=_do_library_movie)
-
- library_movie_subcommands = library_movie_parser.add_subparsers(title='Command', dest='command')
-
- library_movie_add_parser = library_movie_subcommands.add_parser('add')
- library_movie_add_parser.add_argument('movie_id', help='ID of movie to add', action='append')
- library_movie_add_parser.add_argument('-b', '--borrowed', action='store_true')
-
- library_movie_remove_parser = library_movie_subcommands.add_parser('remove')
- library_movie_remove_parser.add_argument('movie_id', help='ID of movie to remove', action='append')
-
- library_show_parser = library_subcommands.add_parser('show')
- library_show_parser.set_defaults(func=_do_library_show)
-
- library_show_subcommands = library_show_parser.add_subparsers(title='Command', dest='command')
-
- library_show_add_parser = library_show_subcommands.add_parser('add')
- library_show_add_parser.add_argument('show_id', help='Show IDs to add')
- library_show_add_parser.add_argument('episode_id', nargs='*', help='Show IDs to add')
-
- library_show_rmv_parser = library_show_subcommands.add_parser('remove')
-
- # Demonstrates a custom completion function that does more with the command line than is
- # allowed by the standard completion functions
- def _filter_episodes(self, text, line, begidx, endidx, show_db, user_lib):
- tokens, _ = self.tokens_for_completion(line, begidx, endidx)
- show_id = tokens[3]
- if show_id:
- if show_id in show_db:
- show = show_db[show_id]
- all_episodes = itertools.chain(*(show['seasons'].values()))
-
- if show_id in user_lib:
- user_eps = user_lib[show_id]
- else:
- user_eps = []
-
- return self._filter_library(text, line, begidx, endidx, all_episodes, user_eps)
- return []
-
- @cmd2.with_category(CAT_AUTOCOMPLETE)
- @cmd2.with_argparser(library_parser)
- def do_library(self, args):
- """Media management command demonstrates multiple layers of sub-commands being handled by AutoCompleter"""
- func = getattr(args, 'func', None)
- if func is not None:
- # Call whatever subcommand function was selected
- func(self, args)
- else:
- # No subcommand was provided, so call help
- self.do_help('library')
-
- def complete_library(self, text, line, begidx, endidx):
-
- # this demonstrates the much more complicated scenario of having
- # unique completion parameters per sub-command that use the same
- # argument name. To do this we build a multi-layer nested tree
- # of lookups far AutoCompleter to traverse. This nested tree must
- # match the structure of the argparse parser
- #
-
- movie_add_choices = {'movie_id': self._query_movie_database}
- movie_remove_choices = {'movie_id': self._query_movie_user_library}
-
- # This demonstrates the ability to mix custom completion functions with argparse completion.
- # By specifying a tuple for a completer, AutoCompleter expects a custom completion function
- # with optional index-based as well as keyword based arguments. This is an alternative to using
- # a partial function.
-
- show_add_choices = {'show_id': (self._filter_library, # This is a custom completion function
- # This tuple represents index-based args to append to the function call
- (list(TabCompleteExample.SHOW_DATABASE.keys()),)
- ),
- 'episode_id': (self._filter_episodes, # this is a custom completion function
- # this list represents index-based args to append to the function call
- [TabCompleteExample.SHOW_DATABASE],
- # this dict contains keyword-based args to append to the function call
- {'user_lib': TabCompleteExample.USER_SHOW_LIBRARY})}
- show_remove_choices = {}
-
- # The library movie sub-parser group 'command' has 2 sub-parsers:
- # 'add' and 'remove'
- library_movie_command_params = \
- {'add': (movie_add_choices, None),
- 'remove': (movie_remove_choices, None)}
-
- library_show_command_params = \
- {'add': (show_add_choices, None),
- 'remove': (show_remove_choices, None)}
-
- # The 'library movie' command has a sub-parser group called 'command'
- library_movie_subcommand_groups = {'command': library_movie_command_params}
- library_show_subcommand_groups = {'command': library_show_command_params}
-
- # Mapping of a specific sub-parser of the 'type' group to a tuple. Each
- # tuple has 2 values corresponding what's passed to the constructor
- # parameters (arg_choices,subcmd_args_lookup) of the nested
- # instance of AutoCompleter
- library_type_params = {'movie': (None, library_movie_subcommand_groups),
- 'show': (None, library_show_subcommand_groups)}
-
- # maps the a subcommand group to a dictionary mapping a specific
- # sub-command to a tuple of (arg_choices, subcmd_args_lookup)
- #
- # In this example, 'library_parser' has a sub-parser group called 'type'
- # under the type sub-parser group, there are 2 sub-parsers: 'movie', 'show'
- library_subcommand_groups = {'type': library_type_params}
-
- completer = argparse_completer.AutoCompleter(TabCompleteExample.library_parser,
- self,
- subcmd_args_lookup=library_subcommand_groups)
-
- tokens, _ = self.tokens_for_completion(line, begidx, endidx)
- results = completer.complete_command(tokens, text, line, begidx, endidx)
-
- return results
-
if __name__ == '__main__':
import sys