diff options
author | Eric Lin <anselor@gmail.com> | 2018-05-02 15:22:14 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-05-02 15:22:14 -0400 |
commit | a55f0b6ed559d03f2d8b596898d638b288c11a68 (patch) | |
tree | 54d84c8d1d7c14247ac3a22186ed106190a8b494 /examples | |
parent | dbf4846e8bc0e6ca38c928d8fe4752f9b6173803 (diff) | |
parent | a95c8a065abeac286c196783393ecc49e4356f54 (diff) | |
download | cmd2-git-a55f0b6ed559d03f2d8b596898d638b288c11a68.tar.gz |
Merge branch 'test_merge' into test_ply_merge
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 | 76 | ||||
-rwxr-xr-x | examples/submenus.py | 2 | ||||
-rwxr-xr-x | examples/tab_autocompletion.py | 8 | ||||
-rwxr-xr-x | examples/tab_completion.py | 4 | ||||
-rwxr-xr-x | examples/table_display.py | 2 |
17 files changed, 71 insertions, 56 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 95e8ff01..b2f0fcda 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 3d436323..6e5dcf35 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 35e2c49f..f07b9c74 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 bcf9a368..f6f4c629 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 75c0733e..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,52 @@ 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): """ Example cmd2 application where we a base command which has a couple subcommands @@ -35,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 a1a8daee..f3302533 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # coding=utf-8 """ A example usage of the AutoCompleter @@ -10,14 +10,15 @@ 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', 'Lupita Nyong\'o', 'Andy Serkis', 'Liam Neeson', 'Ewan McGregor', 'Natalie Portman', 'Jake Lloyd', 'Hayden Christensen', 'Christopher Lee'] + def query_actors() -> List[str]: """Simulating a function that queries and returns a completion values""" return actors @@ -324,6 +325,7 @@ 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 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 |