summaryrefslogtreecommitdiff
path: root/examples/argparse_example.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-01-07 18:30:34 -0700
committerkotfu <kotfu@kotfu.net>2018-01-07 18:30:34 -0700
commit8c58bb558adceb8ff32c7a3e1a88d2a13371dbfa (patch)
tree896f61548da5db985fb33e61e90486fb3d7fe5a7 /examples/argparse_example.py
parentc25a2b7949c02449279f548db1c8de9d10214cdc (diff)
downloadcmd2-git-8c58bb558adceb8ff32c7a3e1a88d2a13371dbfa.tar.gz
Properly set docstring so it contains help message
Diffstat (limited to 'examples/argparse_example.py')
-rwxr-xr-xexamples/argparse_example.py72
1 files changed, 36 insertions, 36 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py
index 16de343c..c6a17435 100755
--- a/examples/argparse_example.py
+++ b/examples/argparse_example.py
@@ -1,14 +1,15 @@
#!/usr/bin/env python
# coding=utf-8
-"""A sample application for cmd2 showing how to use Argparse to process command line arguments for your application.
-It parses command line arguments looking for known arguments, but then still passes any unknown arguments onto cmd2
-to treat them as arguments at invocation.
+"""A sample application for cmd2 showing how to use argparse to
+process command line arguments for your application.
-Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for argparse_example.py when
-used with the exampleSession.txt transcript.
+Thanks to cmd2's built-in transcript testing capability, it also
+serves as a test suite for argparse_example.py when used with the
+exampleSession.txt transcript.
-Running `python argparse_example.py -t exampleSession.txt` will run all the commands in the transcript against
-argparse_example.py, verifying that the output produced matches the transcript.
+Running `python argparse_example.py -t exampleSession.txt` will run
+all the commands in the transcript against argparse_example.py,
+verifying that the output produced matches the transcript.
"""
import argparse
import sys
@@ -39,40 +40,14 @@ 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
- @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
- make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
- make_option('-r', '--repeat', type="int", help="output [n] times")
- ])
- def do_speak(self, arg, opts=None):
- """Repeats what you tell me to."""
- words = []
- for word in arg:
- if opts.piglatin:
- word = '%s%say' % (word[1:], word[0])
- if opts.shout:
- arg = arg.upper()
- words.append(word)
- repetitions = opts.repeat or 1
- for i in range(min(repetitions, self.maxrepeats)):
- self.stdout.write(' '.join(words))
- self.stdout.write('\n')
- # self.stdout.write is better than "print", because Cmd can be
- # initialized with a non-standard output destination
- 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(
- prog='sspeak',
- description='Repeats what you tell me to'
- )
+ argparser = argparse.ArgumentParser(prog='speak')
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)
- def do_sspeak(self, rawarg, args=None):
+ def do_speak(self, rawarg, args=None):
"""Repeats what you tell me to."""
words = []
for word in args.words:
@@ -88,10 +63,13 @@ class CmdLineApp(Cmd):
# self.stdout.write is better than "print", because Cmd can be
# initialized with a non-standard output destination
+ 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(
prog='tag',
- description='create an html tag, the first argument is the tag, the rest is the contents'
+ description='create a html tag',
)
argparser.add_argument('tag', nargs=1, help='tag')
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@@ -102,6 +80,28 @@ class CmdLineApp(Cmd):
# self.stdout.write is better than "print", because Cmd can be
# initialized with a non-standard output destination
+ # @options uses the python optparse module which has been deprecated
+ # since 2011. Use @with_argument_parser instead, which utilizes the
+ # python argparse module
+ @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
+ make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
+ make_option('-r', '--repeat', type="int", help="output [n] times")
+ ])
+ def do_deprecated_speak(self, arg, opts=None):
+ """Repeats what you tell me to."""
+ words = []
+ for word in arg:
+ if opts.piglatin:
+ word = '%s%say' % (word[1:], word[0])
+ if opts.shout:
+ arg = arg.upper()
+ words.append(word)
+ repetitions = opts.repeat or 1
+ for i in range(min(repetitions, self.maxrepeats)):
+ self.stdout.write(' '.join(words))
+ self.stdout.write('\n')
+ # 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