switches.rb 0.9.15 → 0.10.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG +31 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +3 -22
- data/Rakefile +14 -0
- data/lib/Kernel/require_gem.rb +7 -0
- data/lib/Switches.rb +7 -3
- data/lib/Thoran/Kernel/RequireGem/require_gem.rb +27 -0
- data/spec/all.rb +160 -156
- data/switches.rb.gemspec +18 -4
- metadata +47 -12
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8e900150793418981ab52b35828a2fc696a5e7b826d4eba77047d96cc7532dfe
|
|
4
|
+
data.tar.gz: f4e598c29f7dbaf2c61b8e685151732afba5770b94085cbe06a413f85151b692
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fe23cc64afe81e073164650ef50b64ae208cfc57cc82a9e6e57efc4925f8cd87853b0b5726f2dea6cbe528cf9348fc1499e9e5fe6febf2e9f4348eb96a1b2e9e
|
|
7
|
+
data.tar.gz: 4ab5f810b80fc2716684aaa46481a2e83db7bf628a6a81bdfe25c4f1ed588eaf3fb090fd011e92df3592ec93962e2915b2ec3f0821c4d93081f04624e207a7cc
|
data/CHANGELOG
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# CHANGELOG
|
|
2
|
+
|
|
3
|
+
## 20260809
|
|
4
|
+
|
|
5
|
+
0.10.1: Get the specs running again, under rspec 3 and rake.
|
|
6
|
+
|
|
7
|
+
1. ~ spec/all.rb: rspec 3 syntax throughout, in place of the pre-rspec-3 #should ==, so the specs run at all.
|
|
8
|
+
2. ~ spec/all.rb: the six raise expectations were written as switches.should{raise MissingArgument}, which asserts nothing and names a constant which doesn't exist; they now expect OptionParser::MissingArgument and RequiredSwitchMissing for real.
|
|
9
|
+
3. ~ spec/all.rb: + specs for #perform and #perform!, which had been left pending, and - the Array#empty! alias, since ARGV.clear says it without patching Array.
|
|
10
|
+
4. + Rakefile: an rspec task pointed at spec/all.rb, since the specs live in the one file rather than under the default spec/**/*_spec.rb, + a version task, and spec as the default task.
|
|
11
|
+
5. ~ Gemfile: use gemspec rather than restate rspec, and + ostruct, which the bundle must provide for require_gem to find under Ruby 4.0.
|
|
12
|
+
6. ~ switches.rb.gemspec: + rake as a development dependency and + Rakefile to the file list.
|
|
13
|
+
|
|
14
|
+
0.10.0: Restore loading and building under Ruby 4.0, and bring the repo up to standard.
|
|
15
|
+
|
|
16
|
+
1. ~ lib/Switches.rb: require ostruct through a vendored require_gem (+ lib/Kernel/require_gem.rb, + lib/Thoran/Kernel/RequireGem/require_gem.rb), so it no longer needs ostruct to be a default gem — which it stopped being as of Ruby 4.0 — nor require_gem to be found on RUBYLIB.
|
|
17
|
+
2. ~ switches.rb.gemspec: - has_rdoc=, which RubyGems has removed and which now raises when the gemspec is loaded.
|
|
18
|
+
3. ~ switches.rb.gemspec: + s.license, + s.required_ruby_version (>= 2.7.0), + s.metadata, + rspec as a development dependency, and + Gemfile, LICENSE.txt and CHANGELOG to the file list.
|
|
19
|
+
4. - Gemfile.lock, which a library should not track, and + .gitignore to keep it out.
|
|
20
|
+
5. + LICENSE.txt: the MIT licence.
|
|
21
|
+
6. + CHANGELOG: this file.
|
|
22
|
+
|
|
23
|
+
## 20180404
|
|
24
|
+
|
|
25
|
+
0.9.16: Add a Gemfile.
|
|
26
|
+
|
|
27
|
+
0.9.15: Rename the gem to switches.rb, since the switches name was taken, and tidy the specs.
|
|
28
|
+
|
|
29
|
+
0.9.14: Turn switches into a gem, convert the README to markdown, and remove the examples directory.
|
|
30
|
+
|
|
31
|
+
Nothing before 0.9.14 is recorded here: switches began in 2009, and its pre-gem history — the 0.4.0 to 0.9.13 series noted in the source header — carries no reliable per-version dates.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2009-2026 thoran
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Switches provides for a nice wrapper to OptionParser to also act as a store for
|
|
|
8
8
|
|
|
9
9
|
Add this line to your application's Gemfile:
|
|
10
10
|
|
|
11
|
-
gem 'switches'
|
|
11
|
+
gem 'switches.rb'
|
|
12
12
|
|
|
13
13
|
And then execute:
|
|
14
14
|
|
|
@@ -16,7 +16,7 @@ And then execute:
|
|
|
16
16
|
|
|
17
17
|
Or install it yourself as:
|
|
18
18
|
|
|
19
|
-
$ gem install switches
|
|
19
|
+
$ gem install switches.rb
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
## Usage
|
|
@@ -24,7 +24,6 @@ Or install it yourself as:
|
|
|
24
24
|
```Ruby
|
|
25
25
|
|
|
26
26
|
# With optional switch
|
|
27
|
-
|
|
28
27
|
switches = Switches.new do |s|
|
|
29
28
|
s.set(:a)
|
|
30
29
|
# OR
|
|
@@ -33,7 +32,6 @@ end
|
|
|
33
32
|
switches.a
|
|
34
33
|
|
|
35
34
|
# With argument required
|
|
36
|
-
|
|
37
35
|
switches = Switches.new do |s|
|
|
38
36
|
s.set!(:a)
|
|
39
37
|
# OR
|
|
@@ -42,7 +40,6 @@ end
|
|
|
42
40
|
switches.a
|
|
43
41
|
|
|
44
42
|
# With switch required
|
|
45
|
-
|
|
46
43
|
switches = Switches.new do |s|
|
|
47
44
|
s.set(:a, required: true)
|
|
48
45
|
# OR
|
|
@@ -51,7 +48,6 @@ end
|
|
|
51
48
|
switches.a
|
|
52
49
|
|
|
53
50
|
# With switch and argument required
|
|
54
|
-
|
|
55
51
|
switches = Switches.new do |s|
|
|
56
52
|
s.set!(:a, required: true)
|
|
57
53
|
# OR
|
|
@@ -60,7 +56,6 @@ end
|
|
|
60
56
|
switches.a
|
|
61
57
|
|
|
62
58
|
# With boolean switch
|
|
63
|
-
|
|
64
59
|
switches = Switches.new do |s|
|
|
65
60
|
s.set(:a?)
|
|
66
61
|
# OR
|
|
@@ -69,14 +64,12 @@ end
|
|
|
69
64
|
switches.a?
|
|
70
65
|
|
|
71
66
|
# With a default value
|
|
72
|
-
|
|
73
67
|
switches = Switches.new do |s|
|
|
74
68
|
s.set(:a, default: 31)
|
|
75
69
|
end
|
|
76
70
|
switches.a # => 31
|
|
77
71
|
|
|
78
72
|
# With switch cast to an integer
|
|
79
|
-
|
|
80
73
|
switches = Switches.new(include_casting_interface_methods: true) do |s|
|
|
81
74
|
s.set(:a, cast: Integer)
|
|
82
75
|
# OR
|
|
@@ -89,7 +82,6 @@ end
|
|
|
89
82
|
switches.a.class # => Integer
|
|
90
83
|
|
|
91
84
|
# With switch cast to a float
|
|
92
|
-
|
|
93
85
|
switches = Switches.new(include_casting_interface_methods: true) do |s|
|
|
94
86
|
s.set(:a, cast: Float)
|
|
95
87
|
# OR
|
|
@@ -102,7 +94,6 @@ end
|
|
|
102
94
|
switches.a.class # => Float
|
|
103
95
|
|
|
104
96
|
# With switch cast to an array
|
|
105
|
-
|
|
106
97
|
switches = Switches.new(include_casting_interface_methods: true) do |s|
|
|
107
98
|
s.set(:a, cast: Array)
|
|
108
99
|
# OR
|
|
@@ -115,7 +106,6 @@ end
|
|
|
115
106
|
switches.a.class # => Array
|
|
116
107
|
|
|
117
108
|
# With switch cast to a regex
|
|
118
|
-
|
|
119
109
|
switches = Switches.new(include_casting_interface_methods: true) do |s|
|
|
120
110
|
s.set(:a, cast: Regexp)
|
|
121
111
|
# OR
|
|
@@ -128,7 +118,6 @@ end
|
|
|
128
118
|
switches.a.class # => Regexp
|
|
129
119
|
|
|
130
120
|
# With a default value and cast to an integer
|
|
131
|
-
|
|
132
121
|
switches = Switches.new(include_casting_interface_methods: true) do |s|
|
|
133
122
|
s.set(:a, default: 31, cast: Integer)
|
|
134
123
|
# OR
|
|
@@ -142,19 +131,16 @@ switches.a # => 31 if no argument supplied
|
|
|
142
131
|
switches.a.class # => Integer
|
|
143
132
|
|
|
144
133
|
# With a description of the switch
|
|
145
|
-
|
|
146
134
|
switches = Switches.new do |s|
|
|
147
135
|
s.set(:a){'This is the -a switch.'}
|
|
148
136
|
end
|
|
149
137
|
|
|
150
138
|
# With a banner
|
|
151
|
-
|
|
152
139
|
switches = Switches.new do |s|
|
|
153
140
|
s.banner = 'Here is a banner for the switches.'
|
|
154
141
|
end
|
|
155
142
|
|
|
156
143
|
# Return a hash instead of an object of class Switches
|
|
157
|
-
|
|
158
144
|
switches = Switches.new(to_h: true) do |s|
|
|
159
145
|
s.set(:a)
|
|
160
146
|
end
|
|
@@ -162,31 +148,26 @@ end
|
|
|
162
148
|
switches = Switches.as_h do |s|
|
|
163
149
|
s.set(:a)
|
|
164
150
|
end
|
|
165
|
-
|
|
166
151
|
switches[:a]
|
|
167
152
|
switches.class # => Hash
|
|
168
153
|
|
|
169
154
|
# Without a block
|
|
170
|
-
|
|
171
155
|
switches = Switches.new
|
|
172
156
|
switches.set(:a)
|
|
173
157
|
switches.parse!
|
|
174
158
|
switches.a
|
|
175
159
|
|
|
176
160
|
# With an action
|
|
177
|
-
|
|
178
161
|
switches = Switches.new do |s|
|
|
179
162
|
switches.perform(:a){puts 'a'}
|
|
180
163
|
switches.parse! # => a
|
|
181
164
|
|
|
182
|
-
# With an action uses
|
|
183
|
-
|
|
165
|
+
# With an action which uses an argument
|
|
184
166
|
switches = Switches.new do |s|
|
|
185
167
|
switches.perform(:a){|block_arg| puts block_arg.upcase}
|
|
186
168
|
switches.parse! # => A
|
|
187
169
|
|
|
188
170
|
# With multiple features combined
|
|
189
|
-
|
|
190
171
|
switches = Switches.new(include_casting_interface_methods: true, to_h: true) do |s|
|
|
191
172
|
s.set(:a, default: 31, cast: Integer){'This is the switch -a. It has a default of 31.'}
|
|
192
173
|
# OR
|
data/Rakefile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Rakefile
|
|
2
|
+
|
|
3
|
+
require 'rspec/core/rake_task'
|
|
4
|
+
|
|
5
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
|
6
|
+
t.pattern = 'spec/all.rb'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
task default: :spec
|
|
10
|
+
|
|
11
|
+
desc "Show version"
|
|
12
|
+
task :version do
|
|
13
|
+
puts "switches.rb #{Gem::Specification.load('switches.rb.gemspec').version}"
|
|
14
|
+
end
|
data/lib/Switches.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Switches.rb
|
|
2
2
|
# Switches
|
|
3
3
|
|
|
4
|
-
#
|
|
5
|
-
# 0.
|
|
4
|
+
# 20260809
|
|
5
|
+
# 0.10.1
|
|
6
6
|
|
|
7
7
|
# Description: Switches provides for a nice wrapper to OptionParser to also act as a store for switches supplied.
|
|
8
8
|
|
|
@@ -136,9 +136,13 @@
|
|
|
136
136
|
# 92. switches gem name taken, so renamed the gem to switches.rb.
|
|
137
137
|
# 93. Version number bump to 0.9.15.
|
|
138
138
|
# 94. Tidied some comments in spec/all.rb.
|
|
139
|
+
# 15/16
|
|
140
|
+
# 95. + Gemfile
|
|
139
141
|
|
|
140
142
|
require 'optparse'
|
|
141
|
-
require '
|
|
143
|
+
require 'Kernel/require_gem'
|
|
144
|
+
|
|
145
|
+
require_gem 'ostruct'
|
|
142
146
|
|
|
143
147
|
class OpenStruct
|
|
144
148
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Thoran/Kernel/RequireGem/require_gem.rb
|
|
2
|
+
# Thoran::Kernel::RequireGem#require_gem.rb
|
|
3
|
+
|
|
4
|
+
# 20240613
|
|
5
|
+
# 0.0.0
|
|
6
|
+
|
|
7
|
+
module Thoran
|
|
8
|
+
module Kernel
|
|
9
|
+
module RequireGem
|
|
10
|
+
|
|
11
|
+
def require_gem(load_string, gem_name: nil)
|
|
12
|
+
require load_string
|
|
13
|
+
rescue LoadError
|
|
14
|
+
if gem_name
|
|
15
|
+
system("gem install #{gem_name}")
|
|
16
|
+
else
|
|
17
|
+
system("gem install #{load_string}")
|
|
18
|
+
end
|
|
19
|
+
Gem.clear_paths
|
|
20
|
+
require load_string
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
Kernel.include(Thoran::Kernel::RequireGem)
|
data/spec/all.rb
CHANGED
|
@@ -1,25 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
# spec/all.rb
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
alias_method :empty!, :clear
|
|
6
|
-
|
|
7
|
-
end
|
|
3
|
+
require_relative '../lib/Switches'
|
|
8
4
|
|
|
9
5
|
describe Switches do
|
|
10
|
-
|
|
11
6
|
describe ".new" do
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Switches.new.class.should == Switches
|
|
7
|
+
it "returns an object of class Switches" do
|
|
8
|
+
expect(Switches.new.class).to eq(Switches)
|
|
15
9
|
end
|
|
16
|
-
|
|
17
10
|
end
|
|
18
|
-
|
|
11
|
+
|
|
19
12
|
describe "#set" do
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
ARGV.empty!
|
|
13
|
+
def build(switches_string = '')
|
|
14
|
+
ARGV.clear
|
|
23
15
|
switches_string.split.each{|a| ARGV << a}
|
|
24
16
|
Switches.new do |s|
|
|
25
17
|
s.set :v?, :verbose?
|
|
@@ -27,196 +19,208 @@ describe Switches do
|
|
|
27
19
|
s.set :host, :h
|
|
28
20
|
end
|
|
29
21
|
end
|
|
30
|
-
|
|
31
|
-
it "
|
|
32
|
-
|
|
22
|
+
|
|
23
|
+
it "sets a short boolean switch to true when supplied with a short switch" do
|
|
24
|
+
expect(build('-v').v?).to eq(true)
|
|
33
25
|
end
|
|
34
|
-
|
|
35
|
-
it "
|
|
36
|
-
|
|
26
|
+
|
|
27
|
+
it "sets a long boolean switch to true when supplied with a short switch" do
|
|
28
|
+
expect(build('-v').verbose?).to eq(true)
|
|
37
29
|
end
|
|
38
|
-
|
|
39
|
-
it "
|
|
40
|
-
|
|
30
|
+
|
|
31
|
+
it "sets a short boolean switch to true when supplied with a long switch" do
|
|
32
|
+
expect(build('--verbose').v?).to eq(true)
|
|
41
33
|
end
|
|
42
|
-
|
|
43
|
-
it "
|
|
44
|
-
|
|
34
|
+
|
|
35
|
+
it "sets a long boolean switch to true when supplied with a long switch" do
|
|
36
|
+
expect(build('--verbose').verbose?).to eq(true)
|
|
45
37
|
end
|
|
46
|
-
|
|
47
|
-
it "
|
|
48
|
-
|
|
38
|
+
|
|
39
|
+
it "sets a short switch to the value supplied when supplied with a short switch" do
|
|
40
|
+
expect(build('-p 22').p).to eq('22')
|
|
49
41
|
end
|
|
50
|
-
|
|
51
|
-
it "
|
|
52
|
-
|
|
42
|
+
|
|
43
|
+
it "sets a long switch to the value supplied when supplied with a short switch" do
|
|
44
|
+
expect(build('-p 22').port).to eq('22')
|
|
53
45
|
end
|
|
54
|
-
|
|
55
|
-
it "
|
|
56
|
-
|
|
46
|
+
|
|
47
|
+
it "sets a short switch to the value supplied when supplied with a long switch" do
|
|
48
|
+
expect(build('--port 22').p).to eq('22')
|
|
57
49
|
end
|
|
58
|
-
|
|
59
|
-
it "
|
|
60
|
-
|
|
50
|
+
|
|
51
|
+
it "sets a long switch to the value supplied when supplied with a long switch" do
|
|
52
|
+
expect(build('--port 22').port).to eq('22')
|
|
61
53
|
end
|
|
62
|
-
|
|
63
|
-
it "
|
|
64
|
-
|
|
65
|
-
|
|
54
|
+
|
|
55
|
+
it "doesn't matter in which order the short and long switches are given" do
|
|
56
|
+
expect(build('-h example.com').h).to eq('example.com')
|
|
57
|
+
expect(build('--host example.com').h).to eq('example.com')
|
|
66
58
|
end
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
end
|
|
60
|
+
|
|
70
61
|
describe "#set!" do
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
ARGV.empty!
|
|
62
|
+
def build(switches_string = '')
|
|
63
|
+
ARGV.clear
|
|
74
64
|
switches_string.split.each{|a| ARGV << a}
|
|
75
65
|
Switches.new do |s|
|
|
76
66
|
s.set! :p, :port
|
|
77
67
|
end
|
|
78
68
|
end
|
|
79
|
-
|
|
80
|
-
it "
|
|
81
|
-
|
|
69
|
+
|
|
70
|
+
it "sets a short switch to the argument supplied with a short switch" do
|
|
71
|
+
expect(build('-p 22').p).to eq('22')
|
|
82
72
|
end
|
|
83
|
-
|
|
84
|
-
it "
|
|
85
|
-
|
|
73
|
+
|
|
74
|
+
it "sets a long switch to the argument supplied with a short switch" do
|
|
75
|
+
expect(build('-p 22').port).to eq('22')
|
|
86
76
|
end
|
|
87
|
-
|
|
88
|
-
it "
|
|
89
|
-
|
|
77
|
+
|
|
78
|
+
it "sets a short switch to the argument supplied with a long switch" do
|
|
79
|
+
expect(build('--port 22').p).to eq('22')
|
|
90
80
|
end
|
|
91
|
-
|
|
92
|
-
it "
|
|
93
|
-
|
|
81
|
+
|
|
82
|
+
it "sets a long switch to the argument supplied with a long switch" do
|
|
83
|
+
expect(build('--port 22').port).to eq('22')
|
|
94
84
|
end
|
|
95
|
-
|
|
96
|
-
it "
|
|
97
|
-
|
|
98
|
-
switches.should{raise MissingArgument}
|
|
85
|
+
|
|
86
|
+
it "raises OptionParser::MissingArgument when no argument is supplied to a short switch" do
|
|
87
|
+
expect{build('-p')}.to raise_error(OptionParser::MissingArgument)
|
|
99
88
|
end
|
|
100
|
-
|
|
101
|
-
it "
|
|
102
|
-
|
|
103
|
-
switches.should{raise MissingArgument}
|
|
89
|
+
|
|
90
|
+
it "raises OptionParser::MissingArgument when no argument is supplied to a long switch" do
|
|
91
|
+
expect{build('--port')}.to raise_error(OptionParser::MissingArgument)
|
|
104
92
|
end
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
93
|
+
end
|
|
94
|
+
|
|
108
95
|
describe "#required" do
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
ARGV.empty!
|
|
96
|
+
def build(switches_string = '')
|
|
97
|
+
ARGV.clear
|
|
112
98
|
switches_string.split.each{|a| ARGV << a}
|
|
113
99
|
Switches.new do |s|
|
|
114
100
|
s.required :p, :port
|
|
115
101
|
end
|
|
116
102
|
end
|
|
117
|
-
|
|
118
|
-
it "
|
|
119
|
-
|
|
103
|
+
|
|
104
|
+
it "sets a short switch to the argument supplied with a short switch" do
|
|
105
|
+
expect(build('-p 22').p).to eq('22')
|
|
120
106
|
end
|
|
121
|
-
|
|
122
|
-
it "
|
|
123
|
-
|
|
107
|
+
|
|
108
|
+
it "sets a long switch to the argument supplied with a short switch" do
|
|
109
|
+
expect(build('-p 22').port).to eq('22')
|
|
124
110
|
end
|
|
125
|
-
|
|
126
|
-
it "
|
|
127
|
-
|
|
111
|
+
|
|
112
|
+
it "sets a short switch to the argument supplied with a long switch" do
|
|
113
|
+
expect(build('--port 22').p).to eq('22')
|
|
128
114
|
end
|
|
129
|
-
|
|
130
|
-
it "
|
|
131
|
-
|
|
115
|
+
|
|
116
|
+
it "sets a long switch to the argument supplied with a long switch" do
|
|
117
|
+
expect(build('--port 22').port).to eq('22')
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it "raises RequiredSwitchMissing when no switch is supplied" do
|
|
121
|
+
expect{build}.to raise_error(RequiredSwitchMissing)
|
|
132
122
|
end
|
|
133
|
-
|
|
134
|
-
it "
|
|
135
|
-
|
|
136
|
-
switches.should{raise RequiredSwitchMissing}
|
|
123
|
+
|
|
124
|
+
it "names every missing switch in the RequiredSwitchMissing message" do
|
|
125
|
+
expect{build}.to raise_error(RequiredSwitchMissing, /-p.*--port/m)
|
|
137
126
|
end
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
127
|
+
end
|
|
128
|
+
|
|
141
129
|
describe "#required!" do
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
ARGV.empty!
|
|
130
|
+
def build(switches_string = '')
|
|
131
|
+
ARGV.clear
|
|
145
132
|
switches_string.split.each{|a| ARGV << a}
|
|
146
133
|
Switches.new do |s|
|
|
147
134
|
s.required! :p, :port
|
|
148
135
|
end
|
|
149
136
|
end
|
|
150
|
-
|
|
151
|
-
it "
|
|
152
|
-
|
|
137
|
+
|
|
138
|
+
it "sets a short switch to the argument supplied with a short switch" do
|
|
139
|
+
expect(build('-p 22').p).to eq('22')
|
|
153
140
|
end
|
|
154
|
-
|
|
155
|
-
it "
|
|
156
|
-
|
|
141
|
+
|
|
142
|
+
it "sets a long switch to the argument supplied with a short switch" do
|
|
143
|
+
expect(build('-p 22').port).to eq('22')
|
|
157
144
|
end
|
|
158
|
-
|
|
159
|
-
it "
|
|
160
|
-
|
|
145
|
+
|
|
146
|
+
it "sets a short switch to the argument supplied with a long switch" do
|
|
147
|
+
expect(build('--port 22').p).to eq('22')
|
|
161
148
|
end
|
|
162
|
-
|
|
163
|
-
it "
|
|
164
|
-
|
|
149
|
+
|
|
150
|
+
it "sets a long switch to the argument supplied with a long switch" do
|
|
151
|
+
expect(build('--port 22').port).to eq('22')
|
|
165
152
|
end
|
|
166
|
-
|
|
167
|
-
it "
|
|
168
|
-
|
|
169
|
-
switches.should{raise MissingArgument}
|
|
153
|
+
|
|
154
|
+
it "raises OptionParser::MissingArgument when no argument is supplied to a short switch" do
|
|
155
|
+
expect{build('-p')}.to raise_error(OptionParser::MissingArgument)
|
|
170
156
|
end
|
|
171
|
-
|
|
172
|
-
it "
|
|
173
|
-
|
|
174
|
-
switches.should{raise MissingArgument}
|
|
157
|
+
|
|
158
|
+
it "raises OptionParser::MissingArgument when no argument is supplied to a long switch" do
|
|
159
|
+
expect{build('--port')}.to raise_error(OptionParser::MissingArgument)
|
|
175
160
|
end
|
|
176
|
-
|
|
177
|
-
it "
|
|
178
|
-
|
|
179
|
-
switches.should{raise RequiredSwitchMissing}
|
|
161
|
+
|
|
162
|
+
it "raises RequiredSwitchMissing when no switch is supplied" do
|
|
163
|
+
expect{build}.to raise_error(RequiredSwitchMissing)
|
|
180
164
|
end
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
165
|
+
end
|
|
166
|
+
|
|
184
167
|
describe "#perform" do
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
ARGV.empty!
|
|
168
|
+
def build(switches_string = '', &block)
|
|
169
|
+
ARGV.clear
|
|
188
170
|
switches_string.split.each{|a| ARGV << a}
|
|
189
|
-
Switches.new
|
|
190
|
-
|
|
191
|
-
|
|
171
|
+
Switches.new{|s| s.perform(:a, :action, &block)}
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
it "calls the block when supplied with a short switch" do
|
|
175
|
+
called = false
|
|
176
|
+
build('-a'){called = true}
|
|
177
|
+
expect(called).to eq(true)
|
|
192
178
|
end
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
179
|
+
|
|
180
|
+
it "calls the block when supplied with a long switch" do
|
|
181
|
+
called = false
|
|
182
|
+
build('--action'){called = true}
|
|
183
|
+
expect(called).to eq(true)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it "doesn't call the block when the switch isn't supplied" do
|
|
187
|
+
called = false
|
|
188
|
+
build{called = true}
|
|
189
|
+
expect(called).to eq(false)
|
|
196
190
|
end
|
|
197
|
-
|
|
198
|
-
it "
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
191
|
+
|
|
192
|
+
it "doesn't require an argument" do
|
|
193
|
+
expect{build('-a'){}}.not_to raise_error
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
202
197
|
describe "#perform!" do
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
ARGV.empty!
|
|
198
|
+
def build(switches_string = '', &block)
|
|
199
|
+
ARGV.clear
|
|
206
200
|
switches_string.split.each{|a| ARGV << a}
|
|
207
|
-
Switches.new
|
|
208
|
-
|
|
209
|
-
|
|
201
|
+
Switches.new{|s| s.perform!(:a, :action, &block)}
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it "passes the argument to the block when supplied with a short switch" do
|
|
205
|
+
argument = nil
|
|
206
|
+
build('-a value'){|o| argument = o}
|
|
207
|
+
expect(argument).to eq('value')
|
|
210
208
|
end
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
it "
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
end
|
|
209
|
+
|
|
210
|
+
it "passes the argument to the block when supplied with a long switch" do
|
|
211
|
+
argument = nil
|
|
212
|
+
build('--action value'){|o| argument = o}
|
|
213
|
+
expect(argument).to eq('value')
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
it "doesn't call the block when the switch isn't supplied" do
|
|
217
|
+
called = false
|
|
218
|
+
build{called = true}
|
|
219
|
+
expect(called).to eq(false)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
it "raises OptionParser::MissingArgument when no argument is supplied" do
|
|
223
|
+
expect{build('-a'){}}.to raise_error(OptionParser::MissingArgument)
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
data/switches.rb.gemspec
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
Gem::Specification.new do |s|
|
|
2
2
|
s.name = 'switches.rb' # I would have preferred 'switches', but there's a gem with the name of switches.
|
|
3
3
|
|
|
4
|
-
s.version = '0.
|
|
5
|
-
s.date = '
|
|
4
|
+
s.version = '0.10.1'
|
|
5
|
+
s.date = '2026-08-09'
|
|
6
6
|
|
|
7
7
|
s.summary = "The easiest way to provide switches to a Ruby program."
|
|
8
8
|
s.description = "Switches provides for a nice wrapper to OptionParser to also act as a store for switches supplied."
|
|
9
9
|
s.author = 'thoran'
|
|
10
10
|
s.email = 'code@thoran.com'
|
|
11
|
-
s.homepage = "
|
|
11
|
+
s.homepage = "https://github.com/thoran/switches"
|
|
12
|
+
s.license = 'MIT'
|
|
13
|
+
s.required_ruby_version = ">= 2.7.0"
|
|
14
|
+
|
|
15
|
+
s.add_development_dependency('rake')
|
|
16
|
+
s.add_development_dependency('rspec')
|
|
12
17
|
|
|
13
18
|
s.files = [
|
|
19
|
+
'CHANGELOG',
|
|
20
|
+
'Gemfile',
|
|
21
|
+
'LICENSE.txt',
|
|
22
|
+
'Rakefile',
|
|
14
23
|
'README.md',
|
|
15
24
|
'switches.rb.gemspec',
|
|
16
25
|
Dir['lib/**/*.rb'],
|
|
@@ -19,5 +28,10 @@ Gem::Specification.new do |s|
|
|
|
19
28
|
|
|
20
29
|
s.require_paths = ['lib']
|
|
21
30
|
|
|
22
|
-
s.
|
|
31
|
+
s.metadata = {
|
|
32
|
+
"bug_tracker_uri" => "https://github.com/thoran/switches/issues",
|
|
33
|
+
"changelog_uri" => "https://github.com/thoran/switches/blob/master/CHANGELOG",
|
|
34
|
+
"source_code_uri" => "https://github.com/thoran/switches",
|
|
35
|
+
"documentation_uri" => "https://github.com/thoran/switches/blob/master/README.md"
|
|
36
|
+
}
|
|
23
37
|
end
|
metadata
CHANGED
|
@@ -1,15 +1,42 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: switches.rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thoran
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
10
|
+
date: 2026-08-09 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rake
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rspec
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
13
40
|
description: Switches provides for a nice wrapper to OptionParser to also act as a
|
|
14
41
|
store for switches supplied.
|
|
15
42
|
email: code@thoran.com
|
|
@@ -17,14 +44,24 @@ executables: []
|
|
|
17
44
|
extensions: []
|
|
18
45
|
extra_rdoc_files: []
|
|
19
46
|
files:
|
|
47
|
+
- CHANGELOG
|
|
48
|
+
- Gemfile
|
|
49
|
+
- LICENSE.txt
|
|
20
50
|
- README.md
|
|
51
|
+
- Rakefile
|
|
52
|
+
- lib/Kernel/require_gem.rb
|
|
21
53
|
- lib/Switches.rb
|
|
54
|
+
- lib/Thoran/Kernel/RequireGem/require_gem.rb
|
|
22
55
|
- spec/all.rb
|
|
23
56
|
- switches.rb.gemspec
|
|
24
|
-
homepage:
|
|
25
|
-
licenses:
|
|
26
|
-
|
|
27
|
-
|
|
57
|
+
homepage: https://github.com/thoran/switches
|
|
58
|
+
licenses:
|
|
59
|
+
- MIT
|
|
60
|
+
metadata:
|
|
61
|
+
bug_tracker_uri: https://github.com/thoran/switches/issues
|
|
62
|
+
changelog_uri: https://github.com/thoran/switches/blob/master/CHANGELOG
|
|
63
|
+
source_code_uri: https://github.com/thoran/switches
|
|
64
|
+
documentation_uri: https://github.com/thoran/switches/blob/master/README.md
|
|
28
65
|
rdoc_options: []
|
|
29
66
|
require_paths:
|
|
30
67
|
- lib
|
|
@@ -32,16 +69,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
32
69
|
requirements:
|
|
33
70
|
- - ">="
|
|
34
71
|
- !ruby/object:Gem::Version
|
|
35
|
-
version:
|
|
72
|
+
version: 2.7.0
|
|
36
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
74
|
requirements:
|
|
38
75
|
- - ">="
|
|
39
76
|
- !ruby/object:Gem::Version
|
|
40
77
|
version: '0'
|
|
41
78
|
requirements: []
|
|
42
|
-
|
|
43
|
-
rubygems_version: 2.7.4
|
|
44
|
-
signing_key:
|
|
79
|
+
rubygems_version: 4.0.17
|
|
45
80
|
specification_version: 4
|
|
46
81
|
summary: The easiest way to provide switches to a Ruby program.
|
|
47
82
|
test_files: []
|