11#!/usr/bin/env python
22# coding=utf-8
3- """A sample application for cmd2 showing how to use Argparse to process command line arguments for your application.
4- It parses command line arguments looking for known arguments, but then still passes any unknown arguments onto cmd2
5- to treat them as arguments at invocation.
3+ """A sample application for cmd2 showing how to use argparse to
4+ process command line arguments for your application.
65
7- Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for argparse_example.py when
8- used with the exampleSession.txt transcript.
6+ Thanks to cmd2's built-in transcript testing capability, it also
7+ serves as a test suite for argparse_example.py when used with the
8+ exampleSession.txt transcript.
99
10- Running `python argparse_example.py -t exampleSession.txt` will run all the commands in the transcript against
11- argparse_example.py, verifying that the output produced matches the transcript.
10+ Running `python argparse_example.py -t exampleSession.txt` will run
11+ all the commands in the transcript against argparse_example.py,
12+ verifying that the output produced matches the transcript.
1213"""
1314import argparse
1415import sys
@@ -39,40 +40,14 @@ def __init__(self, ip_addr=None, port=None, transcript_files=None):
3940 # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
4041 # self.default_to_shell = True
4142
42- @options ([make_option ('-p' , '--piglatin' , action = "store_true" , help = "atinLay" ),
43- make_option ('-s' , '--shout' , action = "store_true" , help = "N00B EMULATION MODE" ),
44- make_option ('-r' , '--repeat' , type = "int" , help = "output [n] times" )
45- ])
46- def do_speak (self , arg , opts = None ):
47- """Repeats what you tell me to."""
48- words = []
49- for word in arg :
50- if opts .piglatin :
51- word = '%s%say' % (word [1 :], word [0 ])
52- if opts .shout :
53- arg = arg .upper ()
54- words .append (word )
55- repetitions = opts .repeat or 1
56- for i in range (min (repetitions , self .maxrepeats )):
57- self .stdout .write (' ' .join (words ))
58- self .stdout .write ('\n ' )
59- # self.stdout.write is better than "print", because Cmd can be
60- # initialized with a non-standard output destination
6143
62- do_say = do_speak # now "say" is a synonym for "speak"
63- do_orate = do_speak # another synonym, but this one takes multi-line input
64-
65-
66- argparser = argparse .ArgumentParser (
67- prog = 'sspeak' ,
68- description = 'Repeats what you tell me to'
69- )
44+ argparser = argparse .ArgumentParser (prog = 'speak' )
7045 argparser .add_argument ('-p' , '--piglatin' , action = 'store_true' , help = 'atinLay' )
7146 argparser .add_argument ('-s' , '--shout' , action = 'store_true' , help = 'N00B EMULATION MODE' )
7247 argparser .add_argument ('-r' , '--repeat' , type = int , help = 'output [n] times' )
7348 argparser .add_argument ('words' , nargs = '+' , help = 'words to say' )
7449 @with_argument_parser (argparser )
75- def do_sspeak (self , rawarg , args = None ):
50+ def do_speak (self , rawarg , args = None ):
7651 """Repeats what you tell me to."""
7752 words = []
7853 for word in args .words :
@@ -88,10 +63,13 @@ def do_sspeak(self, rawarg, args=None):
8863 # self.stdout.write is better than "print", because Cmd can be
8964 # initialized with a non-standard output destination
9065
66+ do_say = do_speak # now "say" is a synonym for "speak"
67+ do_orate = do_speak # another synonym, but this one takes multi-line input
68+
9169
9270 argparser = argparse .ArgumentParser (
9371 prog = 'tag' ,
94- description = 'create an html tag, the first argument is the tag, the rest is the contents'
72+ description = 'create a html tag' ,
9573 )
9674 argparser .add_argument ('tag' , nargs = 1 , help = 'tag' )
9775 argparser .add_argument ('content' , nargs = '+' , help = 'content to surround with tag' )
@@ -102,6 +80,28 @@ def do_tag(self, cmdline, args=None):
10280 # self.stdout.write is better than "print", because Cmd can be
10381 # initialized with a non-standard output destination
10482
83+ # @options uses the python optparse module which has been deprecated
84+ # since 2011. Use @with_argument_parser instead, which utilizes the
85+ # python argparse module
86+ @options ([make_option ('-p' , '--piglatin' , action = "store_true" , help = "atinLay" ),
87+ make_option ('-s' , '--shout' , action = "store_true" , help = "N00B EMULATION MODE" ),
88+ make_option ('-r' , '--repeat' , type = "int" , help = "output [n] times" )
89+ ])
90+ def do_deprecated_speak (self , arg , opts = None ):
91+ """Repeats what you tell me to."""
92+ words = []
93+ for word in arg :
94+ if opts .piglatin :
95+ word = '%s%say' % (word [1 :], word [0 ])
96+ if opts .shout :
97+ arg = arg .upper ()
98+ words .append (word )
99+ repetitions = opts .repeat or 1
100+ for i in range (min (repetitions , self .maxrepeats )):
101+ self .stdout .write (' ' .join (words ))
102+ self .stdout .write ('\n ' )
103+ # self.stdout.write is better than "print", because Cmd can be
104+ # initialized with a non-standard output destination
105105
106106if __name__ == '__main__' :
107107 # You can do your custom Argparse parsing here to meet your application's needs
0 commit comments