Skip to content

Commit f76f437

Browse files
committed
switch from optparse to argparse
1 parent e7685b5 commit f76f437

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

examples/example.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
"""
1313

1414
import random
15+
import argparse
1516

16-
from cmd2 import Cmd, make_option, options, set_use_arg_list
17+
from cmd2 import Cmd, with_argument_parser
1718

1819

1920
class CmdLineApp(Cmd):
@@ -37,41 +38,41 @@ def __init__(self):
3738
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
3839
Cmd.__init__(self, use_ipython=False)
3940

40-
# For option commands, pass a single argument string instead of a list of argument strings to the do_* methods
41-
set_use_arg_list(False)
42-
43-
opts = [make_option('-p', '--piglatin', action="store_true", help="atinLay"),
44-
make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
45-
make_option('-r', '--repeat', type="int", help="output [n] times")]
46-
47-
@options(opts, arg_desc='(text to say)')
48-
def do_speak(self, arg, opts=None):
41+
argparser = argparse.ArgumentParser()
42+
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
43+
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
44+
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
45+
argparser.add_argument('words', nargs='+', help='words to say')
46+
@with_argument_parser(argparser)
47+
def do_speak(self, cmdline, opts=None):
4948
"""Repeats what you tell me to."""
50-
arg = ''.join(arg)
51-
if opts.piglatin:
52-
arg = '%s%say' % (arg[1:], arg[0])
53-
if opts.shout:
54-
arg = arg.upper()
55-
repetitions = opts.repeat or 1
49+
words = []
50+
for word in args.words:
51+
if args.piglatin:
52+
word = '%s%say' % (word[1:], word[0])
53+
if args.shout:
54+
word = word.upper()
55+
words.append(word)
56+
repetitions = args.repeat or 1
5657
for i in range(min(repetitions, self.maxrepeats)):
57-
self.poutput(arg)
58-
# recommend using the poutput function instead of
59-
# self.stdout.write or "print", because Cmd allows the user
60-
# to redirect output
58+
# .poutput handles newlines, and accommodates output redirection too
59+
self.poutput(' '.join(words))
6160

6261
do_say = do_speak # now "say" is a synonym for "speak"
6362
do_orate = do_speak # another synonym, but this one takes multi-line input
6463

65-
@options([ make_option('-r', '--repeat', type="int", help="output [n] times") ])
66-
def do_mumble(self, arg, opts=None):
64+
argparser = argparse.ArgumentParser()
65+
argparser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
66+
argparser.add_argument('words', nargs='+', help='words to say')
67+
@with_argument_parser(argparser)
68+
def do_mumble(self, cmdline, args=None):
6769
"""Mumbles what you tell me to."""
68-
repetitions = opts.repeat or 1
69-
arg = arg.split()
70+
repetitions = args.repeat or 1
7071
for i in range(min(repetitions, self.maxrepeats)):
7172
output = []
7273
if (random.random() < .33):
7374
output.append(random.choice(self.MUMBLE_FIRST))
74-
for word in arg:
75+
for word in args.words:
7576
if (random.random() < .40):
7677
output.append(random.choice(self.MUMBLES))
7778
output.append(word)

0 commit comments

Comments
 (0)