summaryrefslogtreecommitdiff
path: root/examples/example.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-15 01:34:50 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-15 01:34:50 -0500
commit141d95194b30d959f6c21f4546100551c442b13d (patch)
tree5d1602a6d2117aaa4910adfff31ad919b85fea07 /examples/example.py
parent2fcc883f8a7fd5c9fd3beb30465cfa2b86c66ffc (diff)
downloadcmd2-git-141d95194b30d959f6c21f4546100551c442b13d.tar.gz
Made a couple cleanup changes
1) cmd2 no longer imports make_option from optparse - test files and examples now import this directly - this helps emphasize that this old optparse methodology of adding options to commands is deprecated 2) All argparsers have been given custom names instead of just "argparser" - this helps with readability and maintainability, especially with IDE renaming and such
Diffstat (limited to 'examples/example.py')
-rwxr-xr-xexamples/example.py29
1 files changed, 16 insertions, 13 deletions
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()