summaryrefslogtreecommitdiff
path: root/examples/tab_autocompletion.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-06-19 10:06:20 -0600
committerkotfu <kotfu@kotfu.net>2018-06-19 10:06:20 -0600
commit101395a437ef66846e207c039a12ee946128fab9 (patch)
tree4e024a7899396f4e111bda10f2cd8c053db12a5b /examples/tab_autocompletion.py
parentb0a0251c77e73a3f3c0a755f3fabb9fdf136ccfa (diff)
parentb5def934f4d368a7e1a1fe67a98b3cdcc14cd2d9 (diff)
downloadcmd2-git-101395a437ef66846e207c039a12ee946128fab9.tar.gz
Merge branch 'master' into plugin_functions
Diffstat (limited to 'examples/tab_autocompletion.py')
-rwxr-xr-xexamples/tab_autocompletion.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py
index d1726841..342cfff5 100755
--- a/examples/tab_autocompletion.py
+++ b/examples/tab_autocompletion.py
@@ -109,6 +109,15 @@ class TabCompleteExample(cmd2.Cmd):
"""Simulating a function that queries and returns a completion values"""
return actors
+ def instance_query_movie_ids(self) -> List[str]:
+ """Demonstrates showing tabular hinting of tab completion information"""
+ completions_with_desc = []
+
+ for movie_id, movie_entry in self.MOVIE_DATABASE.items():
+ completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title']))
+
+ return completions_with_desc
+
# This demonstrates a number of customizations of the AutoCompleter version of ArgumentParser
# - The help output will separately group required vs optional flags
# - The help output for arguments with multiple flags or with append=True is more concise
@@ -253,6 +262,9 @@ class TabCompleteExample(cmd2.Cmd):
('path_complete', [False, False]))
vid_movies_delete_parser = vid_movies_commands_subparsers.add_parser('delete')
+ vid_delete_movie_id = vid_movies_delete_parser.add_argument('movie_id', help='Movie ID')
+ setattr(vid_delete_movie_id, argparse_completer.ACTION_ARG_CHOICES, instance_query_movie_ids)
+ setattr(vid_delete_movie_id, argparse_completer.ACTION_DESCRIPTIVE_COMPLETION_HEADER, 'Title')
vid_shows_parser = video_types_subparsers.add_parser('shows')
vid_shows_parser.set_defaults(func=_do_vid_media_shows)
@@ -328,6 +340,9 @@ class TabCompleteExample(cmd2.Cmd):
movies_add_parser.add_argument('actor', help='Actors', nargs='*')
movies_delete_parser = movies_commands_subparsers.add_parser('delete')
+ movies_delete_movie_id = movies_delete_parser.add_argument('movie_id', help='Movie ID')
+ setattr(movies_delete_movie_id, argparse_completer.ACTION_ARG_CHOICES, 'instance_query_movie_ids')
+ setattr(movies_delete_movie_id, argparse_completer.ACTION_DESCRIPTIVE_COMPLETION_HEADER, 'Title')
movies_load_parser = movies_commands_subparsers.add_parser('load')
movie_file_action = movies_load_parser.add_argument('movie_file', help='Movie database')
@@ -362,7 +377,7 @@ class TabCompleteExample(cmd2.Cmd):
'director': TabCompleteExample.static_list_directors, # static list
'movie_file': (self.path_complete, [False, False])
}
- completer = argparse_completer.AutoCompleter(TabCompleteExample.media_parser, arg_choices=choices)
+ completer = argparse_completer.AutoCompleter(TabCompleteExample.media_parser, arg_choices=choices, cmd2_app=self)
tokens, _ = self.tokens_for_completion(line, begidx, endidx)
results = completer.complete_command(tokens, text, line, begidx, endidx)