diff options
| author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-03-02 16:58:01 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-02 16:58:01 -0500 |
| commit | 17781f27c49b961526c7e3a5302482744e6a038b (patch) | |
| tree | cad46f7f9746ecd63c7ee5eb6e4d8eabbb44ba0e /examples | |
| parent | 9aeb231a201e070c4897394ff79d96f21753d9b8 (diff) | |
| parent | 0ec3c1d40962563cc5f5863e0d824343f43da13d (diff) | |
| download | cmd2-git-17781f27c49b961526c7e3a5302482744e6a038b.tar.gz | |
Merge pull request #291 from python-cmd2/tab_completion
Tab completion
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/python_scripting.py | 22 | ||||
| -rwxr-xr-x | examples/subcommands.py | 2 | ||||
| -rwxr-xr-x | examples/tab_completion.py | 75 |
3 files changed, 87 insertions, 12 deletions
diff --git a/examples/python_scripting.py b/examples/python_scripting.py index aa62007a..f4606251 100755 --- a/examples/python_scripting.py +++ b/examples/python_scripting.py @@ -18,15 +18,15 @@ import argparse import functools import os -from cmd2 import Cmd, CmdResult, with_argument_list, with_argparser_and_unknown_args +import cmd2 -class CmdLineApp(Cmd): +class CmdLineApp(cmd2.Cmd): """ Example cmd2 application to showcase conditional control flow in Python scripting within cmd2 aps. """ def __init__(self): # Enable the optional ipy command if IPython is installed by setting use_ipython=True - Cmd.__init__(self, use_ipython=True) + cmd2.Cmd.__init__(self, use_ipython=True) self._set_prompt() self.intro = 'Happy 𝛑 Day. Note the full Unicode support: 😇 (Python 3 only) 💩' @@ -46,7 +46,7 @@ class CmdLineApp(Cmd): self._set_prompt() return stop - @with_argument_list + @cmd2.with_argument_list def do_cd(self, arglist): """Change directory. Usage: @@ -56,7 +56,7 @@ class CmdLineApp(Cmd): if not arglist or len(arglist) != 1: self.perror("cd requires exactly 1 argument:", traceback_war=False) self.do_help('cd') - self._last_result = CmdResult('', 'Bad arguments') + self._last_result = cmd2.CmdResult('', 'Bad arguments') return # Convert relative paths to absolute paths @@ -80,22 +80,22 @@ class CmdLineApp(Cmd): if err: self.perror(err, traceback_war=False) - self._last_result = CmdResult(out, err) + self._last_result = cmd2.CmdResult(out, err) - # Enable directory completion for cd command by freezing an argument to path_complete() with functools.partialmethod - complete_cd = functools.partialmethod(Cmd.path_complete, dir_only=True) + # Enable directory completion for cd command by freezing an argument to path_complete() with functools.partial + complete_cd = functools.partial(cmd2.path_complete, dir_only=True) dir_parser = argparse.ArgumentParser() dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line") - @with_argparser_and_unknown_args(dir_parser) + @cmd2.with_argparser_and_unknown_args(dir_parser) def do_dir(self, args, unknown): """List contents of current directory.""" # No arguments for this command if unknown: self.perror("dir does not take any positional arguments:", traceback_war=False) self.do_help('dir') - self._last_result = CmdResult('', 'Bad arguments') + self._last_result = cmd2.CmdResult('', 'Bad arguments') return # Get the contents as a list @@ -108,7 +108,7 @@ class CmdLineApp(Cmd): self.stdout.write(fmt.format(f)) self.stdout.write('\n') - self._last_result = CmdResult(contents) + self._last_result = cmd2.CmdResult(contents) if __name__ == '__main__': diff --git a/examples/subcommands.py b/examples/subcommands.py index e77abc61..a278fd8b 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -24,7 +24,7 @@ class SubcommandsExample(cmd2.Cmd): self.poutput(args.x * args.y) def base_bar(self, args): - """bar sucommand of base command""" + """bar subcommand of base command""" self.poutput('((%s))' % args.z) # create the top-level parser for the base command diff --git a/examples/tab_completion.py b/examples/tab_completion.py new file mode 100755 index 00000000..6c16e63b --- /dev/null +++ b/examples/tab_completion.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# coding=utf-8 +"""A simple example demonstrating how to use flag and index based tab-completion functions +""" +import argparse +import functools + +import cmd2 +from cmd2 import with_argparser, with_argument_list, flag_based_complete, index_based_complete, path_complete + +# List of strings used with flag and index based completion functions +food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato'] +sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football'] + +# Dictionary used with flag based completion functions +flag_dict = \ + { + '-f': food_item_strs, # Tab-complete food items after -f flag in command line + '--food': food_item_strs, # Tab-complete food items after --food flag in command line + '-s': sport_item_strs, # Tab-complete sport items after -s flag in command line + '--sport': sport_item_strs, # Tab-complete sport items after --sport flag in command line + '-o': path_complete, # Tab-complete using path_complete function after -o flag in command line + '--other': path_complete, # Tab-complete using path_complete function after --other flag in command line + } + +# Dictionary used with index based completion functions +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: path_complete, # Tab-complete using path_complete function at index 3 in command line + } + + +class TabCompleteExample(cmd2.Cmd): + """ Example cmd2 application where we a base command which has a couple subcommands.""" + + def __init__(self): + cmd2.Cmd.__init__(self) + + 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') + + @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 + complete_add_item = functools.partial(flag_based_complete, flag_dict=flag_dict) + + @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 + complete_list_item = functools.partial(index_based_complete, index_dict=index_dict) + + +if __name__ == '__main__': + app = TabCompleteExample() + app.cmdloop() |
