Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="optparse-callback-example-5.html" /> | |
13 | <link rel="parent" href="optparse-option-callbacks.html" /> | |
14 | <link rel="next" href="optparse-extending-optparse.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>6.21.4.9 Callback example 6: variable arguments</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="6.21.4.8 Callback example 5:" | |
24 | href="optparse-callback-example-5.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="6.21.4 Option Callbacks" | |
27 | href="optparse-option-callbacks.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="6.21.5 Extending optparse" | |
30 | href="optparse-extending-optparse.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="optparse-callback-example-5.html">6.21.4.8 Callback example 5:</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="optparse-option-callbacks.html">6.21.4 Option Callbacks</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H3><A NAME="SECTION0082149000000000000000"></A><A NAME="optparse-callback-example-6"></A> | |
55 | <BR> | |
56 | 6.21.4.9 Callback example 6: variable arguments | |
57 | </H3> | |
58 | ||
59 | <P> | |
60 | Things get hairy when you want an option to take a variable number of | |
61 | arguments. For this case, you must write a callback, as <tt class="module">optparse</tt> doesn't | |
62 | provide any built-in capabilities for it. And you have to deal with | |
63 | certain intricacies of conventional <span class="Unix">Unix</span> command-line parsing that <tt class="module">optparse</tt> | |
64 | normally handles for you. In particular, callbacks should implement | |
65 | the conventional rules for bare <code>"-"</code> and <code>"-"</code> arguments: | |
66 | ||
67 | <UL> | |
68 | <LI> | |
69 | either <code>"-"</code> or <code>"-"</code> can be option arguments | |
70 | ||
71 | <P> | |
72 | </LI> | |
73 | <LI> | |
74 | bare <code>"-"</code> (if not the argument to some option): halt command-line | |
75 | processing and discard the <code>"-"</code> | |
76 | ||
77 | <P> | |
78 | </LI> | |
79 | <LI> | |
80 | bare <code>"-"</code> (if not the argument to some option): halt command-line | |
81 | processing but keep the <code>"-"</code> (append it to <code>parser.largs</code>) | |
82 | ||
83 | <P> | |
84 | </LI> | |
85 | </UL> | |
86 | ||
87 | <P> | |
88 | If you want an option that takes a variable number of arguments, there | |
89 | are several subtle, tricky issues to worry about. The exact | |
90 | implementation you choose will be based on which trade-offs you're | |
91 | willing to make for your application (which is why <tt class="module">optparse</tt> doesn't support | |
92 | this sort of thing directly). | |
93 | ||
94 | <P> | |
95 | Nevertheless, here's a stab at a callback for an option with variable | |
96 | arguments: | |
97 | <div class="verbatim"><pre> | |
98 | def vararg_callback(option, opt_str, value, parser): | |
99 | assert value is None | |
100 | done = 0 | |
101 | value = [] | |
102 | rargs = parser.rargs | |
103 | while rargs: | |
104 | arg = rargs[0] | |
105 | ||
106 | # Stop if we hit an arg like "--foo", "-a", "-fx", "--file=f", | |
107 | # etc. Note that this also stops on "-3" or "-3.0", so if | |
108 | # your option takes numeric values, you will need to handle | |
109 | # this. | |
110 | if ((arg[:2] == "--" and len(arg) > 2) or | |
111 | (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")): | |
112 | break | |
113 | else: | |
114 | value.append(arg) | |
115 | del rargs[0] | |
116 | ||
117 | setattr(parser.values, option.dest, value) | |
118 | ||
119 | [...] | |
120 | parser.add_option("-c", "--callback", | |
121 | action="callback", callback=varargs) | |
122 | </pre></div> | |
123 | ||
124 | <P> | |
125 | The main weakness with this particular implementation is that negative | |
126 | numbers in the arguments following <code>"-c"</code> will be interpreted as | |
127 | further options (probably causing an error), rather than as arguments to | |
128 | <code>"-c"</code>. Fixing this is left as an exercise for the reader. | |
129 | ||
130 | <P> | |
131 | ||
132 | <DIV CLASS="navigation"> | |
133 | <div class='online-navigation'> | |
134 | <p></p><hr /> | |
135 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
136 | <tr> | |
137 | <td class='online-navigation'><a rel="prev" title="6.21.4.8 Callback example 5:" | |
138 | href="optparse-callback-example-5.html"><img src='../icons/previous.png' | |
139 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
140 | <td class='online-navigation'><a rel="parent" title="6.21.4 Option Callbacks" | |
141 | href="optparse-option-callbacks.html"><img src='../icons/up.png' | |
142 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
143 | <td class='online-navigation'><a rel="next" title="6.21.5 Extending optparse" | |
144 | href="optparse-extending-optparse.html"><img src='../icons/next.png' | |
145 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
146 | <td align="center" width="100%">Python Library Reference</td> | |
147 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
148 | href="contents.html"><img src='../icons/contents.png' | |
149 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
150 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
151 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
152 | <td class='online-navigation'><a rel="index" title="Index" | |
153 | href="genindex.html"><img src='../icons/index.png' | |
154 | border='0' height='32' alt='Index' width='32' /></A></td> | |
155 | </tr></table> | |
156 | <div class='online-navigation'> | |
157 | <b class="navlabel">Previous:</b> | |
158 | <a class="sectref" rel="prev" href="optparse-callback-example-5.html">6.21.4.8 Callback example 5:</A> | |
159 | <b class="navlabel">Up:</b> | |
160 | <a class="sectref" rel="parent" href="optparse-option-callbacks.html">6.21.4 Option Callbacks</A> | |
161 | <b class="navlabel">Next:</b> | |
162 | <a class="sectref" rel="next" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> | |
163 | </div> | |
164 | </div> | |
165 | <hr /> | |
166 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
167 | </DIV> | |
168 | <!--End of Navigation Panel--> | |
169 | <ADDRESS> | |
170 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
171 | </ADDRESS> | |
172 | </BODY> | |
173 | </HTML> |