1
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3
<title>Twisted Documentation: Parsing command-lines with usage.Options</title>
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">Parsing command-lines with usage.Options</h1>
9
<div class="toc"><ol><li><a href="#auto0">Introduction</a></li><li><a href="#auto1">Boolean Options</a></li><ul><li><a href="#auto2">Inheritance, Or: How I Learned to Stop Worrying and Love
10
the Superclass</a></li></ul><li><a href="#auto3">Parameters</a></li><li><a href="#auto4">Option Subcommands</a></li><li><a href="#auto5">Generic Code For Options</a></li><li><a href="#auto6">Parsing Arguments</a></li><li><a href="#auto7">Post Processing</a></li><li><a href="#auto8">Type enforcement</a></li></ol></div>
14
<h2>Introduction<a name="auto0"/></h2>
16
<p>There is frequently a need for programs to parse a UNIX-like
17
command line program: options preceded by <code>-</code> or
18
<code>--</code>, sometimes followed by a parameter, followed by
19
a list of arguments. The <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.usage.html" title="twisted.python.usage">twisted.python.usage</a></code> provides a class,
20
<code>Options</code>, to facilitate such parsing.</p>
22
<p>While Python has the <code>getopt</code> module for doing
23
this, it provides a very low level of abstraction for options.
24
Twisted has a higher level of abstraction, in the class <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.usage.Options.html" title="twisted.python.usage.Options">twisted.python.usage.Options</a></code>. It uses
25
Python's reflection facilities to provide an easy to use yet
26
flexible interface to the command line. While most command line
27
processors either force the application writer to write her own
28
loops, or have arbitrary limitations on the command line (the
29
most common one being not being able to have more then one
30
instance of a specific option, thus rendering the idiom
31
<code class="shell">program -v -v -v</code> impossible), Twisted allows the
32
programmer to decide how much control she wants.</p>
34
<p>The <code>Options</code> class is used by subclassing. Since
35
a lot of time it will be used in the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.tap.html" title="twisted.tap">twisted.tap</a></code> package, where the local
36
conventions require the specific options parsing class to also
37
be called <code>Options</code>, it is usually imported with</p>
38
<pre class="python"><p class="py-linenumber">1
39
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
42
<h2>Boolean Options<a name="auto1"/></h2>
44
<p>For simple boolean options, define the attribute
45
<code>optFlags</code> like this:</p>
46
<pre class="python"><p class="py-linenumber">1
49
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
51
<span class="py-src-variable">optFlags</span> = [[<span class="py-src-string">"fast"</span>, <span class="py-src-string">"f"</span>, <span class="py-src-string">"Act quickly"</span>], [<span class="py-src-string">"safe"</span>, <span class="py-src-string">"s"</span>, <span class="py-src-string">"Act safely"</span>]]
53
<p><code>optFlags</code> should be a list of 3-lists. The first element
54
is the long name, and will be used on the command line as
55
<code>--fast</code>. The second one is the short name, and will be used
56
on the command line as <code>-f</code>. The last element is a
57
description of the flag and will be used to generate the usage
58
information text. The long name also determines the name of the key
59
that will be set on the Options instance. Its value will be 1 if the
60
option was seen, 0 otherwise. Here is an example for usage:</p>
61
<pre class="python"><p class="py-linenumber"> 1
85
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
87
<span class="py-src-variable">optFlags</span> = [
88
[<span class="py-src-string">"fast"</span>, <span class="py-src-string">"f"</span>, <span class="py-src-string">"Act quickly"</span>],
89
[<span class="py-src-string">"good"</span>, <span class="py-src-string">"g"</span>, <span class="py-src-string">"Act well"</span>],
90
[<span class="py-src-string">"cheap"</span>, <span class="py-src-string">"c"</span>, <span class="py-src-string">"Act cheaply"</span>]
93
<span class="py-src-variable">command_line</span> = [<span class="py-src-string">"-g"</span>, <span class="py-src-string">"--fast"</span>]
95
<span class="py-src-variable">options</span> = <span class="py-src-variable">Options</span>()
96
<span class="py-src-keyword">try</span>:
97
<span class="py-src-variable">options</span>.<span class="py-src-variable">parseOptions</span>(<span class="py-src-variable">command_line</span>)
98
<span class="py-src-keyword">except</span> <span class="py-src-variable">usage</span>.<span class="py-src-variable">UsageError</span>, <span class="py-src-variable">errortext</span>:
99
<span class="py-src-keyword">print</span> <span class="py-src-string">'%s: %s'</span> % (<span class="py-src-variable">sys</span>.<span class="py-src-variable">argv</span>[<span class="py-src-number">0</span>], <span class="py-src-variable">errortext</span>)
100
<span class="py-src-keyword">print</span> <span class="py-src-string">'%s: Try --help for usage details.'</span> % (<span class="py-src-variable">sys</span>.<span class="py-src-variable">argv</span>[<span class="py-src-number">0</span>])
101
<span class="py-src-variable">sys</span>.<span class="py-src-variable">exit</span>(<span class="py-src-number">1</span>)
102
<span class="py-src-keyword">if</span> <span class="py-src-variable">options</span>[<span class="py-src-string">'fast'</span>]:
103
<span class="py-src-keyword">print</span> <span class="py-src-string">"fast"</span>,
104
<span class="py-src-keyword">if</span> <span class="py-src-variable">options</span>[<span class="py-src-string">'good'</span>]:
105
<span class="py-src-keyword">print</span> <span class="py-src-string">"good"</span>,
106
<span class="py-src-keyword">if</span> <span class="py-src-variable">options</span>[<span class="py-src-string">'cheap'</span>]:
107
<span class="py-src-keyword">print</span> <span class="py-src-string">"cheap"</span>,
108
<span class="py-src-keyword">print</span>
111
<p>The above will print <code>fast good</code>.</p>
113
<p>Note here that Options fully supports the mapping interface. You can
114
access it mostly just like you can access any other dict. Options are stored
115
as mapping items in the Options instance: parameters as 'paramname': 'value'
116
and flags as 'flagname': 1 or 0.</p>
118
<h3>Inheritance, Or: How I Learned to Stop Worrying and Love
119
the Superclass<a name="auto2"/></h3>
121
<p>Sometimes there is a need for several option processors with
122
a unifying core. Perhaps you want all your commands to
123
understand <code>-q</code>/<code>--quiet</code> means to be
124
quiet, or something similar. On the face of it, this looks
125
impossible: in Python, the subclass's <code>optFlags</code>
126
would shadow the superclass's. However,
127
<code>usage.Options</code> uses special reflection code to get
128
all of the <code>optFlags</code> defined in the hierarchy. So
130
<pre class="python"><p class="py-linenumber">1
139
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">BaseOptions</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
141
<span class="py-src-variable">optFlags</span> = [[<span class="py-src-string">"quiet"</span>, <span class="py-src-string">"q"</span>, <span class="py-src-variable">None</span>]]
143
<span class="py-src-keyword">class</span> <span class="py-src-identifier">SpecificOptions</span>(<span class="py-src-parameter">BaseOptions</span>):
145
<span class="py-src-variable">optFlags</span> = [
146
[<span class="py-src-string">"fast"</span>, <span class="py-src-string">"f"</span>, <span class="py-src-variable">None</span>], [<span class="py-src-string">"good"</span>, <span class="py-src-string">"g"</span>, <span class="py-src-variable">None</span>], [<span class="py-src-string">"cheap"</span>, <span class="py-src-string">"c"</span>, <span class="py-src-variable">None</span>]
149
<p>Is the same as: </p>
150
<pre class="python"><p class="py-linenumber">1
158
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">SpecificOptions</span>(<span class="py-src-parameter">BaseOptions</span>):
160
<span class="py-src-variable">optFlags</span> = [
161
[<span class="py-src-string">"quiet"</span>, <span class="py-src-string">"q"</span>, <span class="py-src-string">"Silence output"</span>],
162
[<span class="py-src-string">"fast"</span>, <span class="py-src-string">"f"</span>, <span class="py-src-string">"Run quickly"</span>],
163
[<span class="py-src-string">"good"</span>, <span class="py-src-string">"g"</span>, <span class="py-src-string">"Don't validate input"</span>],
164
[<span class="py-src-string">"cheap"</span>, <span class="py-src-string">"c"</span>, <span class="py-src-string">"Use cheap resources"</span>]
168
<h2>Parameters<a name="auto3"/></h2>
170
<p>Parameters are specified using the attribute
171
<code>optParameters</code>. They <em>must</em> be given a
172
default. If you want to make sure you got the parameter from
173
the command line, give a non-string default. Since the command
174
line only has strings, this is completely reliable.</p>
176
<p>Here is an example:</p>
177
<pre class="python"><p class="py-linenumber"> 1
207
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
209
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
211
<span class="py-src-variable">optFlags</span> = [
212
[<span class="py-src-string">"fast"</span>, <span class="py-src-string">"f"</span>, <span class="py-src-string">"Run quickly"</span>],
213
[<span class="py-src-string">"good"</span>, <span class="py-src-string">"g"</span>, <span class="py-src-string">"Don't validate input"</span>],
214
[<span class="py-src-string">"cheap"</span>, <span class="py-src-string">"c"</span>, <span class="py-src-string">"Use cheap resources"</span>]
216
<span class="py-src-variable">optParameters</span> = [[<span class="py-src-string">"user"</span>, <span class="py-src-string">"u"</span>, <span class="py-src-variable">None</span>, <span class="py-src-string">"The user name"</span>]]
218
<span class="py-src-variable">config</span> = <span class="py-src-variable">Options</span>()
219
<span class="py-src-keyword">try</span>:
220
<span class="py-src-variable">config</span>.<span class="py-src-variable">parseOptions</span>() <span class="py-src-comment"># When given no argument, parses sys.argv[1:]</span>
221
<span class="py-src-keyword">except</span> <span class="py-src-variable">usage</span>.<span class="py-src-variable">UsageError</span>, <span class="py-src-variable">errortext</span>:
222
<span class="py-src-keyword">print</span> <span class="py-src-string">'%s: %s'</span> % (<span class="py-src-variable">sys</span>.<span class="py-src-variable">argv</span>[<span class="py-src-number">0</span>], <span class="py-src-variable">errortext</span>)
223
<span class="py-src-keyword">print</span> <span class="py-src-string">'%s: Try --help for usage details.'</span> % (<span class="py-src-variable">sys</span>.<span class="py-src-variable">argv</span>[<span class="py-src-number">0</span>])
224
<span class="py-src-variable">sys</span>.<span class="py-src-variable">exit</span>(<span class="py-src-number">1</span>)
226
<span class="py-src-keyword">if</span> <span class="py-src-variable">config</span>[<span class="py-src-string">'user'</span>] <span class="py-src-keyword">is</span> <span class="py-src-keyword">not</span> <span class="py-src-variable">None</span>:
227
<span class="py-src-keyword">print</span> <span class="py-src-string">"Hello"</span>, <span class="py-src-variable">config</span>[<span class="py-src-string">'user'</span>]
228
<span class="py-src-keyword">print</span> <span class="py-src-string">"So, you want it:"</span>
230
<span class="py-src-keyword">if</span> <span class="py-src-variable">config</span>[<span class="py-src-string">'fast'</span>]:
231
<span class="py-src-keyword">print</span> <span class="py-src-string">"fast"</span>,
232
<span class="py-src-keyword">if</span> <span class="py-src-variable">config</span>[<span class="py-src-string">'good'</span>]:
233
<span class="py-src-keyword">print</span> <span class="py-src-string">"good"</span>,
234
<span class="py-src-keyword">if</span> <span class="py-src-variable">config</span>[<span class="py-src-string">'cheap'</span>]:
235
<span class="py-src-keyword">print</span> <span class="py-src-string">"cheap"</span>,
236
<span class="py-src-keyword">print</span>
239
<p>Like <code>optFlags</code>, <code>optParameters</code> works
240
smoothly with inheritance.</p>
242
<h2>Option Subcommands<a name="auto4"/></h2>
244
<p>It is useful, on occassion, to group a set of options together based
245
on the logical <q>action</q> to which they belong. For this, the
246
<code>usage.Options</code> class allows you to define a set of
247
<q>subcommands</q>, each of which can provide its own
248
<code>usage.Options</code> instance to handle its particular
251
<p>Here is an example for an Options class that might parse
252
options like those the cvs program takes</p>
253
<pre class="python"><p class="py-linenumber"> 1
278
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
280
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ImportOptions</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
281
<span class="py-src-variable">optParameters</span> = [
282
[<span class="py-src-string">'module'</span>, <span class="py-src-string">'m'</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">None</span>], [<span class="py-src-string">'vendor'</span>, <span class="py-src-string">'v'</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">None</span>],
283
[<span class="py-src-string">'release'</span>, <span class="py-src-string">'r'</span>, <span class="py-src-variable">None</span>]
286
<span class="py-src-keyword">class</span> <span class="py-src-identifier">CheckoutOptions</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
287
<span class="py-src-variable">optParameters</span> = [[<span class="py-src-string">'module'</span>, <span class="py-src-string">'m'</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">None</span>], [<span class="py-src-string">'tag'</span>, <span class="py-src-string">'r'</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">None</span>]]
289
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
290
<span class="py-src-variable">subCommands</span> = [[<span class="py-src-string">'import'</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">ImportOptions</span>, <span class="py-src-string">"Do an Import"</span>],
291
[<span class="py-src-string">'checkout'</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">CheckoutOptions</span>, <span class="py-src-string">"Do a Checkout"</span>]]
293
<span class="py-src-variable">optParameters</span> = [
294
[<span class="py-src-string">'compression'</span>, <span class="py-src-string">'z'</span>, <span class="py-src-number">0</span>, <span class="py-src-string">'Use compression'</span>],
295
[<span class="py-src-string">'repository'</span>, <span class="py-src-string">'r'</span>, <span class="py-src-variable">None</span>, <span class="py-src-string">'Specify an alternate repository'</span>]
298
<span class="py-src-variable">config</span> = <span class="py-src-variable">Options</span>(); <span class="py-src-variable">config</span>.<span class="py-src-variable">parseOptions</span>()
299
<span class="py-src-keyword">if</span> <span class="py-src-variable">config</span>.<span class="py-src-variable">subCommand</span> == <span class="py-src-string">'import'</span>:
300
<span class="py-src-variable">doImport</span>(<span class="py-src-variable">config</span>.<span class="py-src-variable">subOptions</span>)
301
<span class="py-src-keyword">elif</span> <span class="py-src-variable">config</span>.<span class="py-src-variable">subCommand</span> == <span class="py-src-string">'checkout'</span>:
302
<span class="py-src-variable">doCheckout</span>(<span class="py-src-variable">config</span>.<span class="py-src-variable">subOptions</span>)
305
<p>The <code>subCommands</code> attribute of <code>Options</code>
306
directs the parser to the two other <code>Options</code> subclasses
307
when the strings <code>"import"</code> or <code>"checkout"</code> are
308
present on the command
309
line. All options after the given command string are passed to the
310
specified Options subclass for further parsing. Only one subcommand
311
may be specified at a time. After parsing has completed, the Options
312
instance has two new attributes - <code>subCommand</code> and <code>
313
subOptions</code> - which hold the command string and the Options
314
instance used to parse the remaining options.</p>
316
<h2>Generic Code For Options<a name="auto5"/></h2>
318
<p>Sometimes, just setting an attribute on the basis of the
319
options is not flexible enough. In those cases, Twisted does
320
not even attempt to provide abstractions such as <q>counts</q> or
321
<q>lists</q>, but rathers lets you call your own method, which will
322
be called whenever the option is encountered.</p>
324
<p>Here is an example of counting verbosity</p>
325
<pre class="python"><p class="py-linenumber"> 1
341
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
343
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
345
<span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>):
346
<span class="py-src-variable">usage</span>.<span class="py-src-variable">Options</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>)
347
<span class="py-src-variable">self</span>[<span class="py-src-string">'verbosity'</span>] = <span class="py-src-number">0</span> <span class="py-src-comment"># default</span>
349
<span class="py-src-keyword">def</span> <span class="py-src-identifier">opt_verbose</span>(<span class="py-src-parameter">self</span>):
350
<span class="py-src-variable">self</span>[<span class="py-src-string">'verbosity'</span>] = <span class="py-src-variable">self</span>[<span class="py-src-string">'verbosity'</span>]+<span class="py-src-number">1</span>
352
<span class="py-src-keyword">def</span> <span class="py-src-identifier">opt_quiet</span>(<span class="py-src-parameter">self</span>):
353
<span class="py-src-variable">self</span>[<span class="py-src-string">'verbosity'</span>] = <span class="py-src-variable">self</span>[<span class="py-src-string">'verbosity'</span>]-<span class="py-src-number">1</span>
355
<span class="py-src-variable">opt_v</span> = <span class="py-src-variable">opt_verbose</span>
356
<span class="py-src-variable">opt_q</span> = <span class="py-src-variable">opt_quiet</span>
359
<p>Command lines that look like
360
<code class="shell">command -v -v -v -v</code> will
361
increase verbosity to 4, while
362
<code class="shell">command -q -q -q</code> will decrease
366
<p>The <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.usage.Options.html" title="twisted.python.usage.Options">usage.Options</a></code>
367
class knows that these are
368
parameter-less options, since the methods do not receive an
369
argument. Here is an example for a method with a parameter:</p>
371
<pre class="python"><p class="py-linenumber"> 1
383
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
385
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
387
<span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>):
388
<span class="py-src-variable">usage</span>.<span class="py-src-variable">Options</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>)
389
<span class="py-src-variable">self</span>[<span class="py-src-string">'symbols'</span>] = []
391
<span class="py-src-keyword">def</span> <span class="py-src-identifier">opt_define</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">symbol</span>):
392
<span class="py-src-variable">self</span>[<span class="py-src-string">'symbols'</span>].<span class="py-src-variable">append</span>(<span class="py-src-variable">symbol</span>)
394
<span class="py-src-variable">opt_D</span> = <span class="py-src-variable">opt_define</span>
397
<p>This example is useful for the common idiom of having
398
<code>command -DFOO -DBAR</code> to define symbols.</p>
400
<h2>Parsing Arguments<a name="auto6"/></h2>
402
<p><code>usage.Options</code> does not stop helping when the
403
last parameter is gone. All the other arguments are sent into a
404
function which should deal with them. Here is an example for a
405
<code>cmp</code> like command.</p>
406
<pre class="python"><p class="py-linenumber">1
415
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
417
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
419
<span class="py-src-variable">optParameters</span> = [[<span class="py-src-string">"max_differences"</span>, <span class="py-src-string">"d"</span>, <span class="py-src-number">1</span>, <span class="py-src-variable">None</span>]]
421
<span class="py-src-keyword">def</span> <span class="py-src-identifier">parseArgs</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">origin</span>, <span class="py-src-parameter">changed</span>):
422
<span class="py-src-variable">self</span>[<span class="py-src-string">'origin'</span>] = <span class="py-src-variable">origin</span>
423
<span class="py-src-variable">self</span>[<span class="py-src-string">'changed'</span>] = <span class="py-src-variable">changed</span>
426
<p>The command should look like <code>command origin
429
<p>If you want to have a variable number of left-over
430
arguments, just use <code>def parseArgs(self, *args):</code>.
431
This is useful for commands like the UNIX
432
<code>cat(1)</code>.</p>
434
<h2>Post Processing<a name="auto7"/></h2>
436
<p>Sometimes, you want to perform post processing of options to
437
patch up inconsistencies, and the like. Here is an example:</p>
438
<pre class="python"><p class="py-linenumber"> 1
451
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
453
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
455
<span class="py-src-variable">optFlags</span> = [
456
[<span class="py-src-string">"fast"</span>, <span class="py-src-string">"f"</span>, <span class="py-src-string">"Run quickly"</span>],
457
[<span class="py-src-string">"good"</span>, <span class="py-src-string">"g"</span>, <span class="py-src-string">"Don't validate input"</span>],
458
[<span class="py-src-string">"cheap"</span>, <span class="py-src-string">"c"</span>, <span class="py-src-string">"Use cheap resources"</span>]
461
<span class="py-src-keyword">def</span> <span class="py-src-identifier">postOptions</span>(<span class="py-src-parameter">self</span>):
462
<span class="py-src-keyword">if</span> <span class="py-src-variable">self</span>[<span class="py-src-string">'fast'</span>] <span class="py-src-keyword">and</span> <span class="py-src-variable">self</span>[<span class="py-src-string">'good'</span>] <span class="py-src-keyword">and</span> <span class="py-src-variable">self</span>[<span class="py-src-string">'cheap'</span>]:
463
<span class="py-src-keyword">raise</span> <span class="py-src-variable">usage</span>.<span class="py-src-variable">UsageError</span>, <span class="py-src-string">"can't have it all, brother"</span>
466
<h2>Type enforcement<a name="auto8"/></h2>
468
<p>By default, all options are handled as strings. You may want to
469
enforce the type of your option in some specific case, the classic example
470
being port number. Any callable can be specified in the fifth row of
471
<code>optParameters</code> and will be called with the string value passed
475
<pre class="python"><p class="py-linenumber">1
480
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
482
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
483
<span class="py-src-variable">optParameters</span> = [[<span class="py-src-string">"shiny_integer"</span>, <span class="py-src-string">"s"</span>, <span class="py-src-number">1</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">int</span>]]
484
<span class="py-src-variable">optParameters</span> = [[<span class="py-src-string">"dummy_float"</span>, <span class="py-src-string">"d"</span>, <span class="py-src-number">3.14159</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">float</span>]]
487
<p>Note that default values are not coerced, so you should either declare
488
it with the good type (as above) or handle it when you use your
491
<p>The coerce function may have a coerceDoc attribute, the content of which
492
will be printed after the documentation of the option. It's particularly
493
useful for reusing the function at multiple places.</p>
495
<pre class="python"><p class="py-linenumber"> 1
506
</p><span class="py-src-keyword">def</span> <span class="py-src-identifier">oneTwoThree</span>(<span class="py-src-parameter">val</span>):
507
<span class="py-src-variable">val</span> = <span class="py-src-variable">int</span>(<span class="py-src-variable">val</span>)
508
<span class="py-src-keyword">if</span> <span class="py-src-variable">val</span> <span class="py-src-keyword">not</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">range</span>(<span class="py-src-number">1</span>, <span class="py-src-number">4</span>):
509
<span class="py-src-keyword">raise</span> <span class="py-src-variable">ValueError</span>(<span class="py-src-string">"Not in range"</span>)
510
<span class="py-src-keyword">return</span> <span class="py-src-variable">val</span>
511
<span class="py-src-variable">oneTwoThree</span>.<span class="py-src-variable">coerceDoc</span> = <span class="py-src-string">"Must be 1, 2 or 3."</span>
513
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">usage</span>
515
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Options</span>(<span class="py-src-parameter">usage</span>.<span class="py-src-parameter">Options</span>):
516
<span class="py-src-variable">optParameters</span> = [[<span class="py-src-string">"one_choice"</span>, <span class="py-src-string">"o"</span>, <span class="py-src-number">1</span>, <span class="py-src-variable">None</span>, <span class="py-src-variable">oneTwoThree</span>]]
519
<p>This example code will print the following help when added to your program:
522
<pre class="shell" xml:space="preserve">
523
$ <em>python myprogram.py --help</em>
524
Usage: myprogram [options]
526
-o, --one_choice= [default: 0]. Must be 1, 2 or 3.
530
<p><a href="index.html">Index</a></p>
531
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'