summaryrefslogtreecommitdiff
path: root/examples/pirate.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-15 12:11:12 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-15 12:11:12 -0500
commiteee2d621abfb3d6455570b540069a4853a68f8c6 (patch)
tree6bfc049369079868eab6f05ce8d0cae233c43cb9 /examples/pirate.py
parentaec6704db9542e35e4cdc6073210bc0d6c507335 (diff)
downloadcmd2-git-eee2d621abfb3d6455570b540069a4853a68f8c6.tar.gz
Changed @with_argument_parser to only pass single argument to commands
Also added another @with_argparser_and_list decorator that uses argparse.parse_known_args to pass two arguments to a command: both the argparse output and a list of unknown/unmatched args.
Diffstat (limited to 'examples/pirate.py')
-rwxr-xr-xexamples/pirate.py23
1 files changed, 12 insertions, 11 deletions
diff --git a/examples/pirate.py b/examples/pirate.py
index f8c91315..55457e06 100755
--- a/examples/pirate.py
+++ b/examples/pirate.py
@@ -6,8 +6,8 @@ presented as part of her PyCon 2010 talk.
It demonstrates many features of cmd2.
"""
-from cmd2 import Cmd, options
-from optparse import make_option
+import argparse
+from cmd2 import Cmd, with_argument_parser
class Pirate(Cmd):
@@ -73,17 +73,18 @@ class Pirate(Cmd):
"""Sing a colorful song."""
print(self.colorize(arg, self.songcolor))
- @options([make_option('--ho', type='int', default=2,
- help="How often to chant 'ho'"),
- make_option('-c', '--commas',
- action="store_true",
- help="Intersperse commas")])
- def do_yo(self, arg, opts):
+ yo_parser = argparse.ArgumentParser()
+ yo_parser.add_argument('--ho', type=int, default=2, help="How often to chant 'ho'")
+ yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas')
+ yo_parser.add_argument('beverage', nargs=1, help='beverage to drink with the chant')
+
+ @with_argument_parser(yo_parser)
+ def do_yo(self, args):
"""Compose a yo-ho-ho type chant with flexible options."""
- chant = ['yo'] + ['ho'] * opts.ho
- separator = ', ' if opts.commas else ' '
+ chant = ['yo'] + ['ho'] * args.ho
+ separator = ', ' if args.commas else ' '
chant = separator.join(chant)
- print('{0} and a bottle of {1}'.format(chant, arg))
+ print('{0} and a bottle of {1}'.format(chant, args.beverage[0]))
if __name__ == '__main__':