commandline 0.7.9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +40 -0
- data/LICENSE +31 -0
- data/README +325 -0
- data/docs/index.html +1005 -0
- data/docs/posted-docs.index.html +1410 -0
- data/docs/tmp/app1.rb +11 -0
- data/docs/tmp/app1b.rb +15 -0
- data/docs/tmp/app2.rb +17 -0
- data/docs/tmp/app_exit.rb +16 -0
- data/docs/tmp/app_file.rb +24 -0
- data/docs/tmp/app_flag.rb +12 -0
- data/docs/tmp/app_replay.rb +19 -0
- data/lib/commandline.rb +16 -0
- data/lib/commandline/application.rb +437 -0
- data/lib/commandline/kernel.rb +17 -0
- data/lib/commandline/optionparser.rb +16 -0
- data/lib/commandline/optionparser/option.rb +185 -0
- data/lib/commandline/optionparser/optiondata.rb +75 -0
- data/lib/commandline/optionparser/optionparser.rb +518 -0
- data/lib/commandline/utils.rb +12 -0
- data/lib/open4.rb +79 -0
- data/lib/test/unit/systemtest.rb +77 -0
- metadata +77 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
== 0.7.9 11/05/2005
|
2
|
+
=== Additions
|
3
|
+
- Renamed gem to lowercase commandline
|
4
|
+
- Added replay command options
|
5
|
+
- Added CommandLine::Application_wo_AutoRun - no auto run set thru at_exit
|
6
|
+
- Added documentation for CommandLine::Application - instead of just README
|
7
|
+
- Changed :arg_arity to :arity in Option
|
8
|
+
- Add :required for use with :opt_found
|
9
|
+
- Added args accessor for @args - suggested by Esteban Manchado Velázquez
|
10
|
+
- Added opt() accessor for @option_data[]
|
11
|
+
|
12
|
+
== 0.7.6
|
13
|
+
=== Additions
|
14
|
+
- Kernel::debug
|
15
|
+
- Add :expected_arguments
|
16
|
+
|
17
|
+
== 0.7.5
|
18
|
+
=== Bug Fixes
|
19
|
+
- Fixed @arg_arity bug
|
20
|
+
- Fixed copyright print bug
|
21
|
+
- Remove need for super
|
22
|
+
|
23
|
+
== 0.6.0 06/24/2005
|
24
|
+
- Refitted and renamed gem to CommandLine
|
25
|
+
- Added application class
|
26
|
+
- Application is all new with many features - includes features
|
27
|
+
suggested from the ARCTAN group - Eric Mahurin, Bassam El Abid
|
28
|
+
and Matt Lawrence
|
29
|
+
- TODO: Add automatic synopsis generation
|
30
|
+
- TODO: Add CVS like parsing
|
31
|
+
|
32
|
+
== 0.5.1 06/17/2005
|
33
|
+
- Contains all planned features except CVS like command handling
|
34
|
+
- Fixed loading path using gems. Is now loaded by:
|
35
|
+
require 'rubygems'
|
36
|
+
require 'commandline/optionparser'
|
37
|
+
- Updated documentation
|
38
|
+
|
39
|
+
== 0.5.0 06/07/2005
|
40
|
+
- First public release
|
data/LICENSE
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
COMMANDLINE 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,325 @@
|
|
1
|
+
CommandLine - Application and OptionParser
|
2
|
+
==========================================
|
3
|
+
Author: Jim Freeze
|
4
|
+
Copyright 2005 Jim Freeze
|
5
|
+
|
6
|
+
DOCS: http://rubyforge.org/docman/view.php/632/232/posted-docs.index.html
|
7
|
+
|
8
|
+
Welcome to CommandLine
|
9
|
+
|
10
|
+
CommandLine is a library that greatly simplifies the repetitive process of building a command line user interface for your applications. It's 'ruby-like' usage style streamlines application development so that even applications with numerous configuration options can be quickly put together. CommandLine automatically builds friendly usage and help screens that are nicely formatted for the user. No longer is starting an application a pain where you have to copy boiler plate code (or a previous application) and retype repetitive code to get an application started.
|
11
|
+
|
12
|
+
CommandLine smartly handles the arguments passed on the commandline. For example, if your application accepts arguments, and none are given, it prints a usage statement. But, if your application accepts no arguments, CommandLine will happily run your application. CommandLine also handles a complex set of options through the OptionParser library, which is described below.
|
13
|
+
|
14
|
+
OptionParser is designed to be a flexible command line parser with a Ruby look and feel to it. OptionParser got its birth from the need for a parser that is standards compliant, yet flexible. OptionParser supports the standard command line styles of Unix, Gnu and X Toolkit, but also lets you break those rules.
|
15
|
+
|
16
|
+
OptionParser is not a port of a traditional command line parser, but it is written to meet the feature requirements of traditional command line parsers. When using it as a library, you should notice that it is expressive, supports Ruby’s blocks and lambda’s, and is sprinkled with a little bit of magic.
|
17
|
+
|
18
|
+
While the library can be used by itself, it is also designed to work with the CommandLine::Application class. These tools work together to facilitate the generation of a sophisticated (batch oriented) application user interface in a matter of minutes.
|
19
|
+
|
20
|
+
If you need a refresher on the traditional option parsing schemes, see "Traditional Option Parsing Schemes" below.
|
21
|
+
|
22
|
+
|
23
|
+
EXAMPLES
|
24
|
+
========
|
25
|
+
|
26
|
+
Probably the best way to describe how the tool works is
|
27
|
+
with some examples:
|
28
|
+
|
29
|
+
% cat app.rb
|
30
|
+
#---------------------------------------------------
|
31
|
+
#!/usr/bin/env ruby
|
32
|
+
|
33
|
+
require 'rubygems'
|
34
|
+
require 'commandline'
|
35
|
+
|
36
|
+
#
|
37
|
+
# A minimum application
|
38
|
+
#
|
39
|
+
class App < CommandLine::Application
|
40
|
+
def main
|
41
|
+
end
|
42
|
+
end#class App
|
43
|
+
#---------------------------------------------------
|
44
|
+
|
45
|
+
% app.rb
|
46
|
+
Usage: app.rb
|
47
|
+
|
48
|
+
% cat app5.rb
|
49
|
+
#---------------------------------------------------
|
50
|
+
#!/usr/bin/env ruby
|
51
|
+
|
52
|
+
begin
|
53
|
+
require 'commandline'
|
54
|
+
rescue LoadError
|
55
|
+
require 'rubygems'
|
56
|
+
retry
|
57
|
+
end
|
58
|
+
|
59
|
+
class App < CommandLine::Application
|
60
|
+
|
61
|
+
def initialize
|
62
|
+
version "0.0.1"
|
63
|
+
author "Author Name"
|
64
|
+
copyright "2005, Jim Freeze"
|
65
|
+
synopsis "[-dhV] param_file out_file"
|
66
|
+
short_description "A simple app example that takes two arguments."
|
67
|
+
long_description "app5 is a simple application example that supports "+
|
68
|
+
"three options and two commandline arguments."
|
69
|
+
|
70
|
+
option :version
|
71
|
+
option :debug
|
72
|
+
option :help
|
73
|
+
|
74
|
+
expected_args :param_file, :out_file
|
75
|
+
end
|
76
|
+
|
77
|
+
def main
|
78
|
+
puts "main called"
|
79
|
+
puts "@param_file = #{@param_file}"
|
80
|
+
puts "@out_file = #{@out_file}"
|
81
|
+
end
|
82
|
+
end#class App
|
83
|
+
#---------------------------------------------------
|
84
|
+
|
85
|
+
% app5.rb
|
86
|
+
Usage: app5.rb [-dhV] param_file out_file
|
87
|
+
|
88
|
+
% app5.rb -h
|
89
|
+
NAME
|
90
|
+
|
91
|
+
app5.rb - A simple app example that takes two arguments.
|
92
|
+
|
93
|
+
DESCRIPTION
|
94
|
+
|
95
|
+
app5.rb is a simple application example that supports three options
|
96
|
+
and two commandline arguments.
|
97
|
+
|
98
|
+
OPTIONS
|
99
|
+
|
100
|
+
--version,-V
|
101
|
+
Displays application version.
|
102
|
+
|
103
|
+
--debug,-d
|
104
|
+
Sets debug to true.
|
105
|
+
|
106
|
+
--help,-h
|
107
|
+
Displays help page.
|
108
|
+
|
109
|
+
AUTHOR: Author Name
|
110
|
+
Copyright (c) 2005, Jim Freeze
|
111
|
+
|
112
|
+
% app5.rb f1 f2
|
113
|
+
main called
|
114
|
+
@param_file = f1
|
115
|
+
@out_file = f2
|
116
|
+
|
117
|
+
% cat app6.rb
|
118
|
+
#---------------------------------------------------
|
119
|
+
#!/usr/bin/env ruby
|
120
|
+
|
121
|
+
begin
|
122
|
+
require 'commandline'
|
123
|
+
rescue LoadError
|
124
|
+
require 'rubygems'
|
125
|
+
retry
|
126
|
+
end
|
127
|
+
|
128
|
+
#
|
129
|
+
# An application demonstrating customizing of canonical options
|
130
|
+
#
|
131
|
+
class App < CommandLine::Application
|
132
|
+
|
133
|
+
def initialize
|
134
|
+
version "0.0.1"
|
135
|
+
author "Author Name"
|
136
|
+
copyright "2005, Jim Freeze"
|
137
|
+
short_description "A simple app example that takes two arguments."
|
138
|
+
long_description "This app is a simple application example that supports "+
|
139
|
+
"three options and two commandline arguments."
|
140
|
+
|
141
|
+
option :version, :names => %w(--version -v --notice-the-change-from-app5)
|
142
|
+
option :debug, :arity => [0,1], :arg_description => "debug_level",
|
143
|
+
:opt_description => "Set debug level from 0 to 9."
|
144
|
+
option :help
|
145
|
+
|
146
|
+
expected_args :param_file, :out_file
|
147
|
+
end
|
148
|
+
|
149
|
+
def main
|
150
|
+
puts "main called"
|
151
|
+
puts "@param_file = #{@param_file}"
|
152
|
+
puts "@out_file = #{@out_file}"
|
153
|
+
end
|
154
|
+
end#class App
|
155
|
+
#---------------------------------------------------
|
156
|
+
|
157
|
+
% app6.rb -h
|
158
|
+
NAME
|
159
|
+
|
160
|
+
app6.rb - A simple app example that takes two arguments.
|
161
|
+
|
162
|
+
DESCRIPTION
|
163
|
+
|
164
|
+
This app is a simple application example that supports three
|
165
|
+
options and two commandline arguments.
|
166
|
+
|
167
|
+
OPTIONS
|
168
|
+
|
169
|
+
--version,-v,--notice-the-change-from-app5
|
170
|
+
Displays application version.
|
171
|
+
|
172
|
+
--debug,-d debug_level
|
173
|
+
Set debug level from 0 to 9.
|
174
|
+
|
175
|
+
--help,-h
|
176
|
+
Displays help page.
|
177
|
+
|
178
|
+
AUTHOR: Author Name
|
179
|
+
Copyright (c) 2005, Jim Freeze
|
180
|
+
|
181
|
+
% cat app7.rb
|
182
|
+
#---------------------------------------------------
|
183
|
+
#!/usr/bin/env ruby
|
184
|
+
|
185
|
+
begin
|
186
|
+
require 'commandline'
|
187
|
+
rescue LoadError
|
188
|
+
require 'rubygems'
|
189
|
+
retry
|
190
|
+
end
|
191
|
+
|
192
|
+
#
|
193
|
+
# An application demonstrating customizing of canonical options
|
194
|
+
#
|
195
|
+
class App < CommandLine::Application
|
196
|
+
|
197
|
+
def initialize
|
198
|
+
version "0.0.1"
|
199
|
+
author "Author Name"
|
200
|
+
copyright "2005, Jim Freeze"
|
201
|
+
short_description "A simple app example that takes two arguments."
|
202
|
+
long_description "This app is a simple application example that supports "+
|
203
|
+
"three options and two commandline arguments."
|
204
|
+
|
205
|
+
option :version, :names => %w(--version -v --notice-the-change-from-app5)
|
206
|
+
option :debug, :arity => [0,1], :arg_description => "debug_level",
|
207
|
+
:opt_description => "Set debug level from 0 to 9."
|
208
|
+
option :help
|
209
|
+
|
210
|
+
expected_args :param_file, :out_file
|
211
|
+
end
|
212
|
+
|
213
|
+
def main
|
214
|
+
puts "main called"
|
215
|
+
puts "@param_file = #{@param_file}"
|
216
|
+
puts "@out_file = #{@out_file}"
|
217
|
+
end
|
218
|
+
end#class App
|
219
|
+
#---------------------------------------------------
|
220
|
+
|
221
|
+
% app7.rb -h
|
222
|
+
NAME
|
223
|
+
|
224
|
+
app7.rb - A simple app example that takes two arguments.
|
225
|
+
|
226
|
+
DESCRIPTION
|
227
|
+
|
228
|
+
This app is a simple application example that supports three
|
229
|
+
options and two commandline arguments.
|
230
|
+
|
231
|
+
OPTIONS
|
232
|
+
|
233
|
+
--version,-v,--notice-the-change-from-app5
|
234
|
+
Displays application version.
|
235
|
+
|
236
|
+
--debug,-d debug_level
|
237
|
+
Set debug level from 0 to 9.
|
238
|
+
|
239
|
+
--help,-h
|
240
|
+
Displays help page.
|
241
|
+
|
242
|
+
AUTHOR: Author Name
|
243
|
+
Copyright (c) 2005, Jim Freeze
|
244
|
+
|
245
|
+
TESTS
|
246
|
+
=====
|
247
|
+
Tests: 81
|
248
|
+
Assertions: 310
|
249
|
+
|
250
|
+
|
251
|
+
Download & Installation
|
252
|
+
=======================
|
253
|
+
|
254
|
+
Homepage: http://rubyforge.org/projects/optionparser/
|
255
|
+
Documentation: http://optionparser.rubyforge.org/
|
256
|
+
Download: http://rubyforge.org/frs/?group_id=632&release_id=2345
|
257
|
+
|
258
|
+
Dependencies:
|
259
|
+
* None
|
260
|
+
|
261
|
+
Currently optionparser is only available as a rubygem.
|
262
|
+
|
263
|
+
Via RubyGems
|
264
|
+
$ gem install -r commandline
|
265
|
+
|
266
|
+
All feedback is appreciated!
|
267
|
+
|
268
|
+
Installations not yet available
|
269
|
+
===============================
|
270
|
+
# not in RPA yet
|
271
|
+
Via RPA
|
272
|
+
$ rpa install commandline
|
273
|
+
|
274
|
+
# this either
|
275
|
+
The do-it-yourself way
|
276
|
+
$ ruby setup.rb config
|
277
|
+
$ ruby setup.rb setup
|
278
|
+
$ ruby setup.rb install
|
279
|
+
|
280
|
+
# nor this
|
281
|
+
The simplified do-it-yourself way
|
282
|
+
$ rake install
|
283
|
+
|
284
|
+
|
285
|
+
RELEASE NOTES
|
286
|
+
=============
|
287
|
+
|
288
|
+
== 0.7.9 11/05/2005
|
289
|
+
=== Additions
|
290
|
+
- Renamed gem to lowercase commandline
|
291
|
+
- Added replay command options
|
292
|
+
- Added CommandLine::Application_wo_AutoRun - no auto run set thru at_exit
|
293
|
+
- Added documentation for CommandLine::Application - instead of just README
|
294
|
+
- Changed :arg_arity to :arity in Option
|
295
|
+
- Add :required for use with :opt_found
|
296
|
+
- Added args accessor for @args - suggested by Esteban Manchado Velázquez
|
297
|
+
- Added opt() accessor for @option_data[]
|
298
|
+
|
299
|
+
|
300
|
+
HISTORY
|
301
|
+
=======
|
302
|
+
After poking around in a few corporations, it was evident that
|
303
|
+
option parsing was not well understood. Therefore, many inhouse
|
304
|
+
tools were built that did not conform to any of the POSIX, Gnu or XTools
|
305
|
+
option styles. CommandLine::OptionParser was developed so that
|
306
|
+
new applications could be written that conformed to accepted standards,
|
307
|
+
but non-standard option configurations could be handled as well
|
308
|
+
to support legacy interfaces.
|
309
|
+
|
310
|
+
Once the option parsing was written, there was a need to streamline
|
311
|
+
the repetitive tasks in setting up an application. The original
|
312
|
+
boilerplate was simple, but after taking a few cues from
|
313
|
+
rails, a significant amount of functionality was added to
|
314
|
+
Application that make it a very useful tool yet simple to use.
|
315
|
+
|
316
|
+
More information and usage scenarios on OptionParser can be found at:
|
317
|
+
http://rubyforge.org/projects/optionparser/
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
ACKNOWLEDGEMENTS
|
322
|
+
================
|
323
|
+
This library contains code from:
|
324
|
+
* Austin Ziegler - Text::Format
|
325
|
+
* Ara - 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 © 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’s blocks and lambda’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
|
+
"Traditional Option Parsing Schemes" 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], <properties>)
|
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’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 #=> true
|
339
|
+
op2.posix #=> 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 => %w(--debug -d), # the flag has two names
|
349
|
+
:arg_arity => [0,0], # this says take no arugments
|
350
|
+
:opt_description => "Sets debug to true",
|
351
|
+
:arg_description => "",
|
352
|
+
:opt_found => true, # true if seen on command line
|
353
|
+
:opt_not_found => 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 => %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 => ["--debug", "-d"]
|
369
|
+
:arg_arity => [0, 0]
|
370
|
+
:opt_description => "Sets debug to true." # debug is taken from the first name
|
371
|
+
:arg_description => ""
|
372
|
+
:opt_found => true
|
373
|
+
:opt_not_found => false
|
374
|
+
</pre>
|
375
|
+
<p>
|
376
|
+
For a common option like a mode-flag, <tt>Option</tt> will use the first
|
377
|
+
option ‘word’ it finds in the :names list and use that in the
|
378
|
+
automatic option text. Of course, if you don’t want any text, just
|
379
|
+
set the option description to an empty string:
|
380
|
+
</p>
|
381
|
+
<pre>
|
382
|
+
:opt_description => "".
|
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 => "--file", :arg_arity => [1,-1])
|
413
|
+
</pre>
|
414
|
+
<p>
|
415
|
+
Let’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 => "--file", :arg_arity => [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
|
+
‘optional’.
|
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 => "--custom", :arg_arity => [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 => %w(--debug -d),
|
456
|
+
:opt_found => true,
|
457
|
+
:opt_not_found => false
|
458
|
+
)
|
459
|
+
|
460
|
+
opt_debug = Option.new(:flag
|
461
|
+
:names => %w(--debug -d),
|
462
|
+
:opt_found => lambda { true },
|
463
|
+
:opt_not_found => 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["--debug"]
|
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 => %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 => %w(--debug -d),
|
489
|
+
:opt_found => lambda { @debug = true },
|
490
|
+
:opt_not_found => 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’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 => :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 => %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 << Option.new(:flag, :names => %w(--help h))
|
539
|
+
opts << Option.new(:flag, :names => %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 << 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 << 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 << Option.new(:flag, :names => %w[--debug -d])
|
588
|
+
o << Option.new(:flag, :names => %w[--help -h],
|
589
|
+
:opt_description => "Prints this page.")
|
590
|
+
o << Option.new(:names => %w[--ouput -o],
|
591
|
+
:opt_description => "Defines the output file.",
|
592
|
+
:arg_description => "output_file")
|
593
|
+
o << Option.new(:names => %w[--a-long-opt --with-many-names -a -A],
|
594
|
+
:arg_arity => [2,-1],
|
595
|
+
:opt_description => "Your really long description here.",
|
596
|
+
:arg_description => "file1 file2 [file3 ...]")
|
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 => %w(-r),
|
626
|
+
:opt_found => OptionParser::GET_ARGS)
|
627
|
+
od = OptionParser.new(:posix, opt).parse(["-rubygems"])
|
628
|
+
od["-r"] #=> "ubygems"
|
629
|
+
|
630
|
+
od = OptionParser.new(:posix, opt).parse(["-r", "ubygems"])
|
631
|
+
od["-r"] #=> "ubygems"
|
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 << Option.new(:flag, :names => %w(--valid --notvalid))
|
642
|
+
o << Option.new(:flag, :names => %w(--first --second))
|
643
|
+
}.parse(%w(--notvalid --second))
|
644
|
+
od["--valid"] #=> true
|
645
|
+
od["--first"] #=> true
|
646
|
+
od["--notvalid"] #=> CommandLine::OptionData::UnknownOptionError
|
647
|
+
od["--second"] #=> 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 => %w(--file),
|
665
|
+
:argument_arity => [0,-1],
|
666
|
+
:opt_found => OptionParser::GET_ARG_ARRAY)
|
667
|
+
#:opt_found => :collect) # would this be better?
|
668
|
+
od = OptionParser.new(opt).parse(%w(--file))
|
669
|
+
od["--file"] #=> []
|
670
|
+
od = OptionParser.new(opt).parse(%w(--file=file))
|
671
|
+
od["--file"] #=> ["file"]
|
672
|
+
od = OptionParser.new(opt).parse(%w(--file=file1 --file file2))
|
673
|
+
od["--file"] #=> ["file2"]
|
674
|
+
od = OptionParser.new(opt).parse(%w(--file=file1 file2))
|
675
|
+
od["--file"] #=> ["file1", "file2"]
|
676
|
+
od = OptionParser.new(opt).parse(%w(--file file1 file2))
|
677
|
+
od["--file"] #=> ["file1", "file2"]
|
678
|
+
</pre>
|
679
|
+
<h3>GET_ARGS</h3>
|
680
|
+
<p>
|
681
|
+
This is a ‘smart’ 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 => %w(--file),
|
687
|
+
:argument_arity => [0,-1],
|
688
|
+
:opt_found => OptionParser::GET_ARGS)
|
689
|
+
#:opt_found => :smart_collect) # would this be better?
|
690
|
+
od = OptionParser.new(opt).parse(%w(--file))
|
691
|
+
od["--file"] #=> true
|
692
|
+
od = OptionParser.new(opt).parse(%w(--file=file))
|
693
|
+
od["--file"] #=> "file"
|
694
|
+
od = OptionParser.new(opt).parse(%w(--file=file1 --file file2))
|
695
|
+
od["--file"] #=> "file2"
|
696
|
+
od = OptionParser.new(opt).parse(%w(--file=file1 file2))
|
697
|
+
od["--file"] #=> ["file1", "file2"]
|
698
|
+
od = OptionParser.new(opt).parse(%w(--file file1 file2))
|
699
|
+
od["--file"] #=> ["file1", "file2"]
|
700
|
+
</pre>
|
701
|
+
<p>
|
702
|
+
And, for those oxymoronic non-optional options:
|
703
|
+
</p>
|
704
|
+
<pre>
|
705
|
+
opt = Option.new(:names => %w(--not-really-an-option),
|
706
|
+
:opt_not_found => OptionParser::OPT_NOT_FOUND_BUT_REQUIRED
|
707
|
+
)
|
708
|
+
OptionParser.new(opt).parse([]) #=> 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 ‘5’ argument belongs to the ‘k’ 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 ‘see’ the three mode-flags. Instead, they will be
|
815
|
+
treated as arguments to the application:
|
816
|
+
</p>
|
817
|
+
<pre>
|
818
|
+
#args = ["-x", "-y", "-z"]
|
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 — 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 ’=’.
|
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 ’- -’, the
|
880
|
+
<em>Gnu</em> style has a triple-hyphen ’- - -’ 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 = ["-x", "-y", "-z"]
|
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>’-’ 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 ’-’. A definitely
|
920
|
+
conjured example of this freedom is:
|
921
|
+
</p>
|
922
|
+
<pre>
|
923
|
+
:names => %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
|
+
["--foos", "--fbars"]
|
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 "—foos" option flag since it can be determined that
|
946
|
+
"—fo" will only match "—foos" and not
|
947
|
+
"—fbars".
|
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>
|