getopt 1.4.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,19 +1,16 @@
1
1
  require 'rake'
2
2
  require 'rake/clean'
3
- require 'rake/testtask'
3
+ require 'rspec/core/rake_task'
4
4
 
5
5
  CLEAN.include("**/*.gem", "**/*.rbc")
6
6
 
7
7
  namespace :gem do
8
8
  desc "Create the getopt gem"
9
9
  task :create => [:clean] do
10
+ require 'rubygems/package'
10
11
  spec = eval(IO.read('getopt.gemspec'))
11
- if Gem::VERSION < "2.0"
12
- Gem::Builder.new(spec).build
13
- else
14
- require 'rubygems/package'
15
- Gem::Package.build(spec)
16
- end
12
+ spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
13
+ Gem::Package.build(spec)
17
14
  end
18
15
 
19
16
  desc "Install the getopt gem"
@@ -23,23 +20,18 @@ namespace :gem do
23
20
  end
24
21
  end
25
22
 
26
- Rake::TestTask.new do |t|
27
- t.warning = true
28
- t.verbose = true
29
- end
23
+ namespace :spec do
24
+ RSpec::Core::RakeTask.new(:all) do |t|
25
+ t.pattern = FileList['spec/*_spec.rb']
26
+ end
30
27
 
31
- namespace :test do
32
- Rake::TestTask.new('getopt_long') do |t|
33
- t.test_files = 'test/test_getopt_long.rb'
34
- t.warning = true
35
- t.verbose = true
28
+ RSpec::Core::RakeTask.new(:getopt_long) do |t|
29
+ t.pattern = FileList['spec/*long_spec.rb']
36
30
  end
37
31
 
38
- Rake::TestTask.new('getopt_std') do |t|
39
- t.test_files = 'test/test_getopt_std.rb'
40
- t.warning = true
41
- t.verbose = true
32
+ RSpec::Core::RakeTask.new(:getopt_std) do |t|
33
+ t.pattern = FileList['spec/*std_spec.rb']
42
34
  end
43
35
  end
44
36
 
45
- task :default => :test
37
+ task :default => 'spec:all'
@@ -0,0 +1,26 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEcDCCAtigAwIBAgIBATANBgkqhkiG9w0BAQsFADA/MREwDwYDVQQDDAhkamJl
3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aazxxKTsVVpZD8QZxvK91OQ
19
+ h9zr9JBYqCLcCVbr8SkmYCi/laxIZxsNE5YI8cC8vvlLI7AMgSfPSnn/Epq1GjGY
20
+ 6L1iRcEDtanGCIvjqlCXO9+BmsnCfEVehqZkQHeYczA03tpOWb6pon2wzvMKSsKH
21
+ ks0ApVdstSLz1kzzAqem/uHdG9FyXdbTAwH1G4ZPv69sQAFAOCgAqYmdnzedsQtE
22
+ 1LQfaQrx0twO+CZJPcRLEESjq8ScQxWRRkfuh2VeR7cEU7L7KqT10mtUwrvw7APf
23
+ DYoeCY9KyjIBjQXfbj2ke5u1hZj94Fsq9FfbEQg8ygCgwThnmkTrrKEiMSs3alYR
24
+ ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
25
+ WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
26
+ -----END CERTIFICATE-----
data/getopt.gemspec CHANGED
@@ -2,18 +2,27 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'getopt'
5
- spec.version = '1.4.2'
5
+ spec.version = '1.6.0'
6
6
  spec.author = 'Daniel J. Berger'
7
- spec.license = 'Artistic 2.0'
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
+ spec.cert_chain = Dir['certs/*']
13
14
 
14
- spec.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
15
-
16
- spec.add_development_dependency('test-unit', '>= 2.5.0')
15
+ spec.add_development_dependency('rake')
16
+ spec.add_development_dependency('rspec', '~> 3.9')
17
+
18
+ spec.metadata = {
19
+ 'homepage_uri' => 'https://github.com/djberg96/getopt',
20
+ 'bug_tracker_uri' => 'https://github.com/djberg96/getopt/issues',
21
+ 'changelog_uri' => 'https://github.com/djberg96/getopt/blob/master/CHANGES',
22
+ 'documentation_uri' => 'https://github.com/djberg96/getopt/wiki',
23
+ 'source_code_uri' => 'https://github.com/djberg96/getopt',
24
+ 'wiki_uri' => 'https://github.com/djberg96/getopt/wiki'
25
+ }
17
26
 
18
27
  spec.description = <<-EOF
19
28
  The getopt library provides two different command line option parsers.
@@ -0,0 +1 @@
1
+ require_relative 'getopt/long'
data/lib/getopt-std.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'getopt/std'
data/lib/getopt.rb ADDED
@@ -0,0 +1,2 @@
1
+ require_relative 'getopt/std'
2
+ require_relative 'getopt/long'
data/lib/getopt/long.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative 'version'
2
+
1
3
  # The Getopt module serves as a namespace only
2
4
  module Getopt
3
5
 
@@ -12,13 +14,11 @@ module Getopt
12
14
 
13
15
  # The Getopt::Long class encapsulates longhanded parameter parsing options
14
16
  class Long
17
+ include Getopt::Version
15
18
 
16
19
  # Error raised if an illegal option or argument is passed
17
20
  class Error < StandardError; end
18
21
 
19
- # The version of the getopt library
20
- VERSION = '1.4.2'
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
24
24
  # containing each switch name, minus the '-', as a key. The value
@@ -59,7 +59,7 @@ module Getopt
59
59
  valid.push(switch[0]) # Set valid long switches
60
60
 
61
61
  # Set type for long switch, default to BOOLEAN.
62
- if switch[1].kind_of?(Fixnum)
62
+ if switch[1].kind_of?(Integer)
63
63
  switch[2] = switch[1]
64
64
  types[switch[0]] = switch[2]
65
65
  switch[1] = switch[0][1..2]
@@ -75,7 +75,7 @@ module Getopt
75
75
  syns[switch[0]] = switch[1] unless syns[switch[1]]
76
76
  syns[switch[1]] = switch[0] unless syns[switch[1]]
77
77
 
78
- switch[1] = [switch[1]] if RUBY_VERSION.to_f >= 1.9
78
+ switch[1] = [switch[1]]
79
79
 
80
80
  switch[1].each{ |char|
81
81
  types[char] = switch[2] # Set type for short switch
@@ -217,7 +217,7 @@ module Getopt
217
217
  # the same value
218
218
  hash.dup.each{ |switch, val|
219
219
  if syns.keys.include?(switch)
220
- syns[switch] = [syns[switch]] if RUBY_VERSION.to_f >= 1.9
220
+ syns[switch] = [syns[switch]]
221
221
  syns[switch].each{ |key|
222
222
  hash[key] = val
223
223
  }
data/lib/getopt/std.rb CHANGED
@@ -1,17 +1,17 @@
1
+ require_relative 'version'
2
+
1
3
  # The Getopt module serves as a namespace only
2
4
  module Getopt
3
5
 
4
6
  # The Getopt::Std class serves as a base class for the getopts method.
5
7
  class Std
8
+ include Getopt::Version
6
9
 
7
10
  # The Getopt::Std::Error class is raised if there are any illegal
8
11
  # command line arguments.
9
12
  #
10
13
  class Error < StandardError; end
11
14
 
12
- # The version of the getopt library
13
- VERSION = '1.4.2'
14
-
15
15
  # Processes single character command line options with option
16
16
  # clustering. This information is parsed from ARGV and returned
17
17
  # as a hash, with the switch (minus the "-") as the key. The value
@@ -0,0 +1,6 @@
1
+ module Getopt
2
+ module Version
3
+ # The version of the getopt library
4
+ VERSION = '1.6.0'.freeze
5
+ end
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