summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2018-04-25 11:40:33 -0400
committerEric Lin <anselor@gmail.com>2018-04-25 11:40:33 -0400
commite018bbd79b2ddc0cf2538dff84baa8b2bf460ccd (patch)
tree4c35a13a67d0c1902e9d9a95be38f8da2d2f8160 /examples
parent4193ef0344359d050bbfa469cbb898a52db97622 (diff)
downloadcmd2-git-e018bbd79b2ddc0cf2538dff84baa8b2bf460ccd.tar.gz
Removed the expensive imports from cmd2/__init__.py
Added some shared definitions to cmd2/__init__.py -> maybe there's a better place for these? Figured out how to trick bash into showing argument hints. It's a bit weird. Updated all of the tests and examples to import cmd2 resources from their new location without the automatic imports in cmd2/__init__.py For #369
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/alias_startup.py4
-rwxr-xr-xexamples/arg_print.py6
-rwxr-xr-xexamples/argparse_example.py2
-rwxr-xr-xexamples/environment.py2
-rwxr-xr-xexamples/event_loops.py2
-rwxr-xr-xexamples/example.py2
-rwxr-xr-xexamples/help_categories.py2
-rwxr-xr-xexamples/paged_output.py4
-rwxr-xr-xexamples/persistent_history.py2
-rwxr-xr-xexamples/pirate.py2
-rwxr-xr-xexamples/python_scripting.py2
-rwxr-xr-xexamples/remove_unused.py2
-rwxr-xr-xexamples/subcommands.py71
-rwxr-xr-xexamples/submenus.py2
-rwxr-xr-xexamples/tab_autocompletion.py4
-rwxr-xr-xexamples/tab_completion.py4
-rwxr-xr-xexamples/table_display.py2
17 files changed, 63 insertions, 52 deletions
diff --git a/examples/alias_startup.py b/examples/alias_startup.py
index 30764c27..a02972b6 100755
--- a/examples/alias_startup.py
+++ b/examples/alias_startup.py
@@ -6,10 +6,10 @@
"""
import argparse
-import cmd2
+from cmd2 import cmd2
import pyparsing
-from cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args
+from cmd2.cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args
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..7adedb51 100755
--- a/examples/help_categories.py
+++ b/examples/help_categories.py
@@ -4,7 +4,7 @@
A sample application for tagging categories on commands.
"""
-from cmd2 import Cmd, categorize, __version__, with_argparser, with_category
+from cmd2.cmd2 import Cmd, categorize, __version__, with_argparser, with_category
import argparse
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 75c0733e..01f5b218 100755
--- a/examples/subcommands.py
+++ b/examples/subcommands.py
@@ -1,18 +1,56 @@
-#!/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.
This example shows an easy way for a single command to have many subcommands, each of which takes different arguments
and provides separate contextual help.
"""
+from cmd2.argcomplete_bridge import CompletionFinder
+from cmd2.argparse_completer import AutoCompleter
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)
+
+# Set both a function and tab completer for the "sport" subcommand
+
+if __name__ == '__main__':
+ with open('out.txt', 'a') as f:
+ f.write('Here 1')
+ f.flush()
+ completer = CompletionFinder()
+ completer(base_parser, AutoCompleter(base_parser))
+
+
+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 +73,8 @@ 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')
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..02c48ca5 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