getopt 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +6 -0
- data/README +161 -152
- data/Rakefile +29 -23
- data/getopt.gemspec +20 -23
- data/lib/getopt/long.rb +3 -3
- data/lib/getopt/std.rb +1 -1
- data/test/test_getopt_long.rb +1 -1
- data/test/test_getopt_std.rb +1 -1
- metadata +28 -12
data/CHANGES
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 1.4.1 - 17-Jul-2011
|
2
|
+
* Now works with Ruby 1.9.x. Thanks go to Shura for the patch.
|
3
|
+
* Refactored the gemspec. Gem building code is now handled by Rake tasks.
|
4
|
+
* Refactored the Rakefile. Added a default task, removed the old install
|
5
|
+
task, and namespaced the gem related tasks.
|
6
|
+
|
1
7
|
== 1.4.0 - 5-Sep-2009
|
2
8
|
* Fixed a packaging bug where the libs weren't actually being included! Gah!
|
3
9
|
Thanks go to Steven Hilton for the spot.
|
data/README
CHANGED
@@ -1,183 +1,192 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
= Description
|
2
|
+
Implements a simple Getopt::Std class for command line parsing, as well
|
3
|
+
as a Getopt::Long class for more advanced command line parsing.
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
== Installation
|
9
|
-
rake test (optional)
|
10
|
-
rake install (non-gem) or rake install_gem (gem)
|
5
|
+
= Installation
|
6
|
+
|
7
|
+
gem install getopt
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
= Synopsis
|
10
|
+
== Getopt::Std
|
11
|
+
|
12
|
+
require 'getopt/std'
|
15
13
|
|
16
|
-
|
17
|
-
|
14
|
+
# Look for -o with argument, and -I and -D boolean arguments
|
15
|
+
opt = Getopt::Std.getopts("o:ID")
|
18
16
|
|
19
|
-
|
20
|
-
|
17
|
+
if opt["I"]
|
18
|
+
# Do something if -I passed
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
if opt["D"]
|
21
|
+
# Do something if -D passed
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
23
|
+
if opt["o"]
|
24
|
+
case opt["o"]
|
25
|
+
# blah, blah, blah
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
== Getopt::Long
|
30
|
+
|
31
|
+
require 'getopt/long'
|
32
|
+
|
33
|
+
opt = Getopt::Long.getopts(
|
34
|
+
["--foo", "-f", Getopt::BOOLEAN],
|
35
|
+
["--bar", "-b", Getopt::REQUIRED]
|
36
|
+
)
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
38
|
+
# Or, to save your fingers some typing:
|
39
|
+
#
|
40
|
+
# require "getopt/long"
|
41
|
+
# include Getopt
|
42
|
+
# opt = Long.getopts(
|
43
|
+
# ["--foo", "-f", BOOLEAN],
|
44
|
+
# ["--bar", "-b", REQUIRED]
|
45
|
+
# )
|
46
|
+
|
47
|
+
if opt["foo"]
|
48
|
+
# Do something if --foo or -f passed
|
49
|
+
|
50
|
+
if opt["b"]
|
51
|
+
# Do something if --bar or -b passed
|
52
|
+
|
53
|
+
= Class Methods
|
54
|
+
|
55
55
|
Std.getopts(switches)
|
56
|
-
Takes a series of single character switches that can be accepted on the
|
57
|
-
command line. Those characters followed by a ':' require an argument. The
|
58
|
-
rest are considered boolean switches. Returns a hash, with the switches
|
59
|
-
as the key (sans the leading '-'). For boolean switches, the value is
|
60
|
-
either true or false. Switches that were not passed on the command line
|
61
|
-
do not appear in the hash.
|
62
56
|
|
63
|
-
|
64
|
-
|
57
|
+
Takes a series of single character switches that can be accepted on the
|
58
|
+
command line. Those characters followed by a ':' require an argument. The
|
59
|
+
rest are considered boolean switches. Returns a hash, with the switches
|
60
|
+
as the key (sans the leading '-'). For boolean switches, the value is
|
61
|
+
either true or false. Switches that were not passed on the command line
|
62
|
+
do not appear in the hash.
|
63
|
+
|
64
|
+
In the event that a switch that accepts an argument appears multiple times
|
65
|
+
the value for that key becomes an array of values.
|
65
66
|
|
66
67
|
Long.getopts(switches)
|
67
|
-
Takes an array of switches beginning with "--" followed by one or more
|
68
|
-
alphanumeric or hyphen characters, or "-" followed by a single character.
|
69
|
-
The type of argument, if any, can be specified as BOOLEAN, OPTIONAL,
|
70
|
-
REQUIRED or INCREMENT.
|
71
68
|
|
72
|
-
|
69
|
+
Takes an array of switches beginning with "--" followed by one or more
|
70
|
+
alphanumeric or hyphen characters, or "-" followed by a single character.
|
71
|
+
The type of argument, if any, can be specified as BOOLEAN, OPTIONAL,
|
72
|
+
REQUIRED or INCREMENT.
|
73
|
+
|
74
|
+
The array should be in the form:
|
73
75
|
|
74
|
-
|
75
|
-
|
76
|
+
# long form, short form (alias), option type
|
77
|
+
["--long", "-l", Getopt::OPTION]
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
79
|
+
Note that only the long form is required. If the short form is not
|
80
|
+
specified, it will automatically be set to the first letter of the long
|
81
|
+
switch. If multiple long switches with the same first character are
|
82
|
+
listed without short switches, only the first long switch gets the short
|
83
|
+
switch alias.
|
82
84
|
|
83
|
-
|
85
|
+
If the argument type is not specified, the default is BOOLEAN.
|
84
86
|
|
85
|
-
|
86
|
-
|
87
|
+
For the truly lazy, you can also pass a string of long switches (with
|
88
|
+
no short switches or argument types).
|
87
89
|
|
88
|
-
|
90
|
+
See the 'examples' directory for more examples.
|
91
|
+
|
92
|
+
= Getopt::Long argument types
|
89
93
|
|
90
|
-
== Getopt::Long argument types
|
91
94
|
REQUIRED
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
+
If the option is specified on the command line, it must be followed by
|
96
|
+
a non-blank argument. This argument cannot be another switch. If this
|
97
|
+
switch appears multiple times, the values are collected into an array.
|
95
98
|
|
96
99
|
BOOLEAN
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
+
If the option is specified on the command line, its value is set to true.
|
101
|
+
It must not be followed by a non-blank argument, excluding other switches.
|
102
|
+
Attempting to pass a boolean switch more than once will raise an error.
|
100
103
|
|
101
104
|
OPTIONAL
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
105
|
+
If the option is specified on the command line, it may or may not accept
|
106
|
+
an argument, excluding other valid switches. If an argument is present,
|
107
|
+
it's value is set to that argument. If an argument is not present, it's
|
108
|
+
value is set to nil.
|
106
109
|
|
107
110
|
INCREMENT
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
111
|
+
If the option is specified on the command line, its value is incremented
|
112
|
+
by one for each appearance on the command line, or set to 1 if it appears
|
113
|
+
only once.
|
114
|
+
|
115
|
+
= Future Plans
|
116
|
+
Add support for negatable options so that you can do "--no-foo", for
|
117
|
+
example.
|
118
|
+
|
119
|
+
Add support for numeric types, so that you don't have to manually convert
|
120
|
+
strings to numbers.
|
121
|
+
|
122
|
+
Allow shortcut characters for the option types, e.g. "?" for BOOLEAN, "+"
|
123
|
+
for INCREMENT, etc.
|
124
|
+
|
125
|
+
= Known Bugs
|
126
|
+
|
127
|
+
== Getopt::Std
|
128
|
+
|
129
|
+
You cannot squish switches that require arguments with the argument itself.
|
130
|
+
For example, if you do Getopt::Std.getopts("o:ID"), it will not parse
|
131
|
+
"-IDohello" properly. Instead, you must do "-IDo hello". Or, you can just
|
132
|
+
separate the argument, e.g. "-I -D -o hello".
|
133
|
+
|
134
|
+
== Getopt::Long
|
135
|
+
|
136
|
+
If you mix and match compressed switches with separate, optional switches
|
137
|
+
the optional switch will be set to true instead of nil if it separated
|
138
|
+
from the compressed switches.
|
133
139
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
140
|
+
== Reporting Bugs
|
141
|
+
|
142
|
+
If you find any other bugs, please log them on the project
|
143
|
+
page at http://www.rubyforge.org/projects/shards.
|
144
|
+
|
145
|
+
= Other Issues
|
146
|
+
|
147
|
+
Neither class attempts to be POSIX compliant in any way, shape or form.
|
148
|
+
And I don't care!
|
149
|
+
|
150
|
+
= Notes From the Author
|
151
|
+
|
152
|
+
My main gripe with the getoptlong library currently in the standard library
|
153
|
+
is that it doesn't return a hash, yet gives you partial hash behavior. This
|
154
|
+
was both confusing and annoying, since the first thing I do (along with
|
155
|
+
everyone else) is collect the results into a hash for later processing.
|
156
|
+
|
157
|
+
My main gripe with the optparse library (also in the standard library) is
|
158
|
+
that it treats command line processing like event processing. It's too
|
159
|
+
complex, when 90% of the time all you want to do is slurp the command line
|
160
|
+
options into a hash.
|
161
|
+
|
162
|
+
So, I did something utterly novel with this library. I collected the command
|
163
|
+
line options ... (wait for it) ... into a hash! Then I give that hash to
|
164
|
+
you, aliases and all. I did get some ideas from Perl's Getopt::Long library,
|
165
|
+
but this is in no way a port of that module (which supports POSIX parsing, GNU
|
166
|
+
parsing, more option types, etc). My goal was to provide the functionality
|
167
|
+
that I felt would cover the vast majority of common cases, yet still provide
|
168
|
+
a little extra spice with switch types (REQUIRED, OPTIONAL, etc).
|
169
|
+
|
170
|
+
There are a few extra things I plan to add (see the 'Future Plans' above) but
|
171
|
+
I do not plan on this library ever becoming as feature rich as, say, Perl's
|
172
|
+
Getopt::Long module.
|
164
173
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
174
|
+
If you plan to write a full fledged command line application, e.g. you plan
|
175
|
+
on implementing a full help system, gobs of command line options and tons of
|
176
|
+
switches, consider Jim Freeze's 'commandline' package, available on
|
177
|
+
RubyForge.
|
169
178
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
179
|
+
= Warranty
|
180
|
+
This package is provided "as is" and without any express or
|
181
|
+
implied warranties, including, without limitation, the implied
|
182
|
+
warranties of merchantability and fitness for a particular purpose.
|
174
183
|
|
175
|
-
|
176
|
-
|
184
|
+
= License
|
185
|
+
Artistic 2.0
|
177
186
|
|
178
|
-
|
179
|
-
|
180
|
-
|
187
|
+
= Copyright
|
188
|
+
(C) 2005-2011, Daniel J. Berger
|
189
|
+
All Rights Reserved
|
181
190
|
|
182
|
-
|
183
|
-
|
191
|
+
= Author
|
192
|
+
Daniel J. Berger
|
data/Rakefile
CHANGED
@@ -1,34 +1,40 @@
|
|
1
1
|
require 'rake'
|
2
|
+
require 'rake/clean'
|
2
3
|
require 'rake/testtask'
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
CLEAN.include("**/*.gem", "**/*.rbc")
|
6
|
+
|
7
|
+
namespace :gem do
|
8
|
+
desc "Create the getopt gem"
|
9
|
+
task :create => [:clean] do
|
10
|
+
spec = eval(IO.read('getopt.gemspec'))
|
11
|
+
Gem::Builder.new(spec).build
|
12
|
+
end
|
11
13
|
|
12
|
-
desc "Install the getopt
|
13
|
-
task :
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
desc "Install the getopt gem"
|
15
|
+
task :install => [:create] do
|
16
|
+
file = Dir["*.gem"].first
|
17
|
+
sh "gem install #{file}"
|
18
|
+
end
|
17
19
|
end
|
18
20
|
|
19
21
|
Rake::TestTask.new do |t|
|
20
|
-
|
21
|
-
|
22
|
+
t.warning = true
|
23
|
+
t.verbose = true
|
22
24
|
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
namespace :test do
|
27
|
+
Rake::TestTask.new('getopt_long') do |t|
|
28
|
+
t.test_files = 'test/test_getopt_long.rb'
|
29
|
+
t.warning = true
|
30
|
+
t.verbose = true
|
31
|
+
end
|
29
32
|
|
30
|
-
Rake::TestTask.new('
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
Rake::TestTask.new('getopt_std') do |t|
|
34
|
+
t.test_files = 'test/test_getopt_std.rb'
|
35
|
+
t.warning = true
|
36
|
+
t.verbose = true
|
37
|
+
end
|
34
38
|
end
|
39
|
+
|
40
|
+
task :default => :test
|
data/getopt.gemspec
CHANGED
@@ -1,29 +1,26 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') }
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'getopt'
|
5
|
+
spec.version = '1.4.1'
|
6
|
+
spec.author = 'Daniel J. Berger'
|
7
|
+
spec.license = 'Artistic 2.0'
|
8
|
+
spec.email = 'djberg96@gmail.com'
|
9
|
+
spec.homepage = 'http://www.rubyforge.org/projects/shards'
|
10
|
+
spec.platform = Gem::Platform::RUBY
|
11
|
+
spec.summary = 'Getopt::Std and Getopt::Long option parsers for Ruby'
|
12
|
+
spec.test_files = Dir['test/*.rb']
|
13
|
+
spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
|
15
14
|
|
16
|
-
|
17
|
-
|
15
|
+
spec.rubyforge_project = 'shards'
|
16
|
+
spec.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
|
18
17
|
|
19
|
-
|
18
|
+
spec.add_development_dependency('test-unit', '>= 2.0.3')
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
spec.description = <<-EOF
|
21
|
+
The getopt library provides two different command line option parsers.
|
22
|
+
They are meant as easier and more convenient replacements for the
|
23
|
+
command line parsers that ship as part of the Ruby standard library.
|
24
|
+
Please see the README for additional comments.
|
25
|
+
EOF
|
27
26
|
end
|
28
|
-
|
29
|
-
Gem::Builder.new(spec).build
|
data/lib/getopt/long.rb
CHANGED
@@ -17,7 +17,7 @@ module Getopt
|
|
17
17
|
class Error < StandardError; end
|
18
18
|
|
19
19
|
# The version of the getopt library
|
20
|
-
VERSION = '1.4.
|
20
|
+
VERSION = '1.4.1'
|
21
21
|
|
22
22
|
# Takes an array of switches. Each array consists of up to three
|
23
23
|
# elements that indicate the name and type of switch. Returns a hash
|
@@ -215,7 +215,7 @@ module Getopt
|
|
215
215
|
#
|
216
216
|
# This allows users to refer to the long or short switch and get
|
217
217
|
# the same value
|
218
|
-
hash.each{ |switch, val|
|
218
|
+
hash.dup.each{ |switch, val|
|
219
219
|
if syns.keys.include?(switch)
|
220
220
|
syns[switch] = [syns[switch]] if RUBY_VERSION.to_f >= 1.9
|
221
221
|
syns[switch].each{ |key|
|
@@ -225,7 +225,7 @@ module Getopt
|
|
225
225
|
}
|
226
226
|
|
227
227
|
# Get rid of leading "--" and "-" to make it easier to reference
|
228
|
-
hash.each{ |key, value|
|
228
|
+
hash.dup.each{ |key, value|
|
229
229
|
if key =~ /^-/
|
230
230
|
if key[0,2] == '--'
|
231
231
|
nkey = key.sub('--', '')
|
data/lib/getopt/std.rb
CHANGED
@@ -10,7 +10,7 @@ module Getopt
|
|
10
10
|
class Error < StandardError; end
|
11
11
|
|
12
12
|
# The version of the getopt library
|
13
|
-
VERSION = '1.4.
|
13
|
+
VERSION = '1.4.1'
|
14
14
|
|
15
15
|
# Processes single character command line options with option
|
16
16
|
# clustering. This information is parsed from ARGV and returned
|
data/test/test_getopt_long.rb
CHANGED
data/test/test_getopt_std.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: getopt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 5
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 4
|
9
|
+
- 1
|
10
|
+
version: 1.4.1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Daniel J. Berger
|
@@ -9,20 +15,25 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
13
|
-
default_executable:
|
18
|
+
date: 2011-07-17 00:00:00 Z
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: test-unit
|
17
|
-
|
18
|
-
|
19
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
20
25
|
requirements:
|
21
26
|
- - ">="
|
22
27
|
- !ruby/object:Gem::Version
|
28
|
+
hash: 9
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 0
|
32
|
+
- 3
|
23
33
|
version: 2.0.3
|
24
|
-
|
25
|
-
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
description: " The getopt library provides two different command line option parsers.\n They are meant as easier and more convenient replacements for the\n command line parsers that ship as part of the Ruby standard library.\n Please see the README for additional comments.\n"
|
26
37
|
email: djberg96@gmail.com
|
27
38
|
executables: []
|
28
39
|
|
@@ -44,7 +55,6 @@ files:
|
|
44
55
|
- README
|
45
56
|
- test/test_getopt_long.rb
|
46
57
|
- test/test_getopt_std.rb
|
47
|
-
has_rdoc: true
|
48
58
|
homepage: http://www.rubyforge.org/projects/shards
|
49
59
|
licenses:
|
50
60
|
- Artistic 2.0
|
@@ -54,21 +64,27 @@ rdoc_options: []
|
|
54
64
|
require_paths:
|
55
65
|
- lib
|
56
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
57
68
|
requirements:
|
58
69
|
- - ">="
|
59
70
|
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
60
74
|
version: "0"
|
61
|
-
version:
|
62
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
63
77
|
requirements:
|
64
78
|
- - ">="
|
65
79
|
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
66
83
|
version: "0"
|
67
|
-
version:
|
68
84
|
requirements: []
|
69
85
|
|
70
86
|
rubyforge_project: shards
|
71
|
-
rubygems_version: 1.3
|
87
|
+
rubygems_version: 1.8.3
|
72
88
|
signing_key:
|
73
89
|
specification_version: 3
|
74
90
|
summary: Getopt::Std and Getopt::Long option parsers for Ruby
|