def complete_select(self, text: str, line: str, begin_index: int, end_index: int) -> List[str]: """ Handles the auto completion for this command """ choices = dict(id=Client.unique_client_ids()) completer = argparse_completer.AutoCompleter(SelectPlugin.select_parser, arg_choices=choices) tokens, _ = self.tokens_for_completion(line, begin_index, end_index) return completer.complete_command(tokens, text, line, begin_index, end_index)
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, [False, False]) } completer = argparse_completer.AutoCompleter(TabCompleteExample.media_parser, arg_choices=choices) tokens, _ = self.tokens_for_completion(line, begidx, endidx) results = completer.complete_command(tokens, text, line, begidx, endidx) return results
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