You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -61,6 +70,9 @@ for help on the command. When you use the ``@with_argparser``
61
70
decorator, the docstring for the ``do_*`` method is used to set the description for the ``argparse.ArgumentParser`` is
62
71
With this code::
63
72
73
+
import argparse
74
+
from cmd2 import with_argparser
75
+
64
76
argparser = argparse.ArgumentParser()
65
77
argparser.add_argument('tag', help='tag')
66
78
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@@ -89,6 +101,9 @@ The ``help tag`` command displays:
89
101
If you would prefer you can set the ``description`` while instantiating the ``argparse.ArgumentParser`` and leave the
90
102
docstring on your method empty::
91
103
104
+
import argparse
105
+
from cmd2 import with_argparser
106
+
92
107
argparser = argparse.ArgumentParser(description='create an html tag')
93
108
argparser.add_argument('tag', help='tag')
94
109
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@@ -115,10 +130,11 @@ Now when the user enters ``help tag`` they see:
115
130
116
131
To add additional text to the end of the generated help message, use the ``epilog`` variable::
117
132
118
-
argparser = argparse.ArgumentParser(
119
-
description='create an html tag',
120
-
epilog='This command can not generate tags with no content, like <br/>.'
121
-
)
133
+
import argparse
134
+
from cmd2 import with_argparser
135
+
136
+
argparser = argparse.ArgumentParser(description='create an html tag',
137
+
epilog='This command can not generate tags with no content, like <br/>.')
122
138
argparser.add_argument('tag', help='tag')
123
139
argparser.add_argument('content', nargs='+', help='content to surround with tag')
124
140
@with_argparser(argparser)
@@ -150,17 +166,19 @@ Receiving an argument list
150
166
The default behavior of ``cmd2`` is to pass the user input directly to your
151
167
``do_*`` methods as a string. If you don't want to use the full argument parser support outlined above, you can still have ``cmd2`` apply shell parsing rules to the user input and pass you a list of arguments instead of a string. Apply the ``@with_argument_list`` decorator to those methods that should receive an argument list instead of a string::
152
168
153
-
class CmdLineApp(cmd2.Cmd):
154
-
""" Example cmd2 application. """
169
+
from cmd2 import with_argument_list
170
+
171
+
class CmdLineApp(cmd2.Cmd):
172
+
""" Example cmd2 application. """
155
173
156
-
def do_say(self, cmdline):
157
-
# cmdline contains a string
158
-
pass
174
+
def do_say(self, cmdline):
175
+
# cmdline contains a string
176
+
pass
159
177
160
-
@with_argument_list
161
-
def do_speak(self, arglist):
162
-
# arglist contains a list of arguments
163
-
pass
178
+
@with_argument_list
179
+
def do_speak(self, arglist):
180
+
# arglist contains a list of arguments
181
+
pass
164
182
165
183
166
184
Using the argument parser decorator and also receiving a a list of unknown positional arguments
@@ -170,6 +188,9 @@ decorate the command method with the ``@with_argparser_and_unknown_args`` decora
170
188
171
189
Here's what it looks like::
172
190
191
+
import argparse
192
+
from cmd2 import with_argparser_and_unknown_args
193
+
173
194
dir_parser = argparse.ArgumentParser()
174
195
dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line")
175
196
@@ -209,6 +230,9 @@ recommends using ``argparse`` instead.
0 commit comments