CommandLine 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,31 @@
1
+ OPTIONPARSER LICENSE
2
+
3
+ Copyright (c) 2005, Jim Freeze
4
+ All rights reserved.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files
8
+ (the "Software"), to deal in the Software without restriction,
9
+ including without limitation the rights to use, copy, modify,
10
+ merge, publish, distribute, sublicense, and/or sell copies of the
11
+ Software, and to permit persons to whom the Software is furnished
12
+ to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ http://www.opensource.org/licenses/mit-license.php
30
+ http://www.opensource.org/licenses/bsd-license.php
31
+
data/README ADDED
@@ -0,0 +1,380 @@
1
+ CommandLine - Application and OptionParser
2
+ ==========================================
3
+ Author: Jim Freeze
4
+ Copyright 2005 Jim Freeze
5
+
6
+ ABOUT
7
+ =====
8
+ CommandLine is a tool that facilitates building command line
9
+ applications and parsing the command line. Version 0.6.0
10
+ supercedes OptionParser-0.5.0 since the option libs are now part
11
+ of CommandLine. (I thought that maintianing two gems for
12
+ the option libraries would be confusing.)
13
+
14
+ CommandLine provides a convenient way to quickly develop
15
+ a professional looking commandline application.
16
+ The OptionParser provides efficient tools to add and
17
+ handle options while still allowing your application to
18
+ handle just about any argument configuration you may need.
19
+
20
+ Probably the best way to describe how the tool works is
21
+ with an example:
22
+
23
+
24
+ % cat app2.rb
25
+ #---------------------------------------------------
26
+ #!/usr/bin/env ruby
27
+
28
+ require 'rubygems'
29
+ require 'commandline'
30
+
31
+ #
32
+ # A minimum application
33
+ #
34
+ class App < CommandLine::Application
35
+
36
+ def initialize
37
+ args 1
38
+ super
39
+ end
40
+
41
+ def main
42
+ end
43
+ end#class App
44
+ #---------------------------------------------------
45
+
46
+ % app2.rb
47
+ Usage: app2.rb
48
+
49
+ % cat app5.rb
50
+ #---------------------------------------------------
51
+ #!/usr/bin/env ruby
52
+
53
+ begin
54
+ require 'commandline'
55
+ rescue LoadError
56
+ require 'rubygems'
57
+ retry
58
+ end
59
+
60
+ class App < CommandLine::Application
61
+
62
+ def initialize
63
+ version "0.0.1"
64
+ author "Author Name"
65
+ copyright "Copyright (c) 2005, Jim Freeze"
66
+ synopsis "[-dhV] param_file out_file"
67
+ short_description "A simple app example that takes two arguments."
68
+ long_description "app5 is a simple application example that supports "+
69
+ "three options and two commandline arguments."
70
+
71
+ option :version
72
+ option :debug
73
+ option :help
74
+
75
+ args :param_file, :out_file
76
+
77
+ super
78
+ end
79
+
80
+ def main
81
+ puts "main called"
82
+ puts "@param_file = #{@param_file}"
83
+ puts "@out_file = #{@out_file}"
84
+ end
85
+ end#class App
86
+ #---------------------------------------------------
87
+
88
+ % app5.rb
89
+ Usage: app5.rb [-dhV] param_file out_file
90
+
91
+ % app5.rb -h
92
+ NAME
93
+
94
+ app5.rb - A simple app example that takes two arguments.
95
+
96
+ DESCRIPTION
97
+
98
+ app5.rb is a simple application example that supports three options
99
+ and two commandline arguments.
100
+
101
+ OPTIONS
102
+
103
+ --version,-V
104
+ Displays application version.
105
+
106
+ --debug,-d
107
+ Sets debug to true.
108
+
109
+ --help,-h
110
+ Displays help page.
111
+
112
+ AUTHOR: Author Name
113
+ Copyright (c) 2005, Jim Freeze
114
+
115
+ % app5.rb f1 f2
116
+ main called
117
+ @param_file = f1
118
+ @out_file = f2
119
+
120
+ % cat app6.rb
121
+ #---------------------------------------------------
122
+ #!/usr/bin/env ruby
123
+
124
+ begin
125
+ require 'commandline'
126
+ rescue LoadError
127
+ require 'rubygems'
128
+ retry
129
+ end
130
+
131
+ #
132
+ # An application demonstrating customizing of canonical options
133
+ #
134
+ class App < CommandLine::Application
135
+
136
+ def initialize
137
+ version "0.0.1"
138
+ author "Author Name"
139
+ copyright "Copyright (c) 2005, Jim Freeze"
140
+ short_description "A simple app example that takes two arguments."
141
+ long_description "This app is a simple application example that supports "+
142
+ "three options and two commandline arguments."
143
+
144
+ option :version, :names => %w(--version -v --notice-the-change-from-app5)
145
+ option :debug, :arity => [0,1], :arg_description => "debug_level",
146
+ :opt_description => "Set debug level from 0 to 9."
147
+ option :help
148
+
149
+ args :param_file, :out_file
150
+
151
+ super
152
+ end
153
+
154
+ def main
155
+ puts "main called"
156
+ puts "@param_file = #{@param_file}"
157
+ puts "@out_file = #{@out_file}"
158
+ end
159
+ end#class App
160
+ #---------------------------------------------------
161
+
162
+ % app6.rb -h
163
+ NAME
164
+
165
+ app6.rb - A simple app example that takes two arguments.
166
+
167
+ DESCRIPTION
168
+
169
+ This app is a simple application example that supports three
170
+ options and two commandline arguments.
171
+
172
+ OPTIONS
173
+
174
+ --version,-v,--notice-the-change-from-app5
175
+ Displays application version.
176
+
177
+ --debug,-d debug_level
178
+ Set debug level from 0 to 9.
179
+
180
+ --help,-h
181
+ Displays help page.
182
+
183
+ AUTHOR: Author Name
184
+ Copyright (c) 2005, Jim Freeze
185
+
186
+ % cat app7.rb
187
+ #---------------------------------------------------
188
+ #!/usr/bin/env ruby
189
+
190
+ begin
191
+ require 'commandline'
192
+ rescue LoadError
193
+ require 'rubygems'
194
+ retry
195
+ end
196
+
197
+ #
198
+ # An application demonstrating customizing of canonical options
199
+ #
200
+ class App < CommandLine::Application
201
+
202
+ def initialize
203
+ version "0.0.1"
204
+ author "Author Name"
205
+ copyright "Copyright (c) 2005, Jim Freeze"
206
+ short_description "A simple app example that takes two arguments."
207
+ long_description "This app is a simple application example that supports "+
208
+ "three options and two commandline arguments."
209
+
210
+ option :version, :names => %w(--version -v --notice-the-change-from-app5)
211
+ option :debug, :arity => [0,1], :arg_description => "debug_level",
212
+ :opt_description => "Set debug level from 0 to 9."
213
+ option :help
214
+
215
+ args :param_file, :out_file
216
+
217
+ super
218
+ end
219
+
220
+ def main
221
+ puts "main called"
222
+ puts "@param_file = #{@param_file}"
223
+ puts "@out_file = #{@out_file}"
224
+ end
225
+ end#class App
226
+ #---------------------------------------------------
227
+
228
+ % app7.rb -h
229
+ NAME
230
+
231
+ app7.rb - A simple app example that takes two arguments.
232
+
233
+ DESCRIPTION
234
+
235
+ This app is a simple application example that supports three
236
+ options and two commandline arguments.
237
+
238
+ OPTIONS
239
+
240
+ --version,-v,--notice-the-change-from-app5
241
+ Displays application version.
242
+
243
+ --debug,-d debug_level
244
+ Set debug level from 0 to 9.
245
+
246
+ --help,-h
247
+ Displays help page.
248
+
249
+ AUTHOR: Author Name
250
+ Copyright (c) 2005, Jim Freeze
251
+
252
+ TESTS
253
+ =====
254
+ Tests: 49
255
+ Assertions: 215
256
+
257
+
258
+ HISTORY
259
+ =======
260
+ After poking around in a few corporations, it was evident that
261
+ option parsing was not well understood. Therefore, many inhouse
262
+ tools were built that did not conform to any of the POSIX, Gnu or XTools
263
+ option styles. CommandLine::OptionParser was developed so that
264
+ new applications could be written that conformed to accepted standards,
265
+ but non-standard option configurations could be handled as well
266
+ to support legacy interfaces.
267
+
268
+ Once the option parsing was written, there was a need to streamline
269
+ the repetitive tasks in setting up an application. The original
270
+ boilerplate was simple, but after taking a few cues from
271
+ rails, a significant amount of functionality was added to
272
+ Application that make it a very useful tool yet simple to use.
273
+
274
+ More information and usage scenarios on OptionParser can be found at:
275
+ http://rubyforge.org/projects/optionparser/
276
+
277
+ Download & Installation
278
+ =======================
279
+
280
+ Homepage: http://rubyforge.org/projects/optionparser/
281
+ Documentation: http://optionparser.rubyforge.org/
282
+ Download: http://rubyforge.org/frs/?group_id=632&release_id=2345
283
+
284
+ Dependencies:
285
+ * None
286
+
287
+ Currently optionparser is only available as a rubygem.
288
+
289
+ Via RubyGems
290
+ $ gem install -r CommandLine
291
+
292
+ All feedback is appreciated!
293
+
294
+ Installations not yet available
295
+ ===============================
296
+ # not in RPA yet
297
+ Via RPA
298
+ $ rpa install commandline
299
+
300
+ # this either
301
+ The do-it-yourself way
302
+ $ ruby setup.rb config
303
+ $ ruby setup.rb setup
304
+ $ ruby setup.rb install
305
+
306
+ # nor this
307
+ The simplified do-it-yourself way
308
+ $ rake install
309
+
310
+
311
+ RELEASE NOTES
312
+ =============
313
+
314
+ 0.6.0 06/24/2005
315
+ * Refitted and renamed gem to CommandLine
316
+ * Added application class
317
+ * Application is all new with many features - includes features
318
+ suggested from the ARCTAN group - Eric Mahurin, Bassam El Abid
319
+ and Matt Lawrence
320
+ * TODO: Add automatic synopsis generation
321
+ * TODO: Add CVS like parsing
322
+ ---------------------------------------------------------------------
323
+ 0.5.1 06/17/2005
324
+ * Contains all planned features except CVS like command handling
325
+ * Fixed loading path using gems. Is now loaded by:
326
+ require 'rubygems'
327
+ require 'commandline/optionparser'
328
+ * Updated documentation
329
+
330
+ ---------------------------------------------------------------------
331
+ 0.5.0 06/07/2005
332
+ * First public release
333
+
334
+ APPENDIX
335
+ ========
336
+ OPTION PARSER
337
+ =============
338
+ CommandLine is a library for building applications
339
+ and parsing commandlines.
340
+
341
+ CommandLine::OptionParser is part of the CommandLine suite of
342
+ tools and is used for command line parsing. The command line
343
+ parser suite consists of classes CommandLine::Option,
344
+ CommandLine::OptionData and CommandLine::Application.
345
+
346
+ The parser supports POSIX, Gnu and XTools style parsing options.
347
+ It also provides flexibility to support <em>non standard</em>
348
+ options. For example:
349
+
350
+ POSIX
351
+ =====
352
+ OptionParser.new Option.new(:posix, :names => "-f")
353
+
354
+ Gnu
355
+ ===
356
+ OptionParser.new Option.new(:names => %w[--file -f])
357
+
358
+ XTools
359
+ ======
360
+ OptionParser.new Option.new(:names => "-file")
361
+
362
+ User
363
+ ====
364
+ OptionParser.new(Option.new(
365
+ :names => %w(--file -file --files -files -f),
366
+ :arg_arity => [1,-1],
367
+ :arg_description => "file1 [file2, ...]"))
368
+
369
+ This last option prints:
370
+
371
+ OPTIONS
372
+
373
+ --file,-file,--files,-files,-f file1 [file2, ...]
374
+
375
+
376
+ ACKNOWLEDGEMENTS
377
+ ================
378
+ This library contains code from:
379
+ * Austin Ziegler - Text::Format
380
+ * ?? - open4.rb - obtained from codeforthepeople
data/docs/index.html ADDED
@@ -0,0 +1,1005 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
6
+ <title>CommandLine::OptionParser</title>
7
+
8
+ <style type="text/css">
9
+ body,td {
10
+ font-size: small;
11
+ line-height: 130%;
12
+ }
13
+
14
+ a {
15
+ text-decoration: none;
16
+ }
17
+
18
+ a:link, a:visited {
19
+ color: #2050f0;
20
+ }
21
+
22
+ h1, h2, h3, h4, h5, h6 {
23
+ color: #446060;
24
+ }
25
+
26
+ pre {
27
+ #border-left: 7px solid #e8d8d8;
28
+ background: #ffeeff;
29
+ border-top: 2px solid #aaaaaa;
30
+ border-left: 2px solid #aaaaaa;
31
+ }
32
+
33
+ .sidebar {
34
+ font-size: smaller;
35
+ color: #70b0b0;
36
+ }
37
+
38
+ .sidebar a:link {
39
+ color: #104020;
40
+ }
41
+
42
+ .sidebar a:visited {
43
+ color: #104020;
44
+ }
45
+
46
+ .sidebar a:hover {
47
+ color: #401020;
48
+ font-weight: bold;
49
+ }
50
+
51
+ .Sidebarwarning {
52
+ color: #902020;
53
+ padding-left: 1em;
54
+ }
55
+
56
+ .sidebarholder {
57
+ border-top: 2px solid #aaaaaa;
58
+ border-left: 2px solid #aaaaaa;
59
+ padding: 0px;
60
+ margin-bottom: 16px;
61
+ }
62
+
63
+ .sidebartitle {
64
+ background: #c0e0e0;
65
+ padding-left: 8px;
66
+ color: #0000cc;
67
+ }
68
+
69
+ .sidebarbody {
70
+ background: #f8ffff;
71
+ color: #a08080;
72
+ padding-left: 8px;
73
+ }
74
+
75
+ .sidebartext {
76
+ color: #80a0a0;
77
+ }
78
+
79
+ .sidebar TABLE TABLE, .sidebar TABLE TABLE TD {
80
+ color: #a08080;
81
+ padding-right: 0.5em;
82
+ padding-left: 0em;
83
+ padding-top: 0em;
84
+ padding-bottom: 0em;
85
+ }
86
+
87
+ .sidebarsubhead {
88
+ color: #503030;
89
+ background: #f8d0d0;
90
+ }
91
+
92
+ .indent {
93
+ margin-left: 1.5em;
94
+ }
95
+
96
+ .catcount {
97
+ color: #807070;
98
+ }
99
+
100
+ .entry {
101
+ border-top: 2px solid #aaaaaa;
102
+ border-left: 2px solid #aaaaaa;
103
+ padding: 0px;
104
+ }
105
+
106
+ .entrytitlebar {
107
+ #background: #e0c0e0;
108
+ background: #aaaaff;
109
+ }
110
+
111
+ .entrytitle {
112
+ font-family: Arial,Helvetica;
113
+ color: #111166;
114
+ padding-left: 12pt;
115
+ font-size: large;
116
+ font-variant: small-caps;
117
+ }
118
+
119
+ .entrytitledetail {
120
+ text-align: right;
121
+ font-size: x-small;
122
+ padding-right: 12pt;
123
+ }
124
+
125
+ .entrybody {
126
+ padding-left: 36pt;
127
+ padding-right: 12pt;
128
+ padding-bottom: 12pt;
129
+ line-height: 130%;
130
+ #background: #f8f0f0;
131
+ background: #eeeeff;
132
+ }
133
+
134
+ .entrybody h1,h2,h3,h4 {
135
+ line-height: 100%;
136
+ }
137
+
138
+ .pagetitle {
139
+ font-size: xx-large;
140
+ font-family: Arial,Helvetica;
141
+ #text-shadow: .18em .15em .2em #223366;
142
+ text-shadow: .18em .15em .2em #9999cc;
143
+ }
144
+
145
+ .titlemenu {
146
+ font-size: x-small;
147
+ font-family: Arial,Helvetica;
148
+ text-align: right;
149
+ }
150
+
151
+ .schedhead {
152
+ font-size: small;
153
+ font-family: Arial,Helvetica;
154
+ text-align: right;
155
+ font-weight: bold;
156
+ background: #403030;
157
+ color: #c0c0c0;
158
+ }
159
+
160
+ .schedentry {
161
+ font-size: small;
162
+ font-family: Arial,Helvetica;
163
+ text-align: center;
164
+ background: #d0c0c0;
165
+ }
166
+
167
+ .schedempty {
168
+ font-size: small;
169
+ background: #f0e0e0;
170
+ }
171
+
172
+ .sidebartable {
173
+ color: #a08080;
174
+ }
175
+
176
+ .c {
177
+ text-align: right;
178
+ padding-right: 0.5em;
179
+ padding-left: 0em;
180
+ padding-top: 0em;
181
+ padding-bottom: 0em;
182
+ }
183
+ .ctitle {
184
+ text-align: right;
185
+ padding-right: 0.5em;
186
+ padding-left: 0em;
187
+ padding-top: 0em;
188
+ padding-bottom: 0em;
189
+ border-bottom: 1px solid #d0d0d0;
190
+ }
191
+ .ctotal {
192
+ text-align: right;
193
+ padding-right: 0.5em;
194
+ padding-left: 0em;
195
+ padding-top: 0em;
196
+ padding-bottom: 0em;
197
+ border-top: 1px solid #d0d0d0;
198
+ border-bottom: 1px solid #d0d0d0;
199
+ }
200
+
201
+ .caltoday {
202
+ text-align: right;
203
+ background: #f8d0d0;
204
+ }
205
+
206
+ </style>
207
+
208
+ </head>
209
+ <body>
210
+ <table border="0" cellpadding="0" cellspacing="0" width="100%">
211
+ <tr valign="bottom">
212
+ <td class="pagetitle">CommandLine::OptionParser</a></td>
213
+ </tr>
214
+ </table>
215
+ <hr />
216
+ <table>
217
+ <tr valign="top"><td>
218
+
219
+ <table class="entry" border="0" cellspacing="0" width="100%">
220
+ <tr class="entrybody"><td colspan="3" class="entrybody">
221
+ <h1>Welcome to OptionParser</h1>
222
+ <ul>
223
+ <li>Copyright &#169; 2005 Jim Freeze
224
+
225
+ </li>
226
+ <li>Author: Jim Freeze
227
+
228
+ </li>
229
+ <li><a href="http://www.freeze.org/ruby/optionparser/license.txt">License</a>
230
+ </li>
231
+ <li><a href="http://rubyforge.org/projects/optionparser/">Project Page</a>
232
+ </li>
233
+ <li><a href="http://rubyforge.org/frs/?group_id=632&release_id=2345">Download</a>
234
+ </li>
235
+ </ul>
236
+ <tt>OptionParser</tt> is
237
+ designed to be a flexible command line parser with a Ruby look and feel to
238
+ it. <tt>OptionParser</tt> got
239
+ its birth from the need for a parser that is standards compliant, yet
240
+ flexible. <tt>OptionParser</tt>
241
+ supports the standard command line styles of <tt>Unix</tt>, <tt>Gnu</tt>
242
+ and <tt>X Toolkit</tt>, but also lets you break those rules.
243
+
244
+ <p>
245
+ <tt>OptionParser</tt> is
246
+ not a port of a traditional command line parser, but it is written to meet
247
+ the feature requirements of traditional command line parsers. When using it
248
+ as a library, you should notice that it is expressive, supports
249
+ Ruby&#8217;s blocks and lambda&#8217;s, and is sprinkled with a little bit
250
+ of magic.
251
+ </p>
252
+ <p>
253
+ While the library can be used by itself, it is also designed to work with
254
+ the <tt>CommandLine::Application</tt> class. These tools work together to
255
+ facilitate the generation of a sophisticated (batch oriented) application
256
+ user interface in a matter of minutes.
257
+ </p>
258
+ <p>
259
+ If you need a refresher on the traditional option parsing schemes, see
260
+ &quot;Traditional Option Parsing Schemes&quot; below.
261
+ </p>
262
+ <h1>Jumping Right In</h1>
263
+ <h1>OptionParser Usage</h1>
264
+ <p>
265
+ The OptionParser
266
+ library consists of three classes, <tt>Option</tt>, <tt>OptionParser</tt> and
267
+ <tt>OptionData</tt>. For each option an <tt>Option</tt> object is created.
268
+ When you are ready to prepare for command line parsing, these options are
269
+ collected into an array and fed to <tt>OptionParser</tt>.
270
+ This <tt>OptionParser</tt>
271
+ object controls the type of option scheme that is implemented. When it
272
+ comes time to parse a command line, call the method <tt>Option#parse</tt>.
273
+ This will parse any array, but parses ARGV by default. The result is an
274
+ <tt>OptionData</tt> object. This object can be used from which to extract
275
+ values or it can be passed to another class as a fully encapsulated data
276
+ object.
277
+ </p>
278
+ <h2>Getting Started</h2>
279
+ <h3>Installing</h3>
280
+ <pre>
281
+ gem install -r OptionParser
282
+ </pre>
283
+ <h3>Loading the library</h3>
284
+ <pre>
285
+ require 'rubygems'
286
+ require 'commandline/optionparser'
287
+ include CommandLine
288
+ </pre>
289
+ <h3>Using Option Parser</h3>
290
+ <p>
291
+ An option is created with the following syntax:
292
+ </p>
293
+ <pre>
294
+ opt = Option.new([options], &lt;properties&gt;)
295
+ </pre>
296
+ <p>
297
+ The options can be <tt>:flag</tt> or <tt>:posix</tt>. <tt>:flag</tt> means
298
+ that the option is a mode flag and does not take any arguments.
299
+ <tt>:posix</tt> means that <tt>Option</tt> will validate the properties to
300
+ ensure they are posix compliant.
301
+ </p>
302
+ <p>
303
+ An option object has six properties. Four of these properties define
304
+ attributes of the object. The last two define <em>actions</em> that are
305
+ taken when a command line is parsed.
306
+ </p>
307
+ <ol>
308
+ <li>:names
309
+
310
+ </li>
311
+ <li>:arg_arity
312
+
313
+ </li>
314
+ <li>:opt_description
315
+
316
+ </li>
317
+ <li>:arg_description
318
+
319
+ </li>
320
+ <li>:opt_found
321
+
322
+ </li>
323
+ <li>:opt_not_found
324
+
325
+ </li>
326
+ </ol>
327
+ <p>
328
+ It is not necessary to set values for all of these properties. Some are set
329
+ automatically, as we&#8217;ll see below.
330
+ </p>
331
+ <h3>Posix</h3>
332
+ <p>
333
+ The default <tt>Option</tt> object is non-posix.
334
+ </p>
335
+ <pre>
336
+ op1 = OptionParser.new(:posix, opts)
337
+ op2 = OptionParser.new(opts)
338
+ op1.posix #=&gt; true
339
+ op2.posix #=&gt; false
340
+ </pre>
341
+ <h3>Mode-Flag</h3>
342
+ <p>
343
+ To create a mode flag, that is, an option that is either true or false
344
+ depending if it is seen on the command line or not, we could write:
345
+ </p>
346
+ <pre>
347
+ opt_debug = Option.new(
348
+ :names =&gt; %w(--debug -d), # the flag has two names
349
+ :arg_arity =&gt; [0,0], # this says take no arugments
350
+ :opt_description =&gt; &quot;Sets debug to true&quot;,
351
+ :arg_description =&gt; &quot;&quot;,
352
+ :opt_found =&gt; true, # true if seen on command line
353
+ :opt_not_found =&gt; false # false if not seen on command line
354
+ )
355
+ </pre>
356
+ <p>
357
+ Now, this is a lot of work just for a common mode-flag. However, there is a
358
+ shorter way:
359
+ </p>
360
+ <pre>
361
+ opt = Option.new(:flag, :names =&gt; %w(--debug -d))
362
+ </pre>
363
+ <p>
364
+ When <tt>Option</tt> sees the :flag option, it makes some assignments
365
+ behind the scenes and what you are left with is:
366
+ </p>
367
+ <pre>
368
+ :names =&gt; [&quot;--debug&quot;, &quot;-d&quot;]
369
+ :arg_arity =&gt; [0, 0]
370
+ :opt_description =&gt; &quot;Sets debug to true.&quot; # debug is taken from the first name
371
+ :arg_description =&gt; &quot;&quot;
372
+ :opt_found =&gt; true
373
+ :opt_not_found =&gt; false
374
+ </pre>
375
+ <p>
376
+ For a common option like a mode-flag, <tt>Option</tt> will use the first
377
+ option &#8216;word&#8217; it finds in the :names list and use that in the
378
+ automatic option text. Of course, if you don&#8217;t want any text, just
379
+ set the option description to an empty string:
380
+ </p>
381
+ <pre>
382
+ :opt_description =&gt; &quot;&quot;.
383
+ </pre>
384
+ <h3>Option Arguments</h3>
385
+ <p>
386
+ If an option is not a mode flag, then it takes arguments. Most option
387
+ parsers only permit a single argument per option flag. If your application
388
+ needs multiple arguments, the standard method is just to repeat the option
389
+ multiple times, once for each required argument. For example, if I need to
390
+ pass two files to an application I would need something like:
391
+ </p>
392
+ <pre>
393
+ myapp -f file1 -f file2
394
+ </pre>
395
+ <p>
396
+ But, it would be cleaner if the command line could be expressed as:
397
+ </p>
398
+ <pre>
399
+ myapp -f file1 file2
400
+ </pre>
401
+ <p>
402
+ Well, no longer do you have to suffer with thirty-year old option parser
403
+ technology. <tt>OptionParser</tt>
404
+ permits multiple arguments per option flag and the number of arguments can
405
+ be defined to be variable.
406
+ </p>
407
+ <p>
408
+ To define an option that takes 1 or more arguments, the following can be
409
+ done:
410
+ </p>
411
+ <pre>
412
+ opt = Option.new(:names =&gt; &quot;--file&quot;, :arg_arity =&gt; [1,-1])
413
+ </pre>
414
+ <p>
415
+ Let&#8217;s say the option required at least two arguments, but not more
416
+ than five. This is defined with:
417
+ </p>
418
+ <pre>
419
+ opt = Option.new(:names =&gt; &quot;--file&quot;, :arg_arity =&gt; [2,5])
420
+ OptionParser.new(opt).parse
421
+
422
+ % myapp --file file1 # exception raised
423
+ % myapp --file file1 file2 # ok
424
+ % myapp --file file1 file2 file3 # ok
425
+ % myapp --file f1 f2 f3 f4 f5 f6 # f6 remains on the command line
426
+ </pre>
427
+ <p>
428
+ This ability is handy on occassions where an option argument is
429
+ &#8216;optional&#8217;.
430
+ </p>
431
+ <pre>
432
+ myapp --custom # no args, uses $HOME/.myapprc
433
+ myapp --custom my_custom_file # uses my_custom_file
434
+ </pre>
435
+ <p>
436
+ This type of option is defined by:
437
+ </p>
438
+ <pre>
439
+ opt = Option.new(:names =&gt; &quot;--custom&quot;, :arg_arity =&gt; [0,1])
440
+ </pre>
441
+ <p>
442
+ If the <tt>:arg_arity</tt> is not satisfied, an exception is raised.
443
+ </p>
444
+ <h3>Actions</h3>
445
+ <p>
446
+ The option properties <tt>:opt_found</tt> and <tt>:opt_not_found</tt> are
447
+ the source of the value returned for an option when it is parsed. These
448
+ properties can be either an object or a proc/lambda. If they are an object,
449
+ then the stored object is simply returned. If they are lambdas, then the
450
+ stored value is the return value of the proc/lambda. So, the following will
451
+ have the same result:
452
+ </p>
453
+ <pre>
454
+ opt_debug = Option.new(:flag
455
+ :names =&gt; %w(--debug -d),
456
+ :opt_found =&gt; true,
457
+ :opt_not_found =&gt; false
458
+ )
459
+
460
+ opt_debug = Option.new(:flag
461
+ :names =&gt; %w(--debug -d),
462
+ :opt_found =&gt; lambda { true },
463
+ :opt_not_found =&gt; lambda { false }
464
+ )
465
+ </pre>
466
+ <p>
467
+ Notice that there is no need to set an instance variable to a default
468
+ value. Normally one does:
469
+ </p>
470
+ <pre>
471
+ @debug = false
472
+ # option setup
473
+ ... parse the commandline
474
+ @debug = true if parse_results[&quot;--debug&quot;]
475
+ </pre>
476
+ <p>
477
+ But with <tt>OptionParser</tt>, one
478
+ has the capability of doing the following:
479
+ </p>
480
+ <pre>
481
+ opt_debug = Option.new(:flag, :names =&gt; %w(--debug -d))
482
+ ... parse the commandline
483
+ @debug = option_data[:debug] # value is set without need for default
484
+
485
+ # or
486
+
487
+ opt_debug = Option.new(:flag
488
+ :names =&gt; %w(--debug -d),
489
+ :opt_found =&gt; lambda { @debug = true },
490
+ :opt_not_found =&gt; lambda { @debug = false }
491
+ )
492
+ # do nothing, variable already set.
493
+ </pre>
494
+ <p>
495
+ I find this much easier to manage than having to worry about setting
496
+ default behaviour. Now that we know how to create options, let&#8217;s move
497
+ on to the commandline parser.
498
+ </p>
499
+ <h2>OptionParser</h2>
500
+ <p>
501
+ Once the options are defined, we load them into an <tt>OptionParser</tt> and
502
+ parse the command line. The syntax for creating an <tt>OptionParser</tt>
503
+ object is:
504
+ </p>
505
+ <pre>
506
+ OptionParser.new(prop_flags, option)
507
+ OptionParser.new(prop_flags, [options])
508
+ OptionParser.new(option)
509
+ OptionParser.new([options])
510
+ </pre>
511
+ <p>
512
+ where the possible property flags are:
513
+ </p>
514
+ <pre>
515
+ :posix
516
+ :unknown_options_action =&gt; :collect | :ignore | :raise
517
+ </pre>
518
+ <p>
519
+ If you want to parse posix, you must specify so. <tt>OptionParser</tt> will
520
+ not assume posix mode just because all of the options are posix options.
521
+ This allows you to use posix only options but not require the strict
522
+ parsing rules.
523
+ </p>
524
+ <p>
525
+ Below are a few examples of creating an <tt>OptionParser</tt>
526
+ object:
527
+ </p>
528
+ <pre>
529
+ opt = Option.new(:flag, :names =&gt; %w(-h))
530
+ op1 = OptionParser.new(:posix, opt)
531
+ op2 = OptionParser.new(opt)
532
+ </pre>
533
+ <p>
534
+ or
535
+ </p>
536
+ <pre>
537
+ opts = []
538
+ opts &lt;&lt; Option.new(:flag, :names =&gt; %w(--help h))
539
+ opts &lt;&lt; Option.new(:flag, :names =&gt; %w(--debug d))
540
+ </pre>
541
+ <p>
542
+ Options may be added to an <tt>OptionParser</tt> by
543
+ three different methods:
544
+ </p>
545
+ <pre>
546
+ # Options added as arguments during OptionParser construction
547
+ op = OptionParser.new(opt1, opt2)
548
+ op = OptionParser.new([opt1, opt2])
549
+ </pre>
550
+ <p>
551
+ or
552
+ </p>
553
+ <pre>
554
+ # Options added in a block constructor
555
+ op = OptionParser.new { |o| o &lt;&lt; opts }
556
+ </pre>
557
+ <p>
558
+ or
559
+ </p>
560
+ <pre>
561
+ # Options added to an existing OptionParser object
562
+ op = OptionParser.new
563
+ op &lt;&lt; opts
564
+ </pre>
565
+ <h3>Parsing the Command Line</h3>
566
+ <p>
567
+ Parsing the command line is as simple as calling <tt>#parse</tt>:
568
+ </p>
569
+ <pre>
570
+ option_data = op.parse
571
+ </pre>
572
+ <h3>Printing an Option Summary</h3>
573
+ <p>
574
+ A <tt>OptionParser</tt> with
575
+ a complete set of options added to it defines the human interface that your
576
+ application presents to a user. Therefore, the parser should be able to
577
+ provide a nicely formatted summary for the user.
578
+ </p>
579
+ <p>
580
+ An example is shown below with its corresponding output:
581
+ </p>
582
+ <pre>
583
+ require 'rubygems'
584
+ require 'commandline/optionparser'
585
+ include CommandLine
586
+ puts OptionParser.new { |o|
587
+ o &lt;&lt; Option.new(:flag, :names =&gt; %w[--debug -d])
588
+ o &lt;&lt; Option.new(:flag, :names =&gt; %w[--help -h],
589
+ :opt_description =&gt; &quot;Prints this page.&quot;)
590
+ o &lt;&lt; Option.new(:names =&gt; %w[--ouput -o],
591
+ :opt_description =&gt; &quot;Defines the output file.&quot;,
592
+ :arg_description =&gt; &quot;output_file&quot;)
593
+ o &lt;&lt; Option.new(:names =&gt; %w[--a-long-opt --with-many-names -a -A],
594
+ :arg_arity =&gt; [2,-1],
595
+ :opt_description =&gt; &quot;Your really long description here.&quot;,
596
+ :arg_description =&gt; &quot;file1 file2 [file3 ...]&quot;)
597
+ }.to_s
598
+ </pre>
599
+ <p>
600
+ Generates the output:
601
+ </p>
602
+ <pre>
603
+ OPTIONS
604
+
605
+ --debug,-d
606
+ Sets debug to true.
607
+
608
+ --help,-h
609
+ Prints this page.
610
+
611
+ --ouput,-o output_file
612
+ Defines the output file.
613
+
614
+ --a-long-opt,--with-many-names,-a,-A file1 file2 [file3 ...]
615
+ Your really long description here.
616
+ </pre>
617
+ <h2>Option Data</h2>
618
+ <p>
619
+ The <tt>OptionData</tt> is the return value of <tt>OptionParser#parse</tt>.
620
+ The parsing results for each option are accessed with the bracket notation
621
+ #[].
622
+ </p>
623
+ <pre>
624
+ opt = Option.new(:posix,
625
+ :names =&gt; %w(-r),
626
+ :opt_found =&gt; OptionParser::GET_ARGS)
627
+ od = OptionParser.new(:posix, opt).parse([&quot;-rubygems&quot;])
628
+ od[&quot;-r&quot;] #=&gt; &quot;ubygems&quot;
629
+
630
+ od = OptionParser.new(:posix, opt).parse([&quot;-r&quot;, &quot;ubygems&quot;])
631
+ od[&quot;-r&quot;] #=&gt; &quot;ubygems&quot;
632
+ </pre>
633
+ <p>
634
+ <tt>OptionData</tt> behaves similar to a hash object in that the parsed
635
+ option data is accessed with #[] where the key is the first item in the
636
+ :names array of each option. An option cannot access its parsed values
637
+ using just any of its names.
638
+ </p>
639
+ <pre>
640
+ od = OptionParser.new { |o|
641
+ o &lt;&lt; Option.new(:flag, :names =&gt; %w(--valid --notvalid))
642
+ o &lt;&lt; Option.new(:flag, :names =&gt; %w(--first --second))
643
+ }.parse(%w(--notvalid --second))
644
+ od[&quot;--valid&quot;] #=&gt; true
645
+ od[&quot;--first&quot;] #=&gt; true
646
+ od[&quot;--notvalid&quot;] #=&gt; CommandLine::OptionData::UnknownOptionError
647
+ od[&quot;--second&quot;] #=&gt; CommandLine::OptionData::UnknownOptionError
648
+ </pre>
649
+ <h3>Built-in Data Handlers</h3>
650
+ <p>
651
+ OptionParser has
652
+ built-in data handlers for handling common scenarios. These lambdas can
653
+ save a lot of typing.
654
+ </p>
655
+ <h3>GET_ARG_ARRAY</h3>
656
+ <p>
657
+ This is useful for options that take a variable number of arguments. It
658
+ returns all the arguments in an array.
659
+ </p>
660
+ <pre>
661
+ # GET_ARG_ARRAY returns all arguments in an array, even if no
662
+ # arguments are present. This is not to be confused with the option
663
+ # occuring multiple times on the command line.
664
+ opt = Option.new(:names =&gt; %w(--file),
665
+ :argument_arity =&gt; [0,-1],
666
+ :opt_found =&gt; OptionParser::GET_ARG_ARRAY)
667
+ #:opt_found =&gt; :collect) # would this be better?
668
+ od = OptionParser.new(opt).parse(%w(--file))
669
+ od[&quot;--file&quot;] #=&gt; []
670
+ od = OptionParser.new(opt).parse(%w(--file=file))
671
+ od[&quot;--file&quot;] #=&gt; [&quot;file&quot;]
672
+ od = OptionParser.new(opt).parse(%w(--file=file1 --file file2))
673
+ od[&quot;--file&quot;] #=&gt; [&quot;file2&quot;]
674
+ od = OptionParser.new(opt).parse(%w(--file=file1 file2))
675
+ od[&quot;--file&quot;] #=&gt; [&quot;file1&quot;, &quot;file2&quot;]
676
+ od = OptionParser.new(opt).parse(%w(--file file1 file2))
677
+ od[&quot;--file&quot;] #=&gt; [&quot;file1&quot;, &quot;file2&quot;]
678
+ </pre>
679
+ <h3>GET_ARGS</h3>
680
+ <p>
681
+ This is a &#8216;smart&#8217; option getter. If no arguments are found, it
682
+ returns true. If a single argument is found, it returns that argument. If
683
+ more than one argument is found, it returns an array of those arguments.
684
+ </p>
685
+ <pre>
686
+ opt = Option.new(:names =&gt; %w(--file),
687
+ :argument_arity =&gt; [0,-1],
688
+ :opt_found =&gt; OptionParser::GET_ARGS)
689
+ #:opt_found =&gt; :smart_collect) # would this be better?
690
+ od = OptionParser.new(opt).parse(%w(--file))
691
+ od[&quot;--file&quot;] #=&gt; true
692
+ od = OptionParser.new(opt).parse(%w(--file=file))
693
+ od[&quot;--file&quot;] #=&gt; &quot;file&quot;
694
+ od = OptionParser.new(opt).parse(%w(--file=file1 --file file2))
695
+ od[&quot;--file&quot;] #=&gt; &quot;file2&quot;
696
+ od = OptionParser.new(opt).parse(%w(--file=file1 file2))
697
+ od[&quot;--file&quot;] #=&gt; [&quot;file1&quot;, &quot;file2&quot;]
698
+ od = OptionParser.new(opt).parse(%w(--file file1 file2))
699
+ od[&quot;--file&quot;] #=&gt; [&quot;file1&quot;, &quot;file2&quot;]
700
+ </pre>
701
+ <p>
702
+ And, for those oxymoronic non-optional options:
703
+ </p>
704
+ <pre>
705
+ opt = Option.new(:names =&gt; %w(--not-really-an-option),
706
+ :opt_not_found =&gt; OptionParser::OPT_NOT_FOUND_BUT_REQUIRED
707
+ )
708
+ OptionParser.new(opt).parse([]) #=&gt; OptionParser::MissingRequiredOptionError
709
+ </pre>
710
+ <h3><tt>OptionData</tt></h3>
711
+ <p>
712
+ We have just shown that after parsing a command line, the result of each
713
+ option is found from OptionData. The values that remain on the command line
714
+ are assigned to <tt>args</tt>. Other attributes of <tt>OptionData</tt> are:
715
+ </p>
716
+ <pre>
717
+ od.argv # the original command line
718
+ od.unknown_options # If OptionParser was told to :collect unknown options
719
+ od.args # arguments not claimed by any option
720
+ od.not_parsed # arguments following a '--' on the command line
721
+ od.cmd # not yet implemented - but a cvs like command
722
+ </pre>
723
+ <hr size="2"></hr><h1>Traditional Option Parsing Schemes</h1>
724
+ <p>
725
+ This section is a brief overview of traditional command line parsing.
726
+ </p>
727
+ <p>
728
+ Command line options traditionally occur in three flavors:
729
+ </p>
730
+ <ul>
731
+ <li><em>Unix</em> (or POSIX.2)
732
+
733
+ </li>
734
+ <li><em>Gnu</em>
735
+
736
+ </li>
737
+ <li><em>X Toolkit</em>
738
+
739
+ </li>
740
+ </ul>
741
+ <p>
742
+ Below is a summary of these schemes. <em>(Note: I did not invent these
743
+ traditional parsing conventions. Most of the information contained below
744
+ was pulled from internet resources and I have quoted these resources where
745
+ possible.)</em>
746
+ </p>
747
+ <h2>Unix Style (POSIX)</h2>
748
+ <p>
749
+ The Unix style command line options are a single character preceded by a
750
+ single dash (hyphen character). In general, lowercase options are preferred
751
+ with their uppercase counterparts being the special case variant.
752
+ </p>
753
+ <h3>Mode Flag</h3>
754
+ <p>
755
+ If an option does not take an argument, then it is a mode-flag.
756
+ </p>
757
+ <h3>Optional Separation Between the Option Flag and Its Argument</h3>
758
+ <p>
759
+ If the option takes an argument, the argument follows it with optional
760
+ white space separating the two. For example, the following forms are both
761
+ valid:
762
+ </p>
763
+ <pre>
764
+ sort -k 5
765
+ sort -k5
766
+ </pre>
767
+ <h3>Grouping</h3>
768
+ <p>
769
+ A mode-flag can be grouped together with other mode-flags behind a single
770
+ dash. For example:
771
+ </p>
772
+ <pre>
773
+ tar -c -v -f
774
+ </pre>
775
+ <p>
776
+ is equivalent to:
777
+ </p>
778
+ <pre>
779
+ tar -cvf
780
+ </pre>
781
+ <p>
782
+ If grouping is done, the last option in a group can be an option that takes
783
+ an argument. For example
784
+ </p>
785
+ <pre>
786
+ sort -r -n -k 5
787
+ </pre>
788
+ <p>
789
+ can be written as
790
+ </p>
791
+ <pre>
792
+ sort -rnk 5
793
+ </pre>
794
+ <p>
795
+ but not
796
+ </p>
797
+ <pre>
798
+ sort -rkn 5
799
+ </pre>
800
+ <p>
801
+ because the &#8216;5&#8217; argument belongs to the &#8216;k&#8217; option
802
+ flag.
803
+ </p>
804
+ <h3>Option Parsing Termination</h3>
805
+ <p>
806
+ It is convention that a double hyphen is a signal to stop option
807
+ interpretation and to read the remaining statements on the command line
808
+ literally. So, a command such as:
809
+ </p>
810
+ <pre>
811
+ app -- -x -y -z
812
+ </pre>
813
+ <p>
814
+ will not &#8216;see&#8217; the three mode-flags. Instead, they will be
815
+ treated as arguments to the application:
816
+ </p>
817
+ <pre>
818
+ #args = [&quot;-x&quot;, &quot;-y&quot;, &quot;-z&quot;]
819
+ </pre>
820
+ <h3>POSIX Summary</h3>
821
+ <ol>
822
+ <li>An option is a hyphen followed by a single alphanumeric character.
823
+
824
+ </li>
825
+ <li>An option may require an argument which must follow the option with an
826
+ optional space in between.
827
+
828
+ <pre>
829
+ -r ubygems
830
+ -rubygems
831
+ -r=ubygems # not ok. '=' is Gnu style
832
+ </pre>
833
+ </li>
834
+ <li>Options that do not require arguments can be grouped after a hyphen.
835
+
836
+ </li>
837
+ <li>Options can appear in any order.
838
+
839
+ </li>
840
+ <li>Options can appear multiple times.
841
+
842
+ </li>
843
+ <li>Options precede other nonoption arguments. TODO: Test for this
844
+
845
+ </li>
846
+ <li>The &#8212; argument terminates options.
847
+
848
+ </li>
849
+ <li>The - option is used to represent the standard input stream.
850
+
851
+ </li>
852
+ </ol>
853
+ <h3>References</h3>
854
+ <p>
855
+ <a
856
+ href="http://www.mkssoftware.com/docs/man1/getopts.1.asp">www.mkssoftware.com/docs/man1/getopts.1.asp</a>
857
+ </p>
858
+ <h2>Gnu Style</h2>
859
+ <p>
860
+ The Gnu style command line options provide support for option words (or
861
+ keywords), yet still maintain compatibility with the Unix style options.
862
+ The options in this style are sometimes referred to as
863
+ <em>long_options</em> and the Unix style options as <em>short_options</em>.
864
+ The compatibility is maintained by preceding the <em>long_options</em> with
865
+ two dashes. The option word must be two or more characters.
866
+ </p>
867
+ <h3>Separation Between the Option Flag and Its Argument</h3>
868
+ <p>
869
+ Gnu style options cannot be grouped. For options that have an argument, the
870
+ argument follows the option with either whitespace or an &#8217;=&#8217;.
871
+ For example, the following are equivalent:
872
+ </p>
873
+ <pre>
874
+ app --with-optimizer yes
875
+ app --with-optimizer=yes
876
+ </pre>
877
+ <h3>Option Parsing Termination</h3>
878
+ <p>
879
+ Similar to the <em>Unix</em> style double-hyphen &#8217;- -&#8217;, the
880
+ <em>Gnu</em> style has a triple-hyphen &#8217;- - -&#8217; to signal that
881
+ option parsing be halted and to treat the remaining text as arguments (that
882
+ is, read literally from the command line)
883
+ </p>
884
+ <pre>
885
+ app --- -x -y -z
886
+ args = [&quot;-x&quot;, &quot;-y&quot;, &quot;-z&quot;]
887
+ </pre>
888
+ <h3>Mixing <em>Gnu</em> and <em>Unix</em> Styles</h3>
889
+ <p>
890
+ The <em>Gnu</em> and the <em>Unix</em> option types can be mixed on the
891
+ same commandline. The following are equivalent:
892
+ </p>
893
+ <pre>
894
+ app -a -b --with-c
895
+ app -ab --with-c
896
+ app -ba --with-c
897
+ app --with-c -ab
898
+ </pre>
899
+ <h2>X Toolkit Style</h2>
900
+ <p>
901
+ The X Toolkit style uses the single hyphen followed by a keyword option.
902
+ This style is not compatible with the <em>Unix</em> or the <em>Gnu</em>
903
+ option types. In most situations this is OK since these options will be
904
+ filtered from the command line before passing them to an application.
905
+ </p>
906
+ <h3>&#8217;-&#8217; and STDIN</h3>
907
+ <p>
908
+ It is convention that a bare hypen indicates to read from stdin.
909
+ </p>
910
+ <h2>The OptionParser Style</h2>
911
+ <p>
912
+ The CommandLine::OptionParser does not
913
+ care what style you use. It is designed for maximum flexiblity so it may be
914
+ used within any organiziation to meet their standards.
915
+ </p>
916
+ <h3>Multiple Option Names</h3>
917
+ <p> OptionParser does
918
+ not place restrictions on the number of options. The only restriction is
919
+ that an option name begin with a hyphen &#8217;-&#8217;. A definitely
920
+ conjured example of this freedom is:
921
+ </p>
922
+ <pre>
923
+ :names =&gt; %w(
924
+ --file --File --f --F -file -File -f -F
925
+ )
926
+ </pre>
927
+ <h3>Prefix Matching</h3>
928
+ <p>
929
+ Although not encouraged, some prefer the ability to truncate option words
930
+ to their first unique match. For example, an application that support this
931
+ style and accepts the following two option words:
932
+ </p>
933
+ <pre>
934
+ [&quot;--foos&quot;, &quot;--fbars&quot;]
935
+ </pre>
936
+ <p>
937
+ will accept any of the following as valid options
938
+ </p>
939
+ <pre>
940
+ app --fo
941
+ app --foo
942
+ app --foos
943
+ </pre>
944
+ <p>
945
+ for the &quot;&#8212;foos&quot; option flag since it can be determined that
946
+ &quot;&#8212;fo&quot; will only match &quot;&#8212;foos&quot; and not
947
+ &quot;&#8212;fbars&quot;.
948
+ </p>
949
+ <h3>Repeated Arguments</h3>
950
+ <p>
951
+ A common question is how an option parser should respond when an option is
952
+ specified on the command line multiple times. This is true for mode flags,
953
+ but especially true for options that require an argument, For example, what
954
+ should happen when the following is given:
955
+ </p>
956
+ <pre>
957
+ app -f file1 -f file2
958
+ </pre>
959
+ <p>
960
+ Should the parser flag this as an error or should it accept both arguments.
961
+ </p>
962
+ <p> OptionParser gives
963
+ you the choice of whether it raises an exception when an option is seen
964
+ more than once, or it just passes the data onto the user.
965
+ </p>
966
+ <p>
967
+ How the data is handled is up to the user, but it typically boils down to
968
+ either Append, Replace or Raise. This is described in more detail in the
969
+ usage section.
970
+ </p>
971
+ <h2>CVS Mode</h2>
972
+ <p>
973
+ CVS is a common application with a unique command line structure. The cvs
974
+ application commandline can be given options, but requires a command. This
975
+ command can also be given options. This means that there are two sets of
976
+ options, one set for the cvs application and one set for the cvs-command.
977
+ Some example formats are:
978
+ </p>
979
+ <pre>
980
+ cvs [cvs-options]
981
+ cvs [cvs-options] command [command-options-and-arguments]
982
+
983
+ cvs -r update
984
+ cvs -r update .
985
+ cvs edit -p file
986
+ </pre>
987
+ <p>
988
+ To handle this, the first unclaimed argument is treated as a command and
989
+ the options and option-arguments that follow belong to that command. More
990
+ on how this is handled in the usage section.
991
+ </p>
992
+ <h2>Option Grouping</h2>
993
+ <p>
994
+ A conflict can occur where a grouping of single letter Unix options has the
995
+ value as a word option preceded by a single dash. For this reason, it is
996
+ customary to use the double-dash notation for word options. Unless
997
+ double-dashes are enforced for word options, OptionParser will
998
+ check for possible name conflicts and raise an exception if it finds one.
999
+ </p>
1000
+
1001
+ </td></tr>
1002
+ </table>
1003
+ </table>
1004
+ </body>
1005
+ </html>