diff options
author | Eric Lin <anselor@gmail.com> | 2018-04-15 17:36:49 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-04-15 17:36:49 -0400 |
commit | 8d4f8411bb5f6bbac8ec121f897ccece1b97006c (patch) | |
tree | cd31383d344fc8f21783375a96bd53f522bebed8 /examples/tab_autocompletion.py | |
parent | 874f8842fe685f63d344aa2c58c470f6c3603c3e (diff) | |
download | cmd2-git-8d4f8411bb5f6bbac8ec121f897ccece1b97006c.tar.gz |
* AutoCompleter
- Fixed a few bugs in AutoCompleter dealing with nargs='+' or nargs='*'
- Adjusted some help output dealing with narg ranges
- Fixed spacing problem with printing argument help
* examples/tab_autocompletion.py
- Removed debug code.
- Minor changes.
Diffstat (limited to 'examples/tab_autocompletion.py')
-rwxr-xr-x | examples/tab_autocompletion.py | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index a28f839d..7b978fed 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -34,18 +34,21 @@ class TabCompleteExample(cmd2.Cmd): @with_argparser(suggest_parser) def do_suggest(self, args): + """Suggest command demonstrates argparse customizations + + See hybrid_suggest and orig_suggest to compare the help output. + + + """ if not args.type: self.do_help('suggest') def complete_suggest(self, text, line, begidx, endidx): """ Adds tab completion to media""" - print('1') completer = AutoCompleter.AutoCompleter(TabCompleteExample.suggest_parser, 1) - print('2') + tokens, _ = self.tokens_for_completion(line, begidx, endidx) - print('22') results = completer.complete_command(tokens, text, line, begidx, endidx) - print('3') return results @@ -62,7 +65,7 @@ class TabCompleteExample(cmd2.Cmd): '\tsingle value - maximum duration\n' '\t[a, b] - duration range') @with_argparser(suggest_parser_hybrid) - def do_orig_suggest(self, args): + def do_hybrid_suggest(self, args): if not args.type: self.do_help('orig_suggest') @@ -75,6 +78,10 @@ class TabCompleteExample(cmd2.Cmd): return results + # This variant demonstrates the AutoCompleter working with the orginial argparse. + # Base argparse is unable to specify narg ranges. Autocompleter will keep expecting additional arguments + # for the -d/--duration flag until you specify a new flaw or end the list it with '--' + suggest_parser_orig = argparse.ArgumentParser() suggest_parser_orig.add_argument('-t', '--type', choices=['movie', 'show'], required=True) @@ -147,7 +154,7 @@ class TabCompleteExample(cmd2.Cmd): @with_argparser(media_parser) def do_media(self, args): - """Media management""" + """Media management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" func = getattr(args, 'func', None) if func is not None: # Call whatever subcommand function was selected @@ -156,12 +163,16 @@ class TabCompleteExample(cmd2.Cmd): # 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""" - directors = ['J. J. Abrams', 'Irvin Kershner', 'George Lucas', 'Richard Marquand', + static_list_directors = ['J. J. Abrams', 'Irvin Kershner', 'George Lucas', 'Richard Marquand', 'Rian Johnson', 'Gareth Edwards'] choices = {'actor': self.query_actors, - 'director': directors} + 'director': static_list_directors} completer = AutoCompleter.AutoCompleter(TabCompleteExample.media_parser, arg_choices=choices) tokens, _ = self.tokens_for_completion(line, begidx, endidx) |