getopt 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/getopt.gemspec CHANGED
@@ -2,20 +2,18 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'getopt'
5
- spec.version = '1.5.1'
5
+ spec.version = '1.6.0'
6
6
  spec.author = 'Daniel J. Berger'
7
7
  spec.license = 'Apache-2.0'
8
8
  spec.email = 'djberg96@gmail.com'
9
9
  spec.homepage = 'https://github.com/djberg96/getopt'
10
10
  spec.summary = 'Getopt::Std and Getopt::Long option parsers for Ruby'
11
- spec.test_files = Dir['test/*.rb']
11
+ spec.test_files = Dir['spec/*_spec.rb']
12
12
  spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
13
13
  spec.cert_chain = Dir['certs/*']
14
14
 
15
- spec.extra_rdoc_files = ['README.rdoc', 'CHANGES.rdoc', 'MANIFEST.rdoc']
16
-
17
- spec.add_development_dependency('test-unit', '>= 2.5.0')
18
- spec.required_ruby_version = '>= 2.2.0'
15
+ spec.add_development_dependency('rake')
16
+ spec.add_development_dependency('rspec', '~> 3.9')
19
17
 
20
18
  spec.metadata = {
21
19
  'homepage_uri' => 'https://github.com/djberg96/getopt',
@@ -1,6 +1,6 @@
1
1
  module Getopt
2
2
  module Version
3
3
  # The version of the getopt library
4
- VERSION = '1.5.1'.freeze
4
+ VERSION = '1.6.0'.freeze
5
5
  end
6
6
  end
@@ -0,0 +1,279 @@
1
+ #####################################################################
2
+ # getopt_long_spec.rb
3
+ #
4
+ # Specs for the getopt-long library. You should run this test
5
+ # via the 'rake spec:getopt_long' rake task.
6
+ #####################################################################
7
+ require 'rspec'
8
+ require 'getopt/long'
9
+
10
+ RSpec.describe Getopt::Long do
11
+ before do
12
+ @opts = nil
13
+ end
14
+
15
+ example "version" do
16
+ expect(Getopt::Long::VERSION).to eq('1.6.0')
17
+ expect(Getopt::Long::VERSION).to be_frozen
18
+ end
19
+
20
+ example "constants" do
21
+ expect(Getopt::BOOLEAN).not_to be_nil
22
+ expect(Getopt::OPTIONAL).not_to be_nil
23
+ expect(Getopt::REQUIRED).not_to be_nil
24
+ expect(Getopt::INCREMENT).not_to be_nil
25
+ end
26
+
27
+ example "getopts long basic functionality" do
28
+ expect(Getopt::Long).to respond_to(:getopts)
29
+
30
+ expect{ Getopt::Long.getopts(["--test"],["--help"],["--foo"]) }.not_to raise_error
31
+ expect{ Getopt::Long.getopts(["--test", "-x"],["--help", "-y"],["--foo", "-z"]) }.not_to raise_error
32
+
33
+ expect{
34
+ Getopt::Long.getopts(
35
+ ["--test", "-x", Getopt::BOOLEAN],
36
+ ["--help", "-y", Getopt::REQUIRED],
37
+ ["--foo", "-z", Getopt::OPTIONAL],
38
+ ["--more", "-m", Getopt::INCREMENT]
39
+ )
40
+ }.not_to raise_error
41
+
42
+ expect(Getopt::Long.getopts("--test")).to be_kind_of(Hash)
43
+ end
44
+
45
+ example "getopts long using equals sign works as expected" do
46
+ ARGV.push("--foo=hello","-b","world")
47
+
48
+ expect{
49
+ @opts = Getopt::Long.getopts(
50
+ ["--foo", "-f", Getopt::REQUIRED],
51
+ ["--bar", "-b", Getopt::OPTIONAL]
52
+ )
53
+ }.not_to raise_error
54
+
55
+ expect(@opts["foo"]).to eq("hello")
56
+ expect(@opts["f"]).to eq("hello")
57
+ expect(@opts["bar"]).to eq("world")
58
+ expect(@opts["b"]).to eq("world")
59
+ end
60
+
61
+ example "getopts long with embedded hyphens works as expected" do
62
+ ARGV.push('--foo-bar', 'hello', '--test1-test2-test3', 'world')
63
+
64
+ expect{
65
+ @opts = Getopt::Long.getopts(
66
+ ['--foo-bar', '-f', Getopt::REQUIRED],
67
+ ['--test1-test2-test3', '-t', Getopt::REQUIRED]
68
+ )
69
+ }.not_to raise_error
70
+
71
+ expect(@opts['foo-bar']).to eq('hello')
72
+ expect(@opts['f']).to eq('hello')
73
+ expect(@opts['test1-test2-test3']).to eq('world')
74
+ expect(@opts['t']).to eq('world')
75
+ end
76
+
77
+ example "getopts long embedded hyphens using equals sign works as expected" do
78
+ ARGV.push('--foo-bar=hello', '--test1-test2-test3=world')
79
+
80
+ expect{
81
+ @opts = Getopt::Long.getopts(
82
+ ['--foo-bar', '-f', Getopt::REQUIRED],
83
+ ['--test1-test2-test3', '-t', Getopt::REQUIRED]
84
+ )
85
+ }.not_to raise_error
86
+
87
+ expect(@opts['foo-bar']).to eq('hello')
88
+ expect(@opts['f']).to eq('hello')
89
+ expect(@opts['test1-test2-test3']).to eq('world')
90
+ expect(@opts['t']).to eq('world')
91
+ end
92
+
93
+ example "getopts long with short switch squished works as expected" do
94
+ ARGV.push("-f", "hello", "-bworld")
95
+
96
+ expect{
97
+ @opts = Getopt::Long.getopts(
98
+ ["--foo", "-f", Getopt::REQUIRED],
99
+ ["--bar", "-b", Getopt::OPTIONAL]
100
+ )
101
+ }.not_to raise_error
102
+
103
+ expect(@opts["f"]).to eq("hello")
104
+ expect(@opts["b"]).to eq("world")
105
+ end
106
+
107
+ example "getopts long increment type works as expected" do
108
+ ARGV.push("-m","-m")
109
+
110
+ expect{ @opts = Getopt::Long.getopts(["--more", "-m", Getopt::INCREMENT]) }.not_to raise_error
111
+
112
+ expect(@opts["more"]).to eq(2)
113
+ expect(@opts["m"]).to eq(2)
114
+ end
115
+
116
+ example "switches are set as expected" do
117
+ ARGV.push("--verbose","--test","--foo")
118
+ expect{ @opts = Getopt::Long.getopts("--verbose --test --foo") }.not_to raise_error
119
+ expect( @opts.has_key?("verbose")).to eq(true)
120
+ expect( @opts.has_key?("test")).to eq(true)
121
+ expect( @opts.has_key?("foo")).to eq(true)
122
+ end
123
+
124
+ example "short switch synonyms work as expected" do
125
+ ARGV.push("--verbose","--test","--foo")
126
+ expect{ @opts = Getopt::Long.getopts("--verbose --test --foo") }.not_to raise_error
127
+ expect(@opts.has_key?("v")).to eq(true)
128
+ expect(@opts.has_key?("t")).to eq(true)
129
+ expect(@opts.has_key?("f")).to eq(true)
130
+ end
131
+
132
+ example "short_switch_synonyms_with_explicit_types" do
133
+ ARGV.push("--verbose", "--test", "hello", "--foo")
134
+
135
+ expect{
136
+ @opts = Getopt::Long.getopts(
137
+ ["--verbose", Getopt::BOOLEAN],
138
+ ["--test", Getopt::REQUIRED],
139
+ ["--foo", Getopt::BOOLEAN]
140
+ )
141
+ }.not_to raise_error
142
+
143
+ expect(@opts.has_key?("v")).to be(true)
144
+ expect(@opts.has_key?("t")).to be(true)
145
+ expect(@opts.has_key?("f")).to be(true)
146
+ end
147
+
148
+ example "switches with required arguments" do
149
+ ARGV.push("--foo","1","--bar","hello")
150
+
151
+ expect{
152
+ @opts = Getopt::Long.getopts(
153
+ ["--foo", "-f", Getopt::REQUIRED],
154
+ ["--bar", "-b", Getopt::REQUIRED]
155
+ )
156
+ }.not_to raise_error
157
+
158
+ expect(@opts).to eq({"foo"=>"1", "bar"=>"hello", "f"=>"1", "b"=>"hello"})
159
+ end
160
+
161
+ example "compressed switches work as expected" do
162
+ ARGV.push("-fb")
163
+
164
+ expect{
165
+ @opts = Getopt::Long.getopts(
166
+ ["--foo", "-f", Getopt::BOOLEAN],
167
+ ["--bar", "-b", Getopt::BOOLEAN]
168
+ )
169
+ }.not_to raise_error
170
+
171
+ expect(@opts).to eq({"foo"=>true, "f"=>true, "b"=>true, "bar"=>true})
172
+ end
173
+
174
+ example "compress switches with required argument works as expected" do
175
+ ARGV.push("-xf", "foo.txt")
176
+
177
+ expect{
178
+ @opts = Getopt::Long.getopts(
179
+ ["--expand", "-x", Getopt::BOOLEAN],
180
+ ["--file", "-f", Getopt::REQUIRED]
181
+ )
182
+ }.not_to raise_error
183
+
184
+ expect(@opts).to eq({"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"})
185
+ end
186
+
187
+ example "compress switches with argument that is compressed works as expected" do
188
+ ARGV.push("-xffoo.txt")
189
+
190
+ expect{
191
+ @opts = Getopt::Long.getopts(
192
+ ["--expand", "-x", Getopt::BOOLEAN],
193
+ ["--file", "-f", Getopt::REQUIRED]
194
+ )
195
+ }.not_to raise_error
196
+
197
+ expect(@opts).to eq({"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"})
198
+ end
199
+
200
+ example "compress switches with optional argument not defined works as expected" do
201
+ ARGV.push("-xf")
202
+
203
+ expect{
204
+ @opts = Getopt::Long.getopts(
205
+ ["--expand", "-x", Getopt::BOOLEAN],
206
+ ["--file", "-f", Getopt::OPTIONAL]
207
+ )
208
+ }.not_to raise_error
209
+
210
+ expect(@opts).to eq({"x"=>true, "expand"=>true, "f"=>nil, "file"=>nil})
211
+ end
212
+
213
+ example "compress switches with optional argument works as expected" do
214
+ ARGV.push("-xf", "boo.txt")
215
+
216
+ expect{
217
+ @opts = Getopt::Long.getopts(
218
+ ["--expand", "-x", Getopt::BOOLEAN],
219
+ ["--file", "-f", Getopt::OPTIONAL]
220
+ )
221
+ }.not_to raise_error
222
+
223
+ expect(@opts).to eq({"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"})
224
+ end
225
+
226
+ example "compress switches with compressed optional argument works as expected" do
227
+ ARGV.push("-xfboo.txt")
228
+
229
+ expect{
230
+ @opts = Getopt::Long.getopts(
231
+ ["--expand", "-x", Getopt::BOOLEAN],
232
+ ["--file", "-f", Getopt::OPTIONAL]
233
+ )
234
+ }.not_to raise_error
235
+
236
+ expect(@opts).to eq({"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"})
237
+ end
238
+
239
+ example "compressed_short_and_long_mixed" do
240
+ ARGV.push("-xb", "--file", "boo.txt", "-v")
241
+
242
+ expect{
243
+ @opts = Getopt::Long.getopts(
244
+ ["--expand", "-x", Getopt::BOOLEAN],
245
+ ["--verbose", "-v", Getopt::BOOLEAN],
246
+ ["--file", "-f", Getopt::REQUIRED],
247
+ ["--bar", "-b", Getopt::OPTIONAL]
248
+ )
249
+ }.not_to raise_error
250
+
251
+ expect(@opts).to eq({
252
+ "x"=>true, "expand"=>true,
253
+ "v"=>true, "verbose"=>true,
254
+ "f"=>"boo.txt", "file"=>"boo.txt",
255
+ "b"=>nil, "bar"=>nil
256
+ })
257
+ end
258
+
259
+ example "multiple similar long switches with no short switches works as expected" do
260
+ ARGV.push('--to','1','--too','2','--tooo','3')
261
+
262
+ expect{
263
+ @opts = Getopt::Long.getopts(
264
+ ["--to", Getopt::REQUIRED],
265
+ ["--too", Getopt::REQUIRED],
266
+ ["--tooo", Getopt::REQUIRED]
267
+ )
268
+ }.not_to raise_error
269
+
270
+ expect(@opts['to']).to eq('1')
271
+ expect(@opts['too']).to eq('2')
272
+ expect(@opts['tooo']).to eq('3')
273
+ end
274
+
275
+ after do
276
+ @opts = nil
277
+ ARGV.clear
278
+ end
279
+ end
@@ -0,0 +1,122 @@
1
+ ###################################################################
2
+ # test_getopt_std.rb
3
+ #
4
+ # Test suite for the Getopt::Std class. You should run this test
5
+ # via the 'rake test' task.
6
+ ###################################################################
7
+ require 'rspec'
8
+ require 'getopt/std'
9
+ include Getopt
10
+
11
+ RSpec.describe Getopt::Std do
12
+ example "version" do
13
+ expect(Std::VERSION).to eq('1.6.0')
14
+ expect(Std::VERSION).to be_frozen
15
+ end
16
+
17
+ example "getopts basic functionality" do
18
+ expect(Std).to respond_to(:getopts)
19
+ expect{ Std.getopts("ID") }.not_to raise_error
20
+ expect(Std.getopts("ID")).to be_kind_of(Hash)
21
+ end
22
+
23
+ example "getopts with separated switches" do
24
+ ARGV.push("-I", "-D")
25
+ expect(Std.getopts("ID")).to eq({"I"=>true, "D"=>true})
26
+ end
27
+
28
+ # Inspired by RF bug #23477
29
+ example "getopts with arguments that match switch are ok" do
30
+ ARGV.push("-d", "d")
31
+ expect(Std.getopts("d:")).to eq({"d" => "d"})
32
+
33
+ ARGV.push("-d", "ad")
34
+ expect(Std.getopts("d:")).to eq({"d" => "ad"})
35
+
36
+ ARGV.push("-a", "ad")
37
+ expect(Std.getopts("d:a:")).to eq({"a" => "ad"})
38
+
39
+ ARGV.push("-a", "da")
40
+ expect(Std.getopts("d:a:")).to eq({"a" => "da"})
41
+
42
+ ARGV.push("-a", "d")
43
+ expect(Std.getopts("d:a:")).to eq({"a" => "d"})
44
+
45
+ ARGV.push("-a", "dad")
46
+ expect(Std.getopts("d:a:")).to eq({"a" => "dad"})
47
+
48
+ ARGV.push("-d", "d", "-a", "a")
49
+ expect(Std.getopts("d:a:")).to eq({"d" => "d", "a" => "a"})
50
+ end
51
+
52
+ example "getopts with joined switches" do
53
+ ARGV.push("-ID")
54
+ expect(Std.getopts("ID")).to eq({"I"=>true, "D"=>true})
55
+ end
56
+
57
+ example "getopts with separated switches and mandatory argument" do
58
+ ARGV.push("-o", "hello", "-I", "-D")
59
+ expect(Std.getopts("o:ID")).to eq({"o"=>"hello", "I"=>true, "D"=>true})
60
+ end
61
+
62
+ example "getopts with joined switches and mandatory argument" do
63
+ ARGV.push("-IDo", "hello")
64
+ expect( Std.getopts("o:ID")).to eq({"o"=>"hello", "I"=>true, "D"=>true})
65
+ end
66
+
67
+ example "getopts with no arguments" do
68
+ expect{ Std.getopts("ID") }.not_to raise_error
69
+ expect(Std.getopts("ID")).to eq({})
70
+ expect(Std.getopts("ID")["I"]).to be_nil
71
+ expect(Std.getopts("ID")["D"]).to be_nil
72
+ end
73
+
74
+ # If a switch that accepts an argument appears more than once, the values
75
+ # are rolled into an array.
76
+ example "getopts with switch repeated" do
77
+ ARGV.push("-I", "-I", "-o", "hello", "-o", "world")
78
+ expect( Std.getopts("o:ID")).to eq({"o" => ["hello","world"], "I"=>true})
79
+ end
80
+
81
+ # EXPECTED ERRORS
82
+
83
+ example "getopts raises expected errors when passing a switch to another switch" do
84
+ ARGV.push("-d", "-d")
85
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error)
86
+
87
+ ARGV.push("-d", "-a")
88
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error)
89
+
90
+ ARGV.push("-a", "-d")
91
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error)
92
+
93
+ ARGV.push("-d", "-d")
94
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error, "cannot use switch '-d' as argument to another switch")
95
+ end
96
+
97
+ example "getopts raises expected errors if argument is missing" do
98
+ ARGV.push("-ID")
99
+ expect{ Std.getopts("I:D") }.to raise_error(Std::Error)
100
+
101
+ ARGV.push("-ID")
102
+ expect{ Std.getopts("ID:") }.to raise_error(Std::Error)
103
+ end
104
+
105
+ example "getopts raises expected errors if there are extra arguments" do
106
+ ARGV.push("-I", "-D", "-X")
107
+ expect{ Std.getopts("ID") }.to raise_error(Std::Error)
108
+
109
+ ARGV.push("-IDX")
110
+ expect{ Std.getopts("ID") }.to raise_error(Std::Error, "invalid option 'X'")
111
+ end
112
+
113
+ example "getopts raises expected errors with invalid or no arguments" do
114
+ expect{ Std.getopts }.to raise_error(ArgumentError)
115
+ expect{ Std.getopts(0) }.to raise_error(NoMethodError)
116
+ expect{ Std.getopts(nil) }.to raise_error(NoMethodError)
117
+ end
118
+
119
+ after do
120
+ ARGV.clear
121
+ end
122
+ end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getopt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain:
11
11
  - |
@@ -35,22 +35,36 @@ cert_chain:
35
35
  ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
36
36
  WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
37
37
  -----END CERTIFICATE-----
38
- date:
38
+ date: 2021-03-02 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
- name: test-unit
41
+ name: rake
42
42
  requirement: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 2.5.0
46
+ version: '0'
47
47
  type: :development
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: 2.5.0
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.9'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.9'
54
68
  description: |2
55
69
  The getopt library provides two different command line option parsers.
56
70
  They are meant as easier and more convenient replacements for the
@@ -59,33 +73,26 @@ description: |2
59
73
  email: djberg96@gmail.com
60
74
  executables: []
61
75
  extensions: []
62
- extra_rdoc_files:
63
- - README.rdoc
64
- - CHANGES.rdoc
65
- - MANIFEST.rdoc
76
+ extra_rdoc_files: []
66
77
  files:
78
+ - CHANGES.md
79
+ - Gemfile
67
80
  - LICENSE
68
- - test
69
- - test/test_getopt_long.rb
70
- - test/test_getopt_std.rb
81
+ - MANIFEST.md
82
+ - README.md
71
83
  - Rakefile
72
- - certs
73
84
  - certs/djberg96_pub.pem
74
- - examples
75
- - examples/example_std.rb
76
85
  - examples/example_long.rb
77
- - lib
86
+ - examples/example_std.rb
87
+ - getopt.gemspec
88
+ - lib/getopt-long.rb
89
+ - lib/getopt-std.rb
78
90
  - lib/getopt.rb
79
- - lib/getopt
80
91
  - lib/getopt/long.rb
81
92
  - lib/getopt/std.rb
82
93
  - lib/getopt/version.rb
83
- - lib/getopt-long.rb
84
- - lib/getopt-std.rb
85
- - CHANGES.rdoc
86
- - MANIFEST.rdoc
87
- - getopt.gemspec
88
- - README.rdoc
94
+ - spec/getopt_long_spec.rb
95
+ - spec/getopt_std_spec.rb
89
96
  homepage: https://github.com/djberg96/getopt
90
97
  licenses:
91
98
  - Apache-2.0
@@ -96,7 +103,7 @@ metadata:
96
103
  documentation_uri: https://github.com/djberg96/getopt/wiki
97
104
  source_code_uri: https://github.com/djberg96/getopt
98
105
  wiki_uri: https://github.com/djberg96/getopt/wiki
99
- post_install_message:
106
+ post_install_message:
100
107
  rdoc_options: []
101
108
  require_paths:
102
109
  - lib
@@ -104,17 +111,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
111
  requirements:
105
112
  - - ">="
106
113
  - !ruby/object:Gem::Version
107
- version: 2.2.0
114
+ version: '0'
108
115
  required_rubygems_version: !ruby/object:Gem::Requirement
109
116
  requirements:
110
117
  - - ">="
111
118
  - !ruby/object:Gem::Version
112
119
  version: '0'
113
120
  requirements: []
114
- rubygems_version: 3.0.6
115
- signing_key:
121
+ rubygems_version: 3.0.3
122
+ signing_key:
116
123
  specification_version: 4
117
124
  summary: Getopt::Std and Getopt::Long option parsers for Ruby
118
125
  test_files:
119
- - test/test_getopt_long.rb
120
- - test/test_getopt_std.rb
126
+ - spec/getopt_std_spec.rb
127
+ - spec/getopt_long_spec.rb