diff options
| author | Eric Lin <anselor@gmail.com> | 2018-05-02 14:53:20 -0400 |
|---|---|---|
| committer | Eric Lin <anselor@gmail.com> | 2018-05-02 14:53:20 -0400 |
| commit | a95c8a065abeac286c196783393ecc49e4356f54 (patch) | |
| tree | b4575eda772450b54620d16c09d4d29c52abdec7 /examples | |
| parent | d2d3c2f37d5324b5bc967fe268a92f9d563a3a30 (diff) | |
| parent | 148f05d0af66f38c8ba6ca3a5619f50a8bb1b8a9 (diff) | |
| download | cmd2-git-a95c8a065abeac286c196783393ecc49e4356f54.tar.gz | |
Merge branch 'bash_completion' into bash_to_pyscript
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/alias_startup.py | 6 | ||||
| -rwxr-xr-x | examples/arg_print.py | 6 | ||||
| -rwxr-xr-x | examples/argparse_example.py | 2 | ||||
| -rwxr-xr-x | examples/environment.py | 2 | ||||
| -rwxr-xr-x | examples/event_loops.py | 2 | ||||
| -rwxr-xr-x | examples/example.py | 2 | ||||
| -rwxr-xr-x | examples/help_categories.py | 3 | ||||
| -rwxr-xr-x | examples/paged_output.py | 4 | ||||
| -rwxr-xr-x | examples/persistent_history.py | 2 | ||||
| -rwxr-xr-x | examples/pirate.py | 2 | ||||
| -rwxr-xr-x | examples/python_scripting.py | 2 | ||||
| -rwxr-xr-x | examples/remove_unused.py | 2 | ||||
| -rwxr-xr-x | examples/subcommands.py | 75 | ||||
| -rwxr-xr-x | examples/submenus.py | 2 | ||||
| -rwxr-xr-x | examples/tab_autocompletion.py | 4 | ||||
| -rwxr-xr-x | examples/tab_completion.py | 4 | ||||
| -rwxr-xr-x | examples/table_display.py | 2 |
17 files changed, 67 insertions, 55 deletions
diff --git a/examples/alias_startup.py b/examples/alias_startup.py index 30764c27..7ccfa6e5 100755 --- a/examples/alias_startup.py +++ b/examples/alias_startup.py @@ -4,12 +4,8 @@ 1) How to add custom command aliases using the alias command 2) How to load an initialization script at startup """ -import argparse -import cmd2 -import pyparsing - -from cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args +from cmd2 import cmd2 class AliasAndStartup(cmd2.Cmd): diff --git a/examples/arg_print.py b/examples/arg_print.py index 18fa483f..90df053a 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -11,10 +11,8 @@ It also serves as an example of how to create command aliases (shortcuts). """ import argparse -import cmd2 -import pyparsing - -from cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args +from cmd2 import cmd2 +from cmd2.cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args class ArgumentAndOptionPrinter(cmd2.Cmd): diff --git a/examples/argparse_example.py b/examples/argparse_example.py index e9b377ba..e8afef5c 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -14,7 +14,7 @@ verifying that the output produced matches the transcript. import argparse import sys -from cmd2 import Cmd, with_argparser, with_argument_list +from cmd2.cmd2 import Cmd, with_argparser, with_argument_list class CmdLineApp(Cmd): diff --git a/examples/environment.py b/examples/environment.py index c245f55d..af452e4e 100755 --- a/examples/environment.py +++ b/examples/environment.py @@ -4,7 +4,7 @@ A sample application for cmd2 demonstrating customized environment parameters """ -from cmd2 import Cmd +from cmd2.cmd2 import Cmd class EnvironmentApp(Cmd): diff --git a/examples/event_loops.py b/examples/event_loops.py index 53d3ca2b..a76c5d91 100755 --- a/examples/event_loops.py +++ b/examples/event_loops.py @@ -6,7 +6,7 @@ This is an example of how to use cmd2 in a way so that cmd2 doesn't own the inne This opens up the possibility of registering cmd2 input with event loops, like asyncio, without occupying the main loop. """ -import cmd2 +from cmd2 import cmd2 class Cmd2EventBased(cmd2.Cmd): diff --git a/examples/example.py b/examples/example.py index 612d81e5..1fc6bf6d 100755 --- a/examples/example.py +++ b/examples/example.py @@ -14,7 +14,7 @@ the transcript. import random import argparse -from cmd2 import Cmd, with_argparser +from cmd2.cmd2 import Cmd, with_argparser class CmdLineApp(Cmd): diff --git a/examples/help_categories.py b/examples/help_categories.py index cfb5f253..dcfbd31f 100755 --- a/examples/help_categories.py +++ b/examples/help_categories.py @@ -4,9 +4,10 @@ A sample application for tagging categories on commands. """ -from cmd2 import Cmd, categorize, __version__, with_argparser, with_category import argparse +from cmd2.cmd2 import Cmd, categorize, __version__, with_argparser, with_category + class HelpCategories(Cmd): """ Example cmd2 application. """ diff --git a/examples/paged_output.py b/examples/paged_output.py index bb410af6..9396f04e 100755 --- a/examples/paged_output.py +++ b/examples/paged_output.py @@ -3,8 +3,8 @@ """A simple example demonstrating the using paged output via the ppaged() method. """ -import cmd2 -from cmd2 import with_argument_list +from cmd2 import cmd2 +from cmd2.cmd2 import with_argument_list class PagedOutput(cmd2.Cmd): diff --git a/examples/persistent_history.py b/examples/persistent_history.py index 61e26b9c..251dbd67 100755 --- a/examples/persistent_history.py +++ b/examples/persistent_history.py @@ -5,7 +5,7 @@ This will allow end users of your cmd2-based application to use the arrow keys and Ctrl+r in a manner which persists across invocations of your cmd2 application. This can make it much easier for them to use your application. """ -import cmd2 +from cmd2 import cmd2 class Cmd2PersistentHistory(cmd2.Cmd): diff --git a/examples/pirate.py b/examples/pirate.py index 7fe3884b..2daa8631 100755 --- a/examples/pirate.py +++ b/examples/pirate.py @@ -7,7 +7,7 @@ presented as part of her PyCon 2010 talk. It demonstrates many features of cmd2. """ import argparse -from cmd2 import Cmd, with_argparser +from cmd2.cmd2 import Cmd, with_argparser class Pirate(Cmd): diff --git a/examples/python_scripting.py b/examples/python_scripting.py index 7e2cf345..865cf052 100755 --- a/examples/python_scripting.py +++ b/examples/python_scripting.py @@ -17,7 +17,7 @@ This application and the "scripts/conditional.py" script serve as an example for import argparse import os -import cmd2 +from cmd2 import cmd2 class CmdLineApp(cmd2.Cmd): diff --git a/examples/remove_unused.py b/examples/remove_unused.py index 8a567123..dfe0a055 100755 --- a/examples/remove_unused.py +++ b/examples/remove_unused.py @@ -9,7 +9,7 @@ name, they just won't clutter the help menu. Commands can also be removed entirely by using Python's "del". """ -import cmd2 +from cmd2 import cmd2 class RemoveUnusedBuiltinCommands(cmd2.Cmd): diff --git a/examples/subcommands.py b/examples/subcommands.py index eda88072..9bf6c666 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -1,5 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # coding=utf-8 +# PYTHON_ARGCOMPLETE_OK """A simple example demonstrating how to use Argparse to support subcommands. @@ -8,11 +9,51 @@ and provides separate contextual help. """ import argparse -import cmd2 -from cmd2 import with_argparser - sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] +# create the top-level parser for the base command +base_parser = argparse.ArgumentParser(prog='base') +base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') + +# create the parser for the "foo" subcommand +parser_foo = base_subparsers.add_parser('foo', help='foo help') +parser_foo.add_argument('-x', type=int, default=1, help='integer') +parser_foo.add_argument('y', type=float, help='float') + +# create the parser for the "bar" subcommand +parser_bar = base_subparsers.add_parser('bar', help='bar help') + +bar_subparsers = parser_bar.add_subparsers(title='layer3', help='help for 3rd layer of commands') +parser_bar.add_argument('z', help='string') + +bar_subparsers.add_parser('apple', help='apple help') +bar_subparsers.add_parser('artichoke', help='artichoke help') +bar_subparsers.add_parser('cranberries', help='cranberries help') + +# create the parser for the "sport" subcommand +parser_sport = base_subparsers.add_parser('sport', help='sport help') +sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') +setattr(sport_arg, 'arg_choices', sport_item_strs) + +# Handle bash completion if it's installed +try: + # only move forward if we can import CompletionFinder and AutoCompleter + from cmd2.argcomplete_bridge import CompletionFinder + from cmd2.argparse_completer import AutoCompleter + if __name__ == '__main__': + with open('out.txt', 'a') as f: + f.write('Here 1') + f.flush() + completer = CompletionFinder() + completer(base_parser, AutoCompleter(base_parser)) +except ImportError: + pass + + +# Intentionally below the bash completion code to reduce tab completion lag +from cmd2 import cmd2 +from cmd2.cmd2 import with_argparser + class SubcommandsExample(cmd2.Cmd): """ @@ -36,33 +77,9 @@ class SubcommandsExample(cmd2.Cmd): """sport subcommand of base command""" self.poutput('Sport is {}'.format(args.sport)) - # create the top-level parser for the base command - base_parser = argparse.ArgumentParser(prog='base') - base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') - - # create the parser for the "foo" subcommand - parser_foo = base_subparsers.add_parser('foo', help='foo help') - parser_foo.add_argument('-x', type=int, default=1, help='integer') - parser_foo.add_argument('y', type=float, help='float') + # Set handler functions for the subcommands parser_foo.set_defaults(func=base_foo) - - # create the parser for the "bar" subcommand - parser_bar = base_subparsers.add_parser('bar', help='bar help') parser_bar.set_defaults(func=base_bar) - - bar_subparsers = parser_bar.add_subparsers(title='layer3', help='help for 3rd layer of commands') - parser_bar.add_argument('z', help='string') - - bar_subparsers.add_parser('apple', help='apple help') - bar_subparsers.add_parser('artichoke', help='artichoke help') - bar_subparsers.add_parser('cranberries', help='cranberries help') - - # create the parser for the "sport" subcommand - parser_sport = base_subparsers.add_parser('sport', help='sport help') - sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') - setattr(sport_arg, 'arg_choices', sport_item_strs) - - # Set both a function and tab completer for the "sport" subcommand parser_sport.set_defaults(func=base_sport) @with_argparser(base_parser) diff --git a/examples/submenus.py b/examples/submenus.py index 44b17f33..27c8cb10 100755 --- a/examples/submenus.py +++ b/examples/submenus.py @@ -11,7 +11,7 @@ of the submenu. Nesting of the submenus is done with the cmd2.AddSubmenu() decor from __future__ import print_function import sys -import cmd2 +from cmd2 import cmd2 from IPython import embed diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index 6146b64b..f3302533 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -10,8 +10,8 @@ import argparse import itertools from typing import List -import cmd2 -from cmd2 import with_argparser, with_category, argparse_completer +from cmd2 import cmd2, argparse_completer +from cmd2.cmd2 import with_argparser, with_category actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac', diff --git a/examples/tab_completion.py b/examples/tab_completion.py index 919e9560..30fa283d 100755 --- a/examples/tab_completion.py +++ b/examples/tab_completion.py @@ -4,8 +4,8 @@ """ import argparse -import cmd2 -from cmd2 import with_argparser, with_argument_list +from cmd2 import cmd2 +from cmd2.cmd2 import with_argparser, with_argument_list # List of strings used with flag and index based completion functions food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato'] diff --git a/examples/table_display.py b/examples/table_display.py index 2e6ea804..5d168408 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -12,7 +12,7 @@ WARNING: This example requires the tabulate module. """ import functools -import cmd2 +from cmd2 import cmd2 import tabulate # Format to use with tabulate module when displaying tables |
