summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/argparse_example.py24
-rwxr-xr-xexamples/example.py29
2 files changed, 29 insertions, 24 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py
index 40ea372c..b203feef 100755
--- a/examples/argparse_example.py
+++ b/examples/argparse_example.py
@@ -14,7 +14,8 @@ verifying that the output produced matches the transcript.
import argparse
import sys
-from cmd2 import Cmd, make_option, options, with_argument_parser, with_argument_list
+from cmd2 import Cmd, options, with_argument_parser, with_argument_list
+from optparse import make_option
class CmdLineApp(Cmd):
@@ -40,13 +41,13 @@ class CmdLineApp(Cmd):
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
# self.default_to_shell = True
+ speak_parser = argparse.ArgumentParser()
+ speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
+ speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
+ speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
+ speak_parser.add_argument('words', nargs='+', help='words to say')
- argparser = argparse.ArgumentParser()
- argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
- argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
- argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
- argparser.add_argument('words', nargs='+', help='words to say')
- @with_argument_parser(argparser)
+ @with_argument_parser(speak_parser)
def do_speak(self, arglist, args=None):
"""Repeats what you tell me to."""
words = []
@@ -63,11 +64,11 @@ class CmdLineApp(Cmd):
do_say = do_speak # now "say" is a synonym for "speak"
do_orate = do_speak # another synonym, but this one takes multi-line input
+ tag_parser = argparse.ArgumentParser(description='create a html tag')
+ tag_parser.add_argument('tag', nargs=1, help='tag')
+ tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
- argparser = argparse.ArgumentParser(description='create a html tag')
- argparser.add_argument('tag', nargs=1, help='tag')
- argparser.add_argument('content', nargs='+', help='content to surround with tag')
- @with_argument_parser(argparser)
+ @with_argument_parser(tag_parser)
def do_tag(self, arglist, args=None):
"""create a html tag"""
self.poutput('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
@@ -107,6 +108,7 @@ class CmdLineApp(Cmd):
# self.stdout.write is better than "print", because Cmd can be
# initialized with a non-standard output destination
+
if __name__ == '__main__':
# You can do your custom Argparse parsing here to meet your application's needs
parser = argparse.ArgumentParser(description='Process the arguments however you like.')
diff --git a/examples/example.py b/examples/example.py
index d7d4e21c..7c78edc3 100755
--- a/examples/example.py
+++ b/examples/example.py
@@ -38,12 +38,13 @@ class CmdLineApp(Cmd):
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
Cmd.__init__(self, use_ipython=False)
- argparser = argparse.ArgumentParser()
- argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
- argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
- argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
- argparser.add_argument('words', nargs='+', help='words to say')
- @with_argument_parser(argparser)
+ speak_parser = argparse.ArgumentParser()
+ speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
+ speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
+ speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
+ speak_parser.add_argument('words', nargs='+', help='words to say')
+
+ @with_argument_parser(speak_parser)
def do_speak(self, cmdline, opts=None):
"""Repeats what you tell me to."""
words = []
@@ -61,25 +62,27 @@ class CmdLineApp(Cmd):
do_say = do_speak # now "say" is a synonym for "speak"
do_orate = do_speak # another synonym, but this one takes multi-line input
- argparser = argparse.ArgumentParser()
- argparser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
- argparser.add_argument('words', nargs='+', help='words to say')
- @with_argument_parser(argparser)
+ mumble_parser = argparse.ArgumentParser()
+ mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
+ mumble_parser.add_argument('words', nargs='+', help='words to say')
+
+ @with_argument_parser(mumble_parser)
def do_mumble(self, cmdline, args=None):
"""Mumbles what you tell me to."""
repetitions = args.repeat or 1
for i in range(min(repetitions, self.maxrepeats)):
output = []
- if (random.random() < .33):
+ if random.random() < .33:
output.append(random.choice(self.MUMBLE_FIRST))
for word in args.words:
- if (random.random() < .40):
+ if random.random() < .40:
output.append(random.choice(self.MUMBLES))
output.append(word)
- if (random.random() < .25):
+ if random.random() < .25:
output.append(random.choice(self.MUMBLE_LAST))
self.poutput(' '.join(output))
+
if __name__ == '__main__':
c = CmdLineApp()
c.cmdloop()