Skip to content

Commit 6780baa

Browse files
committed
Standardize cmd2 imports in tests and examples
1 parent 1a70b90 commit 6780baa

21 files changed

+63
-72
lines changed

examples/alias_startup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
2) How to load an initialization script at startup
66
"""
77

8-
from cmd2 import cmd2
9-
8+
import cmd2
109

1110
class AliasAndStartup(cmd2.Cmd):
1211
""" Example cmd2 application where we create commands that just print the arguments they are called with."""

examples/arg_print.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
"""
1212
import argparse
1313

14-
from cmd2 import cmd2
15-
from cmd2.cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args
16-
14+
import cmd2
1715

1816
class ArgumentAndOptionPrinter(cmd2.Cmd):
1917
""" Example cmd2 application where we create commands that just print the arguments they are called with."""
@@ -31,7 +29,7 @@ def do_aprint(self, arg):
3129
"""Print the argument string this basic command is called with."""
3230
self.poutput('aprint was called with argument: {!r}'.format(arg))
3331

34-
@with_argument_list
32+
@cmd2.with_argument_list
3533
def do_lprint(self, arglist):
3634
"""Print the argument list this basic command is called with."""
3735
self.poutput('lprint was called with the following list of arguments: {!r}'.format(arglist))
@@ -42,7 +40,7 @@ def do_lprint(self, arglist):
4240
oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
4341
oprint_parser.add_argument('words', nargs='+', help='words to print')
4442

45-
@with_argparser(oprint_parser)
43+
@cmd2.with_argparser(oprint_parser)
4644
def do_oprint(self, args):
4745
"""Print the options and argument list this options command was called with."""
4846
self.poutput('oprint was called with the following\n\toptions: {!r}'.format(args))
@@ -51,13 +49,12 @@ def do_oprint(self, args):
5149
pprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
5250
pprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
5351
pprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
54-
@with_argparser_and_unknown_args(pprint_parser)
52+
@cmd2.with_argparser_and_unknown_args(pprint_parser)
5553
def do_pprint(self, args, unknown):
5654
"""Print the options and argument list this options command was called with."""
5755
self.poutput('oprint was called with the following\n\toptions: {!r}\n\targuments: {}'.format(args, unknown))
5856

5957

60-
6158
if __name__ == '__main__':
6259
app = ArgumentAndOptionPrinter()
6360
app.cmdloop()

examples/argparse_example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import argparse
1515
import sys
1616

17-
from cmd2.cmd2 import Cmd, with_argparser, with_argument_list
17+
import cmd2
1818

1919

20-
class CmdLineApp(Cmd):
20+
class CmdLineApp(cmd2.Cmd):
2121
""" Example cmd2 application. """
2222
def __init__(self, ip_addr=None, port=None, transcript_files=None):
2323
self.multiline_commands = ['orate']
@@ -46,7 +46,7 @@ def __init__(self, ip_addr=None, port=None, transcript_files=None):
4646
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
4747
speak_parser.add_argument('words', nargs='+', help='words to say')
4848

49-
@with_argparser(speak_parser)
49+
@cmd2.with_argparser(speak_parser)
5050
def do_speak(self, args):
5151
"""Repeats what you tell me to."""
5252
words = []
@@ -67,13 +67,13 @@ def do_speak(self, args):
6767
tag_parser.add_argument('tag', help='tag')
6868
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
6969

70-
@with_argparser(tag_parser)
70+
@cmd2.with_argparser(tag_parser)
7171
def do_tag(self, args):
7272
"""create a html tag"""
7373
self.poutput('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
7474

7575

76-
@with_argument_list
76+
@cmd2.with_argument_list
7777
def do_tagg(self, arglist):
7878
"""verion of creating an html tag using arglist instead of argparser"""
7979
if len(arglist) >= 2:

examples/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
A sample application for cmd2 demonstrating customized environment parameters
55
"""
66

7-
from cmd2.cmd2 import Cmd
7+
import cmd2
88

99

10-
class EnvironmentApp(Cmd):
10+
class EnvironmentApp(cmd2.Cmd):
1111
""" Example cmd2 application. """
1212

1313
degrees_c = 22

examples/event_loops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
This opens up the possibility of registering cmd2 input with event loops, like asyncio, without occupying the main loop.
88
"""
9-
from cmd2 import cmd2
9+
import cmd2
1010

1111

1212
class Cmd2EventBased(cmd2.Cmd):

examples/example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import random
1515
import argparse
1616

17-
from cmd2.cmd2 import Cmd, with_argparser
17+
import cmd2
1818

1919

20-
class CmdLineApp(Cmd):
20+
class CmdLineApp(cmd2.Cmd):
2121
""" Example cmd2 application. """
2222

2323
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
@@ -43,7 +43,7 @@ def __init__(self):
4343
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
4444
speak_parser.add_argument('words', nargs='+', help='words to say')
4545

46-
@with_argparser(speak_parser)
46+
@cmd2.with_argparser(speak_parser)
4747
def do_speak(self, args):
4848
"""Repeats what you tell me to."""
4949
words = []
@@ -65,7 +65,7 @@ def do_speak(self, args):
6565
mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
6666
mumble_parser.add_argument('words', nargs='+', help='words to say')
6767

68-
@with_argparser(mumble_parser)
68+
@cmd2.with_argparser(mumble_parser)
6969
def do_mumble(self, args):
7070
"""Mumbles what you tell me to."""
7171
repetitions = args.repeat or 1

examples/help_categories.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
import argparse
88

9-
from cmd2.cmd2 import Cmd, categorize, __version__, with_argparser, with_category
9+
import cmd2
1010

11-
12-
class HelpCategories(Cmd):
11+
class HelpCategories(cmd2.Cmd):
1312
""" Example cmd2 application. """
1413

1514
# Command categories
@@ -28,7 +27,7 @@ def do_connect(self, _):
2827
# Tag the above command functions under the category Connecting
2928
categorize(do_connect, CMD_CAT_CONNECTING)
3029

31-
@with_category(CMD_CAT_CONNECTING)
30+
@cmd2.with_category(CMD_CAT_CONNECTING)
3231
def do_which(self, _):
3332
"""Which command"""
3433
self.poutput('Which')
@@ -58,8 +57,8 @@ def do_redeploy(self, _):
5857
choices=['now', 'later', 'sometime', 'whenever'],
5958
help='Specify when to restart')
6059

61-
@with_argparser(restart_parser)
62-
@with_category(CMD_CAT_APP_MGMT)
60+
@cmd2.with_argparser(restart_parser)
61+
@cmd2.with_category(CMD_CAT_APP_MGMT)
6362
def do_restart(self, _):
6463
"""Restart command"""
6564
self.poutput('Restart')
@@ -123,12 +122,12 @@ def do_vminfo(self, _):
123122
self.poutput('VM Info')
124123

125124
# Tag the above command functions under the category Server Information
126-
categorize(do_resources, CMD_CAT_SERVER_INFO)
127-
categorize(do_status, CMD_CAT_SERVER_INFO)
128-
categorize(do_serverinfo, CMD_CAT_SERVER_INFO)
129-
categorize(do_thread_dump, CMD_CAT_SERVER_INFO)
130-
categorize(do_sslconnectorciphers, CMD_CAT_SERVER_INFO)
131-
categorize(do_vminfo, CMD_CAT_SERVER_INFO)
125+
cmd2.categorize(do_resources, CMD_CAT_SERVER_INFO)
126+
cmd2.categorize(do_status, CMD_CAT_SERVER_INFO)
127+
cmd2.categorize(do_serverinfo, CMD_CAT_SERVER_INFO)
128+
cmd2.categorize(do_thread_dump, CMD_CAT_SERVER_INFO)
129+
cmd2.categorize(do_sslconnectorciphers, CMD_CAT_SERVER_INFO)
130+
cmd2.categorize(do_vminfo, CMD_CAT_SERVER_INFO)
132131

133132
# The following command functions don't have the HELP_CATEGORY attribute set
134133
# and show up in the 'Other' group

examples/paged_output.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"""A simple example demonstrating the using paged output via the ppaged() method.
44
"""
55

6-
from cmd2 import cmd2
7-
from cmd2.cmd2 import with_argument_list
6+
import cmd2
87

98

109
class PagedOutput(cmd2.Cmd):
@@ -13,7 +12,7 @@ class PagedOutput(cmd2.Cmd):
1312
def __init__(self):
1413
super().__init__()
1514

16-
@with_argument_list
15+
@cmd2.with_argument_list
1716
def do_page_file(self, args):
1817
"""Read in a text file and display its output in a pager."""
1918
if not args:

examples/persistent_history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
This will allow end users of your cmd2-based application to use the arrow keys and Ctrl+r in a manner which persists
66
across invocations of your cmd2 application. This can make it much easier for them to use your application.
77
"""
8-
from cmd2 import cmd2
8+
import cmd2
99

1010

1111
class Cmd2PersistentHistory(cmd2.Cmd):

examples/pirate.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
It demonstrates many features of cmd2.
88
"""
99
import argparse
10-
from cmd2.cmd2 import Cmd, with_argparser
1110

11+
import cmd2
1212

13-
class Pirate(Cmd):
13+
14+
class Pirate(cmd2.Cmd):
1415
"""A piratical example cmd2 application involving looting and drinking."""
1516
def __init__(self):
1617
self.default_to_shell = True
@@ -74,7 +75,7 @@ def do_sing(self, arg):
7475
yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas')
7576
yo_parser.add_argument('beverage', help='beverage to drink with the chant')
7677

77-
@with_argparser(yo_parser)
78+
@cmd.with_argparser(yo_parser)
7879
def do_yo(self, args):
7980
"""Compose a yo-ho-ho type chant with flexible options."""
8081
chant = ['yo'] + ['ho'] * args.ho

0 commit comments

Comments
 (0)