getopt 1.3.6 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +61 -53
- data/MANIFEST +11 -12
- data/Rakefile +22 -23
- data/examples/example_long.rb +65 -0
- data/examples/example_std.rb +38 -0
- data/getopt.gemspec +24 -0
- data/lib/getopt/long.rb +234 -232
- data/lib/getopt/std.rb +79 -79
- data/test/{tc_getopt_long.rb → test_getopt_long.rb} +264 -264
- data/test/{tc_getopt_std.rb → test_getopt_std.rb} +82 -82
- metadata +53 -40
- data/test/ts_all.rb +0 -5
data/lib/getopt/std.rb
CHANGED
@@ -1,79 +1,79 @@
|
|
1
|
-
module Getopt
|
2
|
-
class Std
|
3
|
-
class Error < StandardError; end
|
4
|
-
|
5
|
-
VERSION = '1.3.
|
6
|
-
|
7
|
-
# Processes single character command line options with option
|
8
|
-
# clustering. This information is parsed from ARGV and returned
|
9
|
-
# as a hash, with the switch (minus the "-") as the key. The value
|
10
|
-
# for that key is either true/false (boolean switches) or the argument
|
11
|
-
# that was passed to the switch.
|
12
|
-
#
|
13
|
-
# Characters followed by a ":" require an argument. The rest are
|
14
|
-
# considered boolean switches. If a switch that accepts an argument
|
15
|
-
# appears more than once, the value for that key becomes an array
|
16
|
-
# of values.
|
17
|
-
#
|
18
|
-
def self.getopts(switches)
|
19
|
-
args = switches.split(/ */)
|
20
|
-
hash = {}
|
21
|
-
|
22
|
-
while !ARGV.empty? && ARGV.first =~ /^-(.)(.*)/s
|
23
|
-
first, rest = $1, $2
|
24
|
-
pos = switches.index(first)
|
25
|
-
|
26
|
-
# Switches on the command line must appear among the characters
|
27
|
-
# declared in +switches+.
|
28
|
-
raise Error, "invalid option '#{first}'" unless pos
|
29
|
-
|
30
|
-
if args[pos+1] == ":"
|
31
|
-
ARGV.shift
|
32
|
-
if rest.empty?
|
33
|
-
rest = ARGV.shift
|
34
|
-
|
35
|
-
# Ensure that switches requiring arguments actually
|
36
|
-
# receive a (non-switch) argument.
|
37
|
-
if rest.nil? || rest.empty?
|
38
|
-
raise Error, "missing argument for '-#{args[pos]}'"
|
39
|
-
end
|
40
|
-
|
41
|
-
# Do not permit switches that require arguments to be
|
42
|
-
# followed immediately by another switch.
|
43
|
-
if args.include?(rest) || args.include?(rest[1..-1])
|
44
|
-
err = "cannot use switch '#{rest}' as argument "
|
45
|
-
err += "to another switch"
|
46
|
-
raise Error, err
|
47
|
-
end
|
48
|
-
|
49
|
-
# For non boolean switches, arguments that appear multiple
|
50
|
-
# times are converted to an array (or pushed onto an already
|
51
|
-
# existant array).
|
52
|
-
if hash.has_key?(first)
|
53
|
-
hash[first] = [hash[first], rest].flatten
|
54
|
-
else
|
55
|
-
hash[first] = rest
|
56
|
-
end
|
57
|
-
else
|
58
|
-
# Do not permit switches that require arguments to be
|
59
|
-
# followed immediately by another switch.
|
60
|
-
if args.include?(rest) || args.include?(rest[1..-1])
|
61
|
-
err = "cannot use switch '#{rest}' as argument "
|
62
|
-
err += "to another switch"
|
63
|
-
raise Error, err
|
64
|
-
end
|
65
|
-
end
|
66
|
-
else
|
67
|
-
hash[first] = true # Boolean switch
|
68
|
-
if rest.empty?
|
69
|
-
ARGV.shift
|
70
|
-
else
|
71
|
-
ARGV[0] = "-#{rest}"
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
hash
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
1
|
+
module Getopt
|
2
|
+
class Std
|
3
|
+
class Error < StandardError; end
|
4
|
+
|
5
|
+
VERSION = '1.3.7'
|
6
|
+
|
7
|
+
# Processes single character command line options with option
|
8
|
+
# clustering. This information is parsed from ARGV and returned
|
9
|
+
# as a hash, with the switch (minus the "-") as the key. The value
|
10
|
+
# for that key is either true/false (boolean switches) or the argument
|
11
|
+
# that was passed to the switch.
|
12
|
+
#
|
13
|
+
# Characters followed by a ":" require an argument. The rest are
|
14
|
+
# considered boolean switches. If a switch that accepts an argument
|
15
|
+
# appears more than once, the value for that key becomes an array
|
16
|
+
# of values.
|
17
|
+
#
|
18
|
+
def self.getopts(switches)
|
19
|
+
args = switches.split(/ */)
|
20
|
+
hash = {}
|
21
|
+
|
22
|
+
while !ARGV.empty? && ARGV.first =~ /^-(.)(.*)/s
|
23
|
+
first, rest = $1, $2
|
24
|
+
pos = switches.index(first)
|
25
|
+
|
26
|
+
# Switches on the command line must appear among the characters
|
27
|
+
# declared in +switches+.
|
28
|
+
raise Error, "invalid option '#{first}'" unless pos
|
29
|
+
|
30
|
+
if args[pos+1] == ":"
|
31
|
+
ARGV.shift
|
32
|
+
if rest.empty?
|
33
|
+
rest = ARGV.shift
|
34
|
+
|
35
|
+
# Ensure that switches requiring arguments actually
|
36
|
+
# receive a (non-switch) argument.
|
37
|
+
if rest.nil? || rest.empty?
|
38
|
+
raise Error, "missing argument for '-#{args[pos]}'"
|
39
|
+
end
|
40
|
+
|
41
|
+
# Do not permit switches that require arguments to be
|
42
|
+
# followed immediately by another switch.
|
43
|
+
if args.include?(rest) || args.include?(rest[1..-1])
|
44
|
+
err = "cannot use switch '#{rest}' as argument "
|
45
|
+
err += "to another switch"
|
46
|
+
raise Error, err
|
47
|
+
end
|
48
|
+
|
49
|
+
# For non boolean switches, arguments that appear multiple
|
50
|
+
# times are converted to an array (or pushed onto an already
|
51
|
+
# existant array).
|
52
|
+
if hash.has_key?(first)
|
53
|
+
hash[first] = [hash[first], rest].flatten
|
54
|
+
else
|
55
|
+
hash[first] = rest
|
56
|
+
end
|
57
|
+
else
|
58
|
+
# Do not permit switches that require arguments to be
|
59
|
+
# followed immediately by another switch.
|
60
|
+
if args.include?(rest) || args.include?(rest[1..-1])
|
61
|
+
err = "cannot use switch '#{rest}' as argument "
|
62
|
+
err += "to another switch"
|
63
|
+
raise Error, err
|
64
|
+
end
|
65
|
+
end
|
66
|
+
else
|
67
|
+
hash[first] = true # Boolean switch
|
68
|
+
if rest.empty?
|
69
|
+
ARGV.shift
|
70
|
+
else
|
71
|
+
ARGV[0] = "-#{rest}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
hash
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -1,264 +1,264 @@
|
|
1
|
-
#####################################################################
|
2
|
-
# tc_getopt_long.rb
|
3
|
-
#
|
4
|
-
# Test suite for the getopt-long package. You should run this test
|
5
|
-
# via the 'rake test' rake task.
|
6
|
-
#####################################################################
|
7
|
-
require 'test/unit'
|
8
|
-
require 'getopt/long'
|
9
|
-
include Getopt
|
10
|
-
|
11
|
-
class TC_Getopt_Long < Test::Unit::TestCase
|
12
|
-
def setup
|
13
|
-
@opts = nil
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_version
|
17
|
-
assert_equal('1.3.
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_constants
|
21
|
-
assert_not_nil(BOOLEAN)
|
22
|
-
assert_not_nil(OPTIONAL)
|
23
|
-
assert_not_nil(REQUIRED)
|
24
|
-
assert_not_nil(INCREMENT)
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_getopts_basic
|
28
|
-
assert_respond_to(Long, :getopts)
|
29
|
-
assert_nothing_raised{
|
30
|
-
Long.getopts(["--test"],["--help"],["--foo"])
|
31
|
-
}
|
32
|
-
assert_nothing_raised{
|
33
|
-
Long.getopts(["--test", "-x"],["--help", "-y"],["--foo", "-z"])
|
34
|
-
}
|
35
|
-
assert_nothing_raised{
|
36
|
-
Long.getopts(
|
37
|
-
["--test", "-x", BOOLEAN],
|
38
|
-
["--help", "-y", REQUIRED],
|
39
|
-
["--foo", "-z", OPTIONAL],
|
40
|
-
["--more", "-m", INCREMENT]
|
41
|
-
)
|
42
|
-
}
|
43
|
-
assert_kind_of(Hash, Long.getopts("--test"))
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_getopts_using_equals
|
47
|
-
ARGV.push("--foo=hello","-b","world")
|
48
|
-
assert_nothing_raised{
|
49
|
-
@opts = Long.getopts(
|
50
|
-
["--foo", "-f", REQUIRED],
|
51
|
-
["--bar", "-b", OPTIONAL]
|
52
|
-
)
|
53
|
-
}
|
54
|
-
assert_equal("hello", @opts["foo"])
|
55
|
-
assert_equal("hello", @opts["f"])
|
56
|
-
assert_equal("world", @opts["bar"])
|
57
|
-
assert_equal("world", @opts["b"])
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_getopts_long_embedded_hyphens
|
61
|
-
ARGV.push('--foo-bar', 'hello', '--test1-test2-test3', 'world')
|
62
|
-
assert_nothing_raised{
|
63
|
-
@opts = Long.getopts(
|
64
|
-
['--foo-bar', '-f', REQUIRED],
|
65
|
-
['--test1-test2-test3', '-t', REQUIRED]
|
66
|
-
)
|
67
|
-
}
|
68
|
-
assert_equal('hello', @opts['foo-bar'])
|
69
|
-
assert_equal('hello', @opts['f'])
|
70
|
-
assert_equal('world', @opts['test1-test2-test3'])
|
71
|
-
assert_equal('world', @opts['t'])
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_getopts_long_embedded_hyphens_using_equals_sign
|
75
|
-
ARGV.push('--foo-bar=hello', '--test1-test2-test3=world')
|
76
|
-
assert_nothing_raised{
|
77
|
-
@opts = Long.getopts(
|
78
|
-
['--foo-bar', '-f', REQUIRED],
|
79
|
-
['--test1-test2-test3', '-t', REQUIRED]
|
80
|
-
)
|
81
|
-
}
|
82
|
-
assert_equal('hello', @opts['foo-bar'])
|
83
|
-
assert_equal('hello', @opts['f'])
|
84
|
-
assert_equal('world', @opts['test1-test2-test3'])
|
85
|
-
assert_equal('world', @opts['t'])
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_getopts_short_switch_squished
|
89
|
-
ARGV.push("-f", "hello", "-bworld")
|
90
|
-
assert_nothing_raised{
|
91
|
-
@opts = Long.getopts(
|
92
|
-
["--foo", "-f", REQUIRED],
|
93
|
-
["--bar", "-b", OPTIONAL]
|
94
|
-
)
|
95
|
-
}
|
96
|
-
assert_equal("hello", @opts["f"])
|
97
|
-
assert_equal("world", @opts["b"])
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_getopts_increment_type
|
101
|
-
ARGV.push("-m","-m")
|
102
|
-
assert_nothing_raised{
|
103
|
-
@opts = Long.getopts(["--more", "-m", INCREMENT])
|
104
|
-
}
|
105
|
-
assert_equal(2, @opts["more"])
|
106
|
-
assert_equal(2, @opts["m"])
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_switches_exist
|
110
|
-
ARGV.push("--verbose","--test","--foo")
|
111
|
-
assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
|
112
|
-
assert_equal(true, @opts.has_key?("verbose"))
|
113
|
-
assert_equal(true, @opts.has_key?("test"))
|
114
|
-
assert_equal(true, @opts.has_key?("foo"))
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_short_switch_synonyms
|
118
|
-
ARGV.push("--verbose","--test","--foo")
|
119
|
-
assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
|
120
|
-
assert_equal(true, @opts.has_key?("v"))
|
121
|
-
assert_equal(true, @opts.has_key?("t"))
|
122
|
-
assert_equal(true, @opts.has_key?("f"))
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_short_switch_synonyms_with_explicit_types
|
126
|
-
ARGV.push("--verbose", "--test", "hello", "--foo")
|
127
|
-
assert_nothing_raised{
|
128
|
-
@opts = Long.getopts(
|
129
|
-
["--verbose", BOOLEAN],
|
130
|
-
["--test", REQUIRED],
|
131
|
-
["--foo", BOOLEAN]
|
132
|
-
)
|
133
|
-
}
|
134
|
-
assert(@opts.has_key?("v"))
|
135
|
-
assert(@opts.has_key?("t"))
|
136
|
-
assert(@opts.has_key?("f"))
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_switches_with_required_arguments
|
140
|
-
ARGV.push("--foo","1","--bar","hello")
|
141
|
-
assert_nothing_raised{
|
142
|
-
@opts = Long.getopts(
|
143
|
-
["--foo", "-f", REQUIRED],
|
144
|
-
["--bar", "-b", REQUIRED]
|
145
|
-
)
|
146
|
-
}
|
147
|
-
assert_equal({"foo"=>"1", "bar"=>"hello", "f"=>"1", "b"=>"hello"}, @opts)
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_compressed_switches
|
151
|
-
ARGV.push("-fb")
|
152
|
-
assert_nothing_raised{
|
153
|
-
@opts = Long.getopts(
|
154
|
-
["--foo", "-f", BOOLEAN],
|
155
|
-
["--bar", "-b", BOOLEAN]
|
156
|
-
)
|
157
|
-
}
|
158
|
-
assert_equal({"foo"=>true, "f"=>true, "b"=>true, "bar"=>true}, @opts)
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_compress_switches_with_required_arg
|
162
|
-
ARGV.push("-xf", "foo.txt")
|
163
|
-
assert_nothing_raised{
|
164
|
-
@opts = Long.getopts(
|
165
|
-
["--expand", "-x", BOOLEAN],
|
166
|
-
["--file", "-f", REQUIRED]
|
167
|
-
)
|
168
|
-
}
|
169
|
-
assert_equal(
|
170
|
-
{"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
|
171
|
-
)
|
172
|
-
end
|
173
|
-
|
174
|
-
def test_compress_switches_with_compressed_required_arg
|
175
|
-
ARGV.push("-xffoo.txt")
|
176
|
-
assert_nothing_raised{
|
177
|
-
@opts = Long.getopts(
|
178
|
-
["--expand", "-x", BOOLEAN],
|
179
|
-
["--file", "-f", REQUIRED]
|
180
|
-
)
|
181
|
-
}
|
182
|
-
assert_equal(
|
183
|
-
{"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
|
184
|
-
)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_compress_switches_with_optional_arg_not_defined
|
188
|
-
ARGV.push("-xf")
|
189
|
-
assert_nothing_raised{
|
190
|
-
@opts = Long.getopts(
|
191
|
-
["--expand", "-x", BOOLEAN],
|
192
|
-
["--file", "-f", OPTIONAL]
|
193
|
-
)
|
194
|
-
}
|
195
|
-
assert_equal(
|
196
|
-
{"x"=>true, "expand"=>true, "f"=>nil, "file"=>nil}, @opts
|
197
|
-
)
|
198
|
-
end
|
199
|
-
|
200
|
-
def test_compress_switches_with_optional_arg
|
201
|
-
ARGV.push("-xf", "boo.txt")
|
202
|
-
assert_nothing_raised{
|
203
|
-
@opts = Long.getopts(
|
204
|
-
["--expand", "-x", BOOLEAN],
|
205
|
-
["--file", "-f", OPTIONAL]
|
206
|
-
)
|
207
|
-
}
|
208
|
-
assert_equal(
|
209
|
-
{"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
|
210
|
-
)
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_compress_switches_with_compressed_optional_arg
|
214
|
-
ARGV.push("-xfboo.txt")
|
215
|
-
assert_nothing_raised{
|
216
|
-
@opts = Long.getopts(
|
217
|
-
["--expand", "-x", BOOLEAN],
|
218
|
-
["--file", "-f", OPTIONAL]
|
219
|
-
)
|
220
|
-
}
|
221
|
-
assert_equal(
|
222
|
-
{"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
|
223
|
-
)
|
224
|
-
end
|
225
|
-
|
226
|
-
def test_compressed_short_and_long_mixed
|
227
|
-
ARGV.push("-xb", "--file", "boo.txt", "-v")
|
228
|
-
assert_nothing_raised{
|
229
|
-
@opts = Long.getopts(
|
230
|
-
["--expand", "-x", BOOLEAN],
|
231
|
-
["--verbose", "-v", BOOLEAN],
|
232
|
-
["--file", "-f", REQUIRED],
|
233
|
-
["--bar", "-b", OPTIONAL]
|
234
|
-
)
|
235
|
-
assert_equal(
|
236
|
-
{ "x"=>true, "expand"=>true,
|
237
|
-
"v"=>true, "verbose"=>true,
|
238
|
-
"f"=>"boo.txt", "file"=>"boo.txt",
|
239
|
-
"b"=>nil, "bar"=>nil
|
240
|
-
},
|
241
|
-
@opts
|
242
|
-
)
|
243
|
-
}
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_multiple_similar_long_switches_with_no_short_switches
|
247
|
-
ARGV.push('--to','1','--too','2','--tooo','3')
|
248
|
-
assert_nothing_raised{
|
249
|
-
@opts = Long.getopts(
|
250
|
-
["--to", REQUIRED],
|
251
|
-
["--too", REQUIRED],
|
252
|
-
["--tooo", REQUIRED]
|
253
|
-
)
|
254
|
-
}
|
255
|
-
assert_equal('1', @opts['to'])
|
256
|
-
assert_equal('2', @opts['too'])
|
257
|
-
assert_equal('3', @opts['tooo'])
|
258
|
-
end
|
259
|
-
|
260
|
-
def teardown
|
261
|
-
@opts = nil
|
262
|
-
ARGV.clear
|
263
|
-
end
|
264
|
-
end
|
1
|
+
#####################################################################
|
2
|
+
# tc_getopt_long.rb
|
3
|
+
#
|
4
|
+
# Test suite for the getopt-long package. You should run this test
|
5
|
+
# via the 'rake test' rake task.
|
6
|
+
#####################################################################
|
7
|
+
require 'test/unit'
|
8
|
+
require 'getopt/long'
|
9
|
+
include Getopt
|
10
|
+
|
11
|
+
class TC_Getopt_Long < Test::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
@opts = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_version
|
17
|
+
assert_equal('1.3.7', Long::VERSION)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_constants
|
21
|
+
assert_not_nil(BOOLEAN)
|
22
|
+
assert_not_nil(OPTIONAL)
|
23
|
+
assert_not_nil(REQUIRED)
|
24
|
+
assert_not_nil(INCREMENT)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_getopts_basic
|
28
|
+
assert_respond_to(Long, :getopts)
|
29
|
+
assert_nothing_raised{
|
30
|
+
Long.getopts(["--test"],["--help"],["--foo"])
|
31
|
+
}
|
32
|
+
assert_nothing_raised{
|
33
|
+
Long.getopts(["--test", "-x"],["--help", "-y"],["--foo", "-z"])
|
34
|
+
}
|
35
|
+
assert_nothing_raised{
|
36
|
+
Long.getopts(
|
37
|
+
["--test", "-x", BOOLEAN],
|
38
|
+
["--help", "-y", REQUIRED],
|
39
|
+
["--foo", "-z", OPTIONAL],
|
40
|
+
["--more", "-m", INCREMENT]
|
41
|
+
)
|
42
|
+
}
|
43
|
+
assert_kind_of(Hash, Long.getopts("--test"))
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_getopts_using_equals
|
47
|
+
ARGV.push("--foo=hello","-b","world")
|
48
|
+
assert_nothing_raised{
|
49
|
+
@opts = Long.getopts(
|
50
|
+
["--foo", "-f", REQUIRED],
|
51
|
+
["--bar", "-b", OPTIONAL]
|
52
|
+
)
|
53
|
+
}
|
54
|
+
assert_equal("hello", @opts["foo"])
|
55
|
+
assert_equal("hello", @opts["f"])
|
56
|
+
assert_equal("world", @opts["bar"])
|
57
|
+
assert_equal("world", @opts["b"])
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_getopts_long_embedded_hyphens
|
61
|
+
ARGV.push('--foo-bar', 'hello', '--test1-test2-test3', 'world')
|
62
|
+
assert_nothing_raised{
|
63
|
+
@opts = Long.getopts(
|
64
|
+
['--foo-bar', '-f', REQUIRED],
|
65
|
+
['--test1-test2-test3', '-t', REQUIRED]
|
66
|
+
)
|
67
|
+
}
|
68
|
+
assert_equal('hello', @opts['foo-bar'])
|
69
|
+
assert_equal('hello', @opts['f'])
|
70
|
+
assert_equal('world', @opts['test1-test2-test3'])
|
71
|
+
assert_equal('world', @opts['t'])
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_getopts_long_embedded_hyphens_using_equals_sign
|
75
|
+
ARGV.push('--foo-bar=hello', '--test1-test2-test3=world')
|
76
|
+
assert_nothing_raised{
|
77
|
+
@opts = Long.getopts(
|
78
|
+
['--foo-bar', '-f', REQUIRED],
|
79
|
+
['--test1-test2-test3', '-t', REQUIRED]
|
80
|
+
)
|
81
|
+
}
|
82
|
+
assert_equal('hello', @opts['foo-bar'])
|
83
|
+
assert_equal('hello', @opts['f'])
|
84
|
+
assert_equal('world', @opts['test1-test2-test3'])
|
85
|
+
assert_equal('world', @opts['t'])
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_getopts_short_switch_squished
|
89
|
+
ARGV.push("-f", "hello", "-bworld")
|
90
|
+
assert_nothing_raised{
|
91
|
+
@opts = Long.getopts(
|
92
|
+
["--foo", "-f", REQUIRED],
|
93
|
+
["--bar", "-b", OPTIONAL]
|
94
|
+
)
|
95
|
+
}
|
96
|
+
assert_equal("hello", @opts["f"])
|
97
|
+
assert_equal("world", @opts["b"])
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_getopts_increment_type
|
101
|
+
ARGV.push("-m","-m")
|
102
|
+
assert_nothing_raised{
|
103
|
+
@opts = Long.getopts(["--more", "-m", INCREMENT])
|
104
|
+
}
|
105
|
+
assert_equal(2, @opts["more"])
|
106
|
+
assert_equal(2, @opts["m"])
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_switches_exist
|
110
|
+
ARGV.push("--verbose","--test","--foo")
|
111
|
+
assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
|
112
|
+
assert_equal(true, @opts.has_key?("verbose"))
|
113
|
+
assert_equal(true, @opts.has_key?("test"))
|
114
|
+
assert_equal(true, @opts.has_key?("foo"))
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_short_switch_synonyms
|
118
|
+
ARGV.push("--verbose","--test","--foo")
|
119
|
+
assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
|
120
|
+
assert_equal(true, @opts.has_key?("v"))
|
121
|
+
assert_equal(true, @opts.has_key?("t"))
|
122
|
+
assert_equal(true, @opts.has_key?("f"))
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_short_switch_synonyms_with_explicit_types
|
126
|
+
ARGV.push("--verbose", "--test", "hello", "--foo")
|
127
|
+
assert_nothing_raised{
|
128
|
+
@opts = Long.getopts(
|
129
|
+
["--verbose", BOOLEAN],
|
130
|
+
["--test", REQUIRED],
|
131
|
+
["--foo", BOOLEAN]
|
132
|
+
)
|
133
|
+
}
|
134
|
+
assert(@opts.has_key?("v"))
|
135
|
+
assert(@opts.has_key?("t"))
|
136
|
+
assert(@opts.has_key?("f"))
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_switches_with_required_arguments
|
140
|
+
ARGV.push("--foo","1","--bar","hello")
|
141
|
+
assert_nothing_raised{
|
142
|
+
@opts = Long.getopts(
|
143
|
+
["--foo", "-f", REQUIRED],
|
144
|
+
["--bar", "-b", REQUIRED]
|
145
|
+
)
|
146
|
+
}
|
147
|
+
assert_equal({"foo"=>"1", "bar"=>"hello", "f"=>"1", "b"=>"hello"}, @opts)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_compressed_switches
|
151
|
+
ARGV.push("-fb")
|
152
|
+
assert_nothing_raised{
|
153
|
+
@opts = Long.getopts(
|
154
|
+
["--foo", "-f", BOOLEAN],
|
155
|
+
["--bar", "-b", BOOLEAN]
|
156
|
+
)
|
157
|
+
}
|
158
|
+
assert_equal({"foo"=>true, "f"=>true, "b"=>true, "bar"=>true}, @opts)
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_compress_switches_with_required_arg
|
162
|
+
ARGV.push("-xf", "foo.txt")
|
163
|
+
assert_nothing_raised{
|
164
|
+
@opts = Long.getopts(
|
165
|
+
["--expand", "-x", BOOLEAN],
|
166
|
+
["--file", "-f", REQUIRED]
|
167
|
+
)
|
168
|
+
}
|
169
|
+
assert_equal(
|
170
|
+
{"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
|
171
|
+
)
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_compress_switches_with_compressed_required_arg
|
175
|
+
ARGV.push("-xffoo.txt")
|
176
|
+
assert_nothing_raised{
|
177
|
+
@opts = Long.getopts(
|
178
|
+
["--expand", "-x", BOOLEAN],
|
179
|
+
["--file", "-f", REQUIRED]
|
180
|
+
)
|
181
|
+
}
|
182
|
+
assert_equal(
|
183
|
+
{"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
|
184
|
+
)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_compress_switches_with_optional_arg_not_defined
|
188
|
+
ARGV.push("-xf")
|
189
|
+
assert_nothing_raised{
|
190
|
+
@opts = Long.getopts(
|
191
|
+
["--expand", "-x", BOOLEAN],
|
192
|
+
["--file", "-f", OPTIONAL]
|
193
|
+
)
|
194
|
+
}
|
195
|
+
assert_equal(
|
196
|
+
{"x"=>true, "expand"=>true, "f"=>nil, "file"=>nil}, @opts
|
197
|
+
)
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_compress_switches_with_optional_arg
|
201
|
+
ARGV.push("-xf", "boo.txt")
|
202
|
+
assert_nothing_raised{
|
203
|
+
@opts = Long.getopts(
|
204
|
+
["--expand", "-x", BOOLEAN],
|
205
|
+
["--file", "-f", OPTIONAL]
|
206
|
+
)
|
207
|
+
}
|
208
|
+
assert_equal(
|
209
|
+
{"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
|
210
|
+
)
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_compress_switches_with_compressed_optional_arg
|
214
|
+
ARGV.push("-xfboo.txt")
|
215
|
+
assert_nothing_raised{
|
216
|
+
@opts = Long.getopts(
|
217
|
+
["--expand", "-x", BOOLEAN],
|
218
|
+
["--file", "-f", OPTIONAL]
|
219
|
+
)
|
220
|
+
}
|
221
|
+
assert_equal(
|
222
|
+
{"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
|
223
|
+
)
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_compressed_short_and_long_mixed
|
227
|
+
ARGV.push("-xb", "--file", "boo.txt", "-v")
|
228
|
+
assert_nothing_raised{
|
229
|
+
@opts = Long.getopts(
|
230
|
+
["--expand", "-x", BOOLEAN],
|
231
|
+
["--verbose", "-v", BOOLEAN],
|
232
|
+
["--file", "-f", REQUIRED],
|
233
|
+
["--bar", "-b", OPTIONAL]
|
234
|
+
)
|
235
|
+
assert_equal(
|
236
|
+
{ "x"=>true, "expand"=>true,
|
237
|
+
"v"=>true, "verbose"=>true,
|
238
|
+
"f"=>"boo.txt", "file"=>"boo.txt",
|
239
|
+
"b"=>nil, "bar"=>nil
|
240
|
+
},
|
241
|
+
@opts
|
242
|
+
)
|
243
|
+
}
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_multiple_similar_long_switches_with_no_short_switches
|
247
|
+
ARGV.push('--to','1','--too','2','--tooo','3')
|
248
|
+
assert_nothing_raised{
|
249
|
+
@opts = Long.getopts(
|
250
|
+
["--to", REQUIRED],
|
251
|
+
["--too", REQUIRED],
|
252
|
+
["--tooo", REQUIRED]
|
253
|
+
)
|
254
|
+
}
|
255
|
+
assert_equal('1', @opts['to'])
|
256
|
+
assert_equal('2', @opts['too'])
|
257
|
+
assert_equal('3', @opts['tooo'])
|
258
|
+
end
|
259
|
+
|
260
|
+
def teardown
|
261
|
+
@opts = nil
|
262
|
+
ARGV.clear
|
263
|
+
end
|
264
|
+
end
|