getopt 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -0
- data/MANIFEST +7 -2
- data/README +111 -19
- data/test/tc_getopt_long.rb +119 -0
- data/test/tc_getopt_std.rb +1 -1
- data/test/ts_all.rb +5 -0
- metadata +5 -3
data/CHANGES
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
== 4-Nov-2005 - 1.3.0
|
2
|
+
* Added the Getopt::Long class (long.rb). This is a complete revamp of the
|
3
|
+
old getoptlong package, with ideas tossed in from Perl's Getopt::Long
|
4
|
+
package. See the README and example script for more detail.
|
5
|
+
* Added an example, and renamed the "test_std.rb" example to "example_std.rb".
|
6
|
+
* Added lots of documentation to the README file.
|
7
|
+
* Updated the MANIFEST, test suite, etc.
|
8
|
+
|
1
9
|
== 24-Oct-2005 - 1.2.0
|
2
10
|
* Altered the way multiple occurrences of the same switch are handled, for
|
3
11
|
those switches that accept arguments.
|
data/MANIFEST
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
CHANGES
|
2
2
|
MANIFEST
|
3
|
+
README
|
3
4
|
install.rb
|
4
5
|
getopt.gemspec
|
5
6
|
|
6
|
-
examples/
|
7
|
+
examples/example_std.rb
|
8
|
+
examples/example_long.rb
|
7
9
|
|
8
10
|
lib/getopt/std.rb
|
11
|
+
lib/getopt/long.rb
|
9
12
|
|
10
|
-
test/tc_getopt_std.rb
|
13
|
+
test/tc_getopt_std.rb
|
14
|
+
test/tc_getopt_long.rb
|
15
|
+
test/ts_all.rb
|
data/README
CHANGED
@@ -1,36 +1,51 @@
|
|
1
1
|
== Description
|
2
|
-
Implements a simple Getopt::Std class for command line parsing
|
3
|
-
|
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
5
|
== Prerequisites
|
6
6
|
Ruby 1.8.0 or later.
|
7
7
|
|
8
8
|
== Installation
|
9
9
|
=== Standard Installation
|
10
|
-
ruby test/
|
10
|
+
ruby test/ts_all.rb
|
11
11
|
ruby install.rb
|
12
|
+
|
12
13
|
=== Gem Installation
|
13
|
-
ruby test/
|
14
|
+
ruby test/ts_all.rb
|
14
15
|
ruby getopt.gemspec
|
15
16
|
gem install getopt-x-y-z.gem # Where x-y-z is a version
|
16
17
|
|
17
18
|
== Synopsis
|
18
|
-
|
19
|
-
|
20
|
-
# Look for -o with argument, and -I and -D boolean arguments
|
21
|
-
opt = Getopt::Std.getopts("o:ID")
|
19
|
+
=== Getopt::Std
|
20
|
+
require "getopt/std"
|
22
21
|
|
23
|
-
|
24
|
-
|
22
|
+
# Look for -o with argument, and -I and -D boolean arguments
|
23
|
+
opt = Getopt::Std.getopts("o:ID")
|
24
|
+
|
25
|
+
if opt["I"]
|
26
|
+
# Do something if -I passed
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
+
if opt["D"]
|
29
|
+
# Do something if -D passed
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
if opt["o"]
|
32
|
+
case opt["o"]
|
33
|
+
# blah, blah, blah
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
=== Getopt::Long
|
38
|
+
require "getopt/long"
|
39
|
+
opt = Getopt::Long.getopts(
|
40
|
+
["--foo", "-f", BOOLEAN],
|
41
|
+
["--bar", "-b", REQUIRED]
|
42
|
+
)
|
43
|
+
|
44
|
+
if opt["foo"]
|
45
|
+
# Do something if --foo or -f passed
|
46
|
+
|
47
|
+
if opt["b"]
|
48
|
+
# Do something if --bar or -b passed
|
34
49
|
|
35
50
|
== Class Methods
|
36
51
|
Std.getopts(switches)
|
@@ -44,10 +59,60 @@ Std.getopts(switches)
|
|
44
59
|
In the event that a switch that accepts an argument appears multiple times
|
45
60
|
the value for that key becomes an array of values.
|
46
61
|
|
62
|
+
Long.getopts(switches)
|
63
|
+
Takes an array of switches beginning with "--" followed by one or more
|
64
|
+
characters, or "-w" followed by a single character. The type of argument,
|
65
|
+
if any, can be specified as BOOLEAN, OPTIONAL, REQUIRED or INCREMENT.
|
66
|
+
|
67
|
+
The array should be in the form:
|
68
|
+
|
69
|
+
["--long", "-l", OPTION]
|
70
|
+
|
71
|
+
Note that only the long form is required. If the short switch is not
|
72
|
+
specified, it will automatically be set to the first letter of the long
|
73
|
+
switch.
|
74
|
+
|
75
|
+
If the argument type is not specified, the default is BOOLEAN.
|
76
|
+
|
77
|
+
For the truly lazy, you can also pass a string of long switches (with
|
78
|
+
no short switches or argument types).
|
79
|
+
|
80
|
+
See the 'examples' directory for more examples.
|
81
|
+
|
82
|
+
== Getopt::Long argument types
|
83
|
+
REQUIRED
|
84
|
+
If the option is specified on the command line, it must be followed by
|
85
|
+
a non-blank argument. This argument cannot be another switch. If this
|
86
|
+
switch appears multiple times, the values are collected into an array.
|
87
|
+
|
88
|
+
BOOLEAN
|
89
|
+
If the option is specified on the command line, it's value is set to true.
|
90
|
+
It must not be followed by a non-blank argument, excluding other switches.
|
91
|
+
Attempting to pass a boolean switch more than once will raise an error.
|
92
|
+
|
93
|
+
OPTIONAL
|
94
|
+
If the option is specified on the command line, it may or may not accept
|
95
|
+
an argument, excluding other valid switches. If an argument is present,
|
96
|
+
it's value is set to that argument. If an argument is not present, it's
|
97
|
+
value is set to nil.
|
98
|
+
|
99
|
+
INCREMENT
|
100
|
+
If the option is specified on the command line, its value is incremented
|
101
|
+
by one for each appearance on the command line, or set to 1 if it appears
|
102
|
+
only once.
|
103
|
+
|
47
104
|
== Future Plans
|
48
|
-
|
49
|
-
|
105
|
+
Add support for negatable options so that you can do "--no-foo", for
|
106
|
+
example.
|
107
|
+
|
108
|
+
Add support for numeric types, so that you don't have to manually convert
|
109
|
+
strings to numbers.
|
110
|
+
|
111
|
+
Allow shortcut characters for the option types, e.g. "?" for BOOLEAN, "+"
|
112
|
+
for INCREMENT, etc.
|
113
|
+
|
50
114
|
== Known Bugs
|
115
|
+
=== Getopt::Std
|
51
116
|
You cannot squish switches that require arguments with the argument itself.
|
52
117
|
For example, if you do Getopt::Std.getopts("o:ID"), it will not parse
|
53
118
|
"-IDohello" properly. Instead, you must do "-IDo hello". Or, you can just
|
@@ -56,6 +121,33 @@ Std.getopts(switches)
|
|
56
121
|
If you find any other bugs, please log them on the project
|
57
122
|
page at http://www.rubyforge.org/projects/shards.
|
58
123
|
|
124
|
+
== Other Issues
|
125
|
+
Neither of the classes attempts to be POSIX compliant in any way, shape
|
126
|
+
or form. And I don't care!
|
127
|
+
|
128
|
+
== Notes From the Author
|
129
|
+
My main gripe with the getoptlong library currently in the standard library
|
130
|
+
is that it doesn't return a hash, yet gives you partial hash behavior. This
|
131
|
+
was both confusing and annoying, since the first thing I (and everyone else)
|
132
|
+
does is collect the results into a hash for later processing.
|
133
|
+
|
134
|
+
My main gripe with the optparse library (also in the standard library) is
|
135
|
+
that it treats command line processing like event processing. It's too
|
136
|
+
complex, when 90% of the time all you want to do is slurp the command line
|
137
|
+
options into a hash.
|
138
|
+
|
139
|
+
So, I did something utterly novel with this library. I collected the command
|
140
|
+
line options ... (wait for it) ... into a hash! Then I give that hash to
|
141
|
+
you, aliases and all. I did get some ideas from Perl's Getopt::Long library,
|
142
|
+
but this is in no way a port of that module (which supports POSIX parsing, GNU
|
143
|
+
parsing, more option types, etc). My goal was to provide the functionality
|
144
|
+
that I felt would cover the vast majority of common cases, yet still provide
|
145
|
+
a little extra spice with switch types (REQUIRED, OPTIONAL, etc).
|
146
|
+
|
147
|
+
There are a few extra things I plan to add (see the 'Future Plans' above) but
|
148
|
+
I do not plan on this library ever becoming as feature rich as, say, Perl's
|
149
|
+
Getopt::Long module.
|
150
|
+
|
59
151
|
== Warranty
|
60
152
|
This package is provided "as is" and without any express or
|
61
153
|
implied warranties, including, without limitation, the implied
|
@@ -0,0 +1,119 @@
|
|
1
|
+
#######################################################
|
2
|
+
# tc_getopt_long.rb
|
3
|
+
#
|
4
|
+
# Test suite for the getopt-long package.
|
5
|
+
#######################################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" || base =~ /getopt/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
11
|
+
Dir.chdir("test") rescue nil
|
12
|
+
end
|
13
|
+
|
14
|
+
require "test/unit"
|
15
|
+
require "getopt/long"
|
16
|
+
include Getopt
|
17
|
+
|
18
|
+
class TC_Getopt_Long < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@opts = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_version
|
24
|
+
assert_equal("1.3.0", Long::VERSION)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_constants
|
28
|
+
assert_not_nil(BOOLEAN)
|
29
|
+
assert_not_nil(OPTIONAL)
|
30
|
+
assert_not_nil(REQUIRED)
|
31
|
+
assert_not_nil(INCREMENT)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_getopts_basic
|
35
|
+
assert_respond_to(Long, :getopts)
|
36
|
+
assert_nothing_raised{
|
37
|
+
Long.getopts(["--test"],["--help"],["--foo"])
|
38
|
+
}
|
39
|
+
assert_nothing_raised{
|
40
|
+
Long.getopts(["--test", "-x"],["--help", "-y"],["--foo", "-z"])
|
41
|
+
}
|
42
|
+
assert_nothing_raised{
|
43
|
+
Long.getopts(
|
44
|
+
["--test", "-x", BOOLEAN],
|
45
|
+
["--help", "-y", REQUIRED],
|
46
|
+
["--foo", "-z", OPTIONAL],
|
47
|
+
["--more", "-m", INCREMENT]
|
48
|
+
)
|
49
|
+
}
|
50
|
+
assert_kind_of(Hash, Long.getopts("--test"))
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_getopts_using_equals
|
54
|
+
ARGV.push("--foo=hello","-b","world")
|
55
|
+
assert_nothing_raised{
|
56
|
+
@opts = Long.getopts(
|
57
|
+
["--foo", "-f", REQUIRED],
|
58
|
+
["--bar", "-b", OPTIONAL]
|
59
|
+
)
|
60
|
+
}
|
61
|
+
assert_equal("hello", @opts["foo"])
|
62
|
+
assert_equal("hello", @opts["f"])
|
63
|
+
assert_equal("world", @opts["bar"])
|
64
|
+
assert_equal("world", @opts["b"])
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_getopts_short_switch_squished
|
68
|
+
ARGV.push("-f", "hello", "-bworld")
|
69
|
+
assert_nothing_raised{
|
70
|
+
@opts = Long.getopts(
|
71
|
+
["--foo", "-f", REQUIRED],
|
72
|
+
["--bar", "-b", OPTIONAL]
|
73
|
+
)
|
74
|
+
}
|
75
|
+
assert_equal("hello", @opts["f"])
|
76
|
+
assert_equal("world", @opts["b"])
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_getopts_increment_type
|
80
|
+
ARGV.push("-m","-m")
|
81
|
+
assert_nothing_raised{
|
82
|
+
@opts = Long.getopts(["--more", "-m", INCREMENT])
|
83
|
+
}
|
84
|
+
assert_equal(2, @opts["more"])
|
85
|
+
assert_equal(2, @opts["m"])
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_switches_exist
|
89
|
+
ARGV.push("--verbose","--test","--foo")
|
90
|
+
assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
|
91
|
+
assert_equal(true, @opts.has_key?("verbose"))
|
92
|
+
assert_equal(true, @opts.has_key?("test"))
|
93
|
+
assert_equal(true, @opts.has_key?("foo"))
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_short_switch_synonyms
|
97
|
+
ARGV.push("--verbose","--test","--foo")
|
98
|
+
assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
|
99
|
+
assert_equal(true, @opts.has_key?("v"))
|
100
|
+
assert_equal(true, @opts.has_key?("t"))
|
101
|
+
assert_equal(true, @opts.has_key?("f"))
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_switches_with_required_arguments
|
105
|
+
ARGV.push("--foo","1","--bar","hello")
|
106
|
+
assert_nothing_raised{
|
107
|
+
@opts = Long.getopts(
|
108
|
+
["--foo", "-f", REQUIRED],
|
109
|
+
["--bar", "-b", REQUIRED]
|
110
|
+
)
|
111
|
+
}
|
112
|
+
assert_equal({"foo"=>"1", "bar"=>"hello", "f"=>"1", "b"=>"hello"}, @opts)
|
113
|
+
end
|
114
|
+
|
115
|
+
def teardown
|
116
|
+
@opts = nil
|
117
|
+
ARGV.clear
|
118
|
+
end
|
119
|
+
end
|
data/test/tc_getopt_std.rb
CHANGED
data/test/ts_all.rb
ADDED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: getopt
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.
|
7
|
-
date: 2005-
|
6
|
+
version: 1.3.0
|
7
|
+
date: 2005-11-04 00:00:00 -07:00
|
8
8
|
summary: Getopt::Std for Ruby
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -32,9 +32,11 @@ files:
|
|
32
32
|
- CHANGES
|
33
33
|
- MANIFEST
|
34
34
|
- README
|
35
|
+
- test/tc_getopt_long.rb
|
35
36
|
- test/tc_getopt_std.rb
|
37
|
+
- test/ts_all.rb
|
36
38
|
test_files:
|
37
|
-
- test/
|
39
|
+
- test/ts_all.rb
|
38
40
|
rdoc_options: []
|
39
41
|
extra_rdoc_files:
|
40
42
|
- README
|