switches.rb 0.10.0 → 0.10.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30d6b5c6bbad8c0a585b418ff83ad25394c9de5cbe1a37089484d8c5ca16ad2a
4
- data.tar.gz: a5a3bda6bf97e2c8355c12e8113947a94523abe9f9e8d344f81fa3f6e4947d6f
3
+ metadata.gz: 3522ecf029cdd554e02bb72d5620de851dfeb091421cfa2efea8c9d115f8d935
4
+ data.tar.gz: 75a81b0ca90c8d6125e4bbac37cc5840291b17bbaef5598050db2c97d27a2742
5
5
  SHA512:
6
- metadata.gz: fdb9781dc62a69ec53edb7e2d95ed7f83edc62079512e632af30a49a258831e1b35be02d8e81b74372664cf99d1e724f5f7b3747946dbdd76e5014bd863fae47
7
- data.tar.gz: 5e7bca50700640d86c65c65355ffefa83480641fc7492141046f83d923403b0ac82efcb9da347db19ae0ea2a89ef36d44d195251341fc2ef688561b18f3e15d9
6
+ metadata.gz: 852da32c748e8b771122eb84e54fb4aaba785a0a973c5b377cfa0c89fd36e6095812dfa96bb466ecd47a637fbd994b14fb6868a1db6a1a0c870e97570a389fc6
7
+ data.tar.gz: c6ffc907d26d8219cee5e6b011a1f0746574139867b7ae4249c7589262394be5b13022d28a04cad9cffc6429f62b88a4acb67c4b5c121c12f0af4405a16bc0c5
data/CHANGELOG CHANGED
@@ -1,7 +1,25 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 20260812
4
+
5
+ 0.10.2: Recover the in-source change log into the CHANGELOG, and move the Notes to the README.
6
+
7
+ 1. + CHANGELOG: the 0.9.0 to 0.9.13 series, recovered from the "Changes since 0.8:" log that lived in the lib/Switches.rb header. The items are reproduced as written; the versions carry no reliable dates and are left undated.
8
+ 2. + README.md: a Notes section, carrying the notes from the lib/Switches.rb header — the unusable "-?" switch, the clash between Switches methods and OptionParser methods, the required/optional demarcation, and the bang interface methods.
9
+ 3. - lib/Switches.rb: the "# Changes since 0.8:" log (now in the CHANGELOG) and the "# Notes:" section (now in the README). The rest of the header — Description, Todo, Ideas, Dependencies — stays.
10
+
11
+
3
12
  ## 20260809
4
13
 
14
+ 0.10.1: Get the specs running again, under rspec 3 and rake.
15
+
16
+ 1. ~ spec/all.rb: rspec 3 syntax throughout, in place of the pre-rspec-3 #should ==, so the specs run at all.
17
+ 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.
18
+ 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.
19
+ 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.
20
+ 5. ~ Gemfile: use gemspec rather than restate rspec, and + ostruct, which the bundle must provide for require_gem to find under Ruby 4.0.
21
+ 6. ~ switches.rb.gemspec: + rake as a development dependency and + Rakefile to the file list.
22
+
5
23
  0.10.0: Restore loading and building under Ruby 4.0, and bring the repo up to standard.
6
24
 
7
25
  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.
@@ -19,4 +37,132 @@
19
37
 
20
38
  0.9.14: Turn switches into a gem, convert the README to markdown, and remove the examples directory.
21
39
 
22
- 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.
40
+ The 0.9.0 to 0.9.13 series below is recovered from the "Changes since 0.8:" log that was kept in the lib/Switches.rb header (removed in 0.10.2); the items are reproduced as they were written there. switches began in 2009 and this pre-gem history carries no reliable per-version dates, so these entries are undated. Nothing before 0.9.0 — the 0.4.0 to 0.8.x Options era — is recorded.
41
+
42
+ 0.9.13: Swap a couple of @settings.instance_variable_get(:@table)'s for OpenStruct#to_h's.
43
+
44
+ 1. Swapped out a couple of @settings.instance_variable_get(:@table)'s for OpenStruct#to_h's.
45
+
46
+ 0.9.12: Remove the default aliases for default_to, which clashed with the mail gem's Mail::Message#default.
47
+
48
+ 1. - Object#default.
49
+ 2. - NilClass#default.
50
+
51
+ 0.9.11: Fix the defaults overwriting the supplied settings.
52
+
53
+ 1. ~ Switches#set_switches_with_defaults to check if a setting had been set before applying a default value.
54
+
55
+ 0.9.10: OpenStruct#to_h and Switches#to_h.
56
+
57
+ 1. + OpenStruct#to_h.
58
+ 2. ~ Switches#to_h.
59
+
60
+ 0.9.9: A large batch — full missing-switch reporting, required_perform, default_is, and Array/Module helpers.
61
+
62
+ 1. ~ Switches#check_required_switches, so as it now assembles the complete list of missing switches rather than failing on and reporting only the first.
63
+ 2. + Switches#required_perform
64
+ 3. + Switches#required_perform!
65
+ 4. - OptionParse#soft_parse, since this was never used here, but only in Attributes.
66
+ 5. + Object#default_is(et.al.)
67
+ 6. + NilClass#default_is(et.al.) overrides Object#default_is for the specific case of nil.
68
+ 7. /switches_defaults/switches_with_defaults/.
69
+ 8. + Array#peek_options.
70
+ 9. + Array#poke_options!.
71
+ 10. + Array#all_but_last.
72
+ 11. + Array#last!.
73
+ 12. + Module#alias_methods.
74
+ 13. ~ CastingInterfaceMethods, to make use of Array#poke_options.
75
+ 14. + CastingInterfaceMethods#flag.
76
+ 15. + Switches.as_h (and associated aliased interfaces).
77
+ 16. ~ Switches#initialize, arguments are now a splat with the intention of allowing both casting_interface_methods and as_h being specified as a hash supplied to the initialiser.
78
+ 17. ~ Switches, to make use of Module#alias_methods.
79
+ 18. ~ Switches, to make use of Array#peek_options and Array#poke_options!.
80
+ 19. + Switches#required_perform.
81
+ 20. + Switches#required_perform!.
82
+ 21. + Switches#switches_with_defaults.
83
+ 22. + Switches#switches_with_castings.
84
+ 23. + Swtiches#to_h.
85
+ 24. + Switches#set_if_required_switch.
86
+ 25. + Switches#set_switches_with_defaults.
87
+ 26. ~ Switches#parse!, + set_switches_with_defaults() and + to_h().
88
+ 27. ~ Switches#do_set, + set_if_required_switch().
89
+ 28. ~ Switches#do_action, + set_if_required_switch().
90
+ 29. ~ Switches#on_args, so as it can handle castings.
91
+
92
+ 0.9.8: Add #perform and #perform!, plus #do_action and the #on_args changes to support them.
93
+
94
+ 1. ~ Switches#on_args, so as if a block is supplied which is not returning a String, then it will not be placed into the on_args argument list.
95
+ 2. + Switches#perform, interface for actions which don't require an argument.
96
+ 3. + Switches#perform!, interface for actions which do require an argument.
97
+ 4. + Switches#do_action, executes a block when called by either of #perform or #perform!.
98
+
99
+ 0.9.7: Some small tidyups and removal of the self-run section.
100
+
101
+ 1. ~ Switches#on_args, shorter when handling casting arguments.
102
+ 2. /unused_switches/unset_switches/.
103
+ 3. ~ Switches#check_required_switches, so that the message is not being assigned until a switch is missing.
104
+ 4. Removed the self-run section and moved it to ./test/run.rb.
105
+
106
+ 0.9.6: Fix the setting of defaults overriding supplied switch values.
107
+
108
+ 1. ~ Switches#set_unused_switches, to make use of #switch_defaults and #unused_switches.
109
+ 2. + Switches#switch_defaults, as an interface method since it might be useful for querying at some point?
110
+ 3. + Switches#unused_switches, also as an interface method since it might also be useful for querying at some point?
111
+ 4. ~ Switches#on_args, so as to enable alternate hash keys (:type and :class) for type casting.
112
+ 5. ~ self-run section to reflect the optionalness/optionality(?) of summaries.
113
+
114
+ 0.9.5: Add default values for switches.
115
+
116
+ 1. ~ CastingInterfaceMethods to extract any options before pushing a cast option, since it was overwriting for those casting interface methods.
117
+ 2. ~ Switches#initialize, + @defaults.
118
+ 3. /Switches#set_unused_switches_to_nil/Switches#set_unused_switches/.
119
+ 4. ~ Switches#set_unused_switches, so as it handles setting unused switches with a default.
120
+
121
+ 0.9.4: Add the casting interface methods.
122
+
123
+ 1. + CastingInterfaceMethods#integer(!).
124
+ 2. + CastingInterfaceMethods#float(!).
125
+ 3. + CastingInterfaceMethods#array(!).
126
+ 4. + CastingInterfaceMethods#regex(p)(!).
127
+ 5. + CastingInterfaceMethods#boolean(!).
128
+ 6. ~ Switches#initialize + include_casting_interface_methods.
129
+ 7. /String#short_arg?/String#short_switch?/.
130
+ 8. /String#long_arg?/String#long_switch?/.
131
+ 9. ~ Switches#on_args - boolean_switch.
132
+
133
+ 0.9.3: Rename the library file.
134
+
135
+ 1. /Options.rb/Switches.rb/.
136
+
137
+ 0.9.2: Add casting to nominated Ruby classes.
138
+
139
+ 1. ~ Switches#on_args + options.
140
+ 2. + Array#extract_options!.
141
+ 3. + Switches#allowed.
142
+ 4. + Switches#allowed!.
143
+ 5. + Swithces#mandatory.
144
+ 6. + Switches#necessary.
145
+ 7. ~ Switches#do_set + ...options.
146
+ 8. ~ Switches#on_args + ...options[:cast].
147
+ 9. ~ self-run section to do a simple test of casting.
148
+
149
+ 0.9.1: Remove ! from the switch-setting methods and move those to the Switches interface methods.
150
+
151
+ 1. + Switches#do_set.
152
+ 2. ~ Switches#set.
153
+ 3. + Switches#set!.
154
+ 4. + Switches#required!.
155
+ 5. - String#required_arg?.
156
+ 6. ~ Switches#on_args + requires_argument.
157
+
158
+ 0.9.0: Rename the project from Options to Switches, since there are required switches now and required options don't make sense.
159
+
160
+ 1. /Options/Switches/.
161
+ 2. /RequiredOptionMissing/RequiredSwitchMissing/.
162
+ 3. /String#boolean_arg?/String#boolean_switch?/.
163
+ 4. Other references to options changed to switches.
164
+ 5. + Switches#required/mandatory/necessary.
165
+ 6. + Swtiches#optional.
166
+ 7. + Switches#supplied_switches.
167
+ 8. + Switches#set_unused_switches_to_nil.
168
+ 9. ~ self-run section to reflect new interface.
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rspec'
3
+ gemspec
4
+
5
+ # ostruct is no longer a default gem as of Ruby 4.0, so it must be provided
6
+ # explicitly under bundler.
7
+ gem 'ostruct'
data/README.md CHANGED
@@ -183,6 +183,13 @@ switches.class # => Hash
183
183
 
184
184
  ```
185
185
 
186
+ ## Notes
187
+
188
+ - The switch `-?` can't be used, since there is no Ruby method `#?` for it to map to.
189
+ - A `Switches` instance can't define a method whose name matches an `OptionParser` method, since such calls are forwarded to the underlying `OptionParser` instance.
190
+ - There is a clear demarcation between whether a switch's argument is required and whether the switch itself is required.
191
+ - The bang interface methods (for example `set!` and `required!`) modify the switch in place with a required argument, rather than relying on a default or a value set elsewhere and later.
192
+
186
193
  ## Contributing
187
194
 
188
195
  1. Fork it ( https://github.com/thoran/HTTP/fork )
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
@@ -2,7 +2,7 @@
2
2
  # Switches
3
3
 
4
4
  # 20260809
5
- # 0.10.0
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
 
@@ -15,130 +15,9 @@
15
15
  # 2. Do away with optional arguments entirely. Since when does anyone want to specify a non-boolean switch and then supply no arguments anyway?... OK, maybe sometimes, but this is pretty obscure IMO. OK, bad idea. These can be used as action oriented sub-commands.
16
16
  # 3. Allow for any one of the switches OpenStruct methods to assign values for any of the other associated methods, so as it is more than a read once switch and can be used for storage through out the application; although this might be stepping on Attributes.rb's toes?...
17
17
 
18
- # Notes:
19
- # 1. A limitation is the inability to use the switch, "-?", since there is no Ruby method, #?.
20
- # 2. An additional limitation is that the Switches class cannot have methods which are the same as any OptionParser methods since these will be forwarded to the OptionParser instance, @op.
21
- # 3. In 0.9.0 there is finally now a clear demarcation between switch arguments and switches themselves being mandatory or optional.
22
- # 4. It mostly makes sense that the Switches interface methods are bang methods, since it is modifying that switch in place with a required argument, rather than relying upon a default or otherwise set value elsewhere and presumably later than when the switch was set.
23
-
24
18
  # Dependencies:
25
19
  # 1. Standard Ruby Library.
26
20
 
27
- # Changes since 0.8:
28
- # (Renamed this project/class from Options to Switches since there are required switches now and required options don't make sense.)
29
- # 1. /Options/Switches/.
30
- # 2. /RequiredOptionMissing/RequiredSwitchMissing/.
31
- # 3. /String#boolean_arg?/String#boolean_switch?/.
32
- # 4. Other references to options changed to switches.
33
- # 5. + Switches#required/mandatory/necessary.
34
- # 6. + Swtiches#optional.
35
- # 7. + Switches#supplied_switches.
36
- # 8. + Switches#set_unused_switches_to_nil.
37
- # 9. ~ self-run section to reflect new interface.
38
- # 0/1 (Removed ! from switch setting methods and moved those to the Switches interface methods.)
39
- # 10. + Switches#do_set.
40
- # 11. ~ Switches#set.
41
- # 12. + Switches#set!.
42
- # 13. + Switches#required!.
43
- # 14. - String#required_arg?.
44
- # 15. ~ Switches#on_args + requires_argument.
45
- # 1/2 (Added casting to nominated Ruby classes.)
46
- # 16. ~ Switches#on_args + options.
47
- # 17. + Array#extract_options!.
48
- # 18. + Switches#allowed.
49
- # 19. + Switches#allowed!.
50
- # 20. + Swithces#mandatory.
51
- # 21. + Switches#necessary.
52
- # 22. ~ Switches#do_set + ...options.
53
- # 23. ~ Switches#on_args + ...options[:cast].
54
- # 24. ~ self-run section to do a simple test of casting.
55
- # 2/3 (Renaming the library file.)
56
- # 25. /Options.rb/Switches.rb/.
57
- # 3/4 (+ casting interface methods)
58
- # 26. + CastingInterfaceMethods#integer(!).
59
- # 27. + CastingInterfaceMethods#float(!).
60
- # 28. + CastingInterfaceMethods#array(!).
61
- # 29. + CastingInterfaceMethods#regex(p)(!).
62
- # 30. + CastingInterfaceMethods#boolean(!).
63
- # 31. ~ Switches#initialize + include_casting_interface_methods.
64
- # 32. /String#short_arg?/String#short_switch?/.
65
- # 33. /String#long_arg?/String#long_switch?/.
66
- # 34. ~ Switches#on_args - boolean_switch.
67
- # 4/5 (Added default values for switches.)
68
- # 35. ~ CastingInterfaceMethods to extract any options before pushing a cast option, since it was overwriting for those casting interface methods.
69
- # 36. ~ Switches#initialize, + @defaults.
70
- # 37. /Switches#set_unused_switches_to_nil/Switches#set_unused_switches/.
71
- # 38. ~ Switches#set_unused_switches, so as it handles setting unused switches with a default.
72
- # 5/6 (Setting of defaults was overriding supplied switch values.)
73
- # 39. ~ Switches#set_unused_switches, to make use of #switch_defaults and #unused_switches.
74
- # 40. + Switches#switch_defaults, as an interface method since it might be useful for querying at some point?
75
- # 41. + Switches#unused_switches, also as an interface method since it might also be useful for querying at some point?
76
- # 42. ~ Switches#on_args, so as to enable alternate hash keys (:type and :class) for type casting.
77
- # 43. ~ self-run section to reflect the optionalness/optionality(?) of summaries.
78
- # 6/7 (Some small tidyups and removal of self-run section.)
79
- # 44. ~ Switches#on_args, shorter when handling casting arguments.
80
- # 45. /unused_switches/unset_switches/.
81
- # 46. ~ Switches#check_required_switches, so that the message is not being assigned until a switch is missing.
82
- # 47. Removed the self-run section and moved it to ./test/run.rb.
83
- # 7/8 (Addition of #perform and #perform! methods, as well the #do_action method and changes to #on_args to facilitate #perform* methods.)
84
- # 48. ~ Switches#on_args, so as if a block is supplied which is not returning a String, then it will not be placed into the on_args argument list.
85
- # 49. + Switches#perform, interface for actions which don't require an argument.
86
- # 50. + Switches#perform!, interface for actions which do require an argument.
87
- # 51. + Switches#do_action, executes a block when called by either of #perform or #perform!.
88
- # 8/9
89
- # 52. ~ Switches#check_required_switches, so as it now assembles the complete list of missing switches rather than failing on and reporting only the first.
90
- # 53. + Switches#required_perform
91
- # 54. + Switches#required_perform!
92
- # 55. - OptionParse#soft_parse, since this was never used here, but only in Attributes.
93
- # 56. + Object#default_is(et.al.)
94
- # 57. + NilClass#default_is(et.al.) overrides Object#default_is for the specific case of nil.
95
- # 58. /switches_defaults/switches_with_defaults/.
96
- # 59. + Array#peek_options.
97
- # 60. + Array#poke_options!.
98
- # 61. + Array#all_but_last.
99
- # 62. + Array#last!.
100
- # 63. + Module#alias_methods.
101
- # 64. ~ CastingInterfaceMethods, to make use of Array#poke_options.
102
- # 65. + CastingInterfaceMethods#flag.
103
- # 66. + Switches.as_h (and associated aliased interfaces).
104
- # 67. ~ Switches#initialize, arguments are now a splat with the intention of allowing both casting_interface_methods and as_h being specified as a hash supplied to the initialiser.
105
- # 68. ~ Switches, to make use of Module#alias_methods.
106
- # 69. ~ Switches, to make use of Array#peek_options and Array#poke_options!.
107
- # 70. + Switches#required_perform.
108
- # 71. + Switches#required_perform!.
109
- # 72. + Switches#switches_with_defaults.
110
- # 73. + Switches#switches_with_castings.
111
- # 74. + Swtiches#to_h.
112
- # 75. + Switches#set_if_required_switch.
113
- # 76. + Switches#set_switches_with_defaults.
114
- # 77. ~ Switches#parse!, + set_switches_with_defaults() and + to_h().
115
- # 78. ~ Switches#do_set, + set_if_required_switch().
116
- # 79. ~ Switches#do_action, + set_if_required_switch().
117
- # 80. ~ Switches#on_args, so as it can handle castings.
118
- # 9/10
119
- # 81. + OpenStruct#to_h.
120
- # 82. ~ Switches#to_h.
121
- # 10/11 (Whoops! The defaults were overwriting the supplied settings.)
122
- # 83. ~ Switches#set_switches_with_defaults to check if a setting had been set before applying a default value.
123
- # 10/11
124
- # 84.
125
- # 11/12 (Removed default aliases for default_to, since it clashes with the mail gem's Mail::Message#default method.)
126
- # 85. - Object#default.
127
- # 86. - NilClass#default.
128
- # 12/13
129
- # 87. Swapped out a couple of @settings.instance_variable_get(:@table)'s for OpenStruct#to_h's.
130
- # 13/14
131
- # 88. README is now markdown.
132
- # 89. - examples directory
133
- # 90. Version number bump to 0.9.14.
134
- # 91. Turned into a gem (+ switches.gemspec).
135
- # 14/15
136
- # 92. switches gem name taken, so renamed the gem to switches.rb.
137
- # 93. Version number bump to 0.9.15.
138
- # 94. Tidied some comments in spec/all.rb.
139
- # 15/16
140
- # 95. + Gemfile
141
-
142
21
  require 'optparse'
143
22
  require 'Kernel/require_gem'
144
23
 
data/spec/all.rb CHANGED
@@ -1,25 +1,17 @@
1
- require "#{File.dirname(__FILE__)}/../lib/Switches"
1
+ # spec/all.rb
2
2
 
3
- class Array
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
- it "should return an object of class Switches" do
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
- def setup(switches_string = '')
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 "should set a short boolean switch or flag to true when supplied with a short switch" do
32
- setup('-v').v?.should == true
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 "should set a long boolean switch value to true when supplied with a short switch" do
36
- setup('-v').verbose?.should == true
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 "should set a short boolean switch or flag to true when supplied with a long switch" do
40
- setup('--verbose').v?.should == true
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 "should set a long boolean switch or flag to true when supplied with a long switch" do
44
- setup('--verbose').verbose?.should == true
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 "should set a short switch value to the value supplied when supplied with a short switch" do
48
- setup('-p 22').p.should == '22'
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 "should set a long switch value to the value supplied when supplied with a short switch" do
52
- setup('-p 22').port.should == '22'
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 "should set a short switch value to the value supplied when supplied with a long switch" do
56
- setup('--port 22').p.should == '22'
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 "should set a long switch value to the value supplied when supplied with a long switch" do
60
- setup('--port 22').port.should == '22'
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 "shouldn't matter in which order the short and long switches are given" do
64
- setup('-h example.com').h.should == 'example.com'
65
- setup('--host example.com').h.should == 'example.com'
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
- end # describe "#set"
69
-
59
+ end
60
+
70
61
  describe "#set!" do
71
-
72
- def setup(switches_string = '')
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 "should set a short switch value to the argument supplied with a short switch" do
81
- setup('-p 22').p.should == '22'
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 "should set a long switch value to the argument supplied with a short switch" do
85
- setup('-p 22').port.should == '22'
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 "should set a short switch value to the argument supplied with a long switch" do
89
- setup('--port 22').p.should == '22'
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 "should set a long switch value to the argument supplied with a long switch" do
93
- setup('--port 22').port.should == '22'
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 "should raise a MissingArgument error when no argument is supplied to a short switch" do
97
- switches = lambda{setup('-p')}
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 "should raise a MissingArgument error when no argument is supplied to a long switch" do
102
- switches = lambda{setup('--port')}
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
- end # describe "#set!"
107
-
93
+ end
94
+
108
95
  describe "#required" do
109
-
110
- def setup(switches_string = '')
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 "should set a short switch value to the argument supplied with a short switch" do
119
- setup('-p 22').p.should == '22'
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 "should set a long switch value to the argument supplied with a short switch" do
123
- setup('-p 22').port.should == '22'
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 "should set a short switch value to the argument supplied with a long switch" do
127
- setup('--port 22').p.should == '22'
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 "should set a long switch value to the argument supplied with a long switch" do
131
- setup('--port 22').port.should == '22'
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 "should raise a RequiredSwitchMissing error no switch is supplied" do
135
- switches = lambda{setup}
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
- end # describe "#required"
140
-
127
+ end
128
+
141
129
  describe "#required!" do
142
-
143
- def setup(switches_string = '')
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 "should set a short switch value to the argument supplied with a short switch" do
152
- setup('-p 22').p.should == '22'
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 "should set a long switch value to the argument supplied with a short switch" do
156
- setup('-p 22').port.should == '22'
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 "should set a short switch value to the argument supplied with a long switch" do
160
- setup('--port 22').p.should == '22'
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 "should set a long switch value to the argument supplied with a long switch" do
164
- setup('--port 22').port.should == '22'
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 "should raise a MissingArgument error when no argument is supplied to a short switch" do
168
- switches = lambda{setup('-p')}
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 "should raise a MissingArgument error when no argument is supplied to a long switch" do
173
- switches = lambda{setup('-p')}
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 "should raise a RequiredSwitchMissing error no switch is supplied" do
178
- switches = lambda{setup}
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
- end # describe "#required!"
183
-
165
+ end
166
+
184
167
  describe "#perform" do
185
-
186
- def setup(switches_string = '')
187
- ARGV.empty!
168
+ def build(switches_string = '', &block)
169
+ ARGV.clear
188
170
  switches_string.split.each{|a| ARGV << a}
189
- Switches.new do |s|
190
- s.perform(:action){an_action}
191
- end
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
- def an_action
195
- 'value'
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 "should send(:an_action)"
199
-
200
- end # describe "#perform"
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
- def setup(switches_string = '')
205
- ARGV.empty!
198
+ def build(switches_string = '', &block)
199
+ ARGV.clear
206
200
  switches_string.split.each{|a| ARGV << a}
207
- Switches.new do |s|
208
- s.perform(:action){an_action}
209
- end
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
- def an_action
213
- 'value'
214
- end
215
-
216
- it "should send(:an_action)"
217
-
218
- it "should require a block parameter"
219
-
220
- end # describe "#perform!"
221
-
222
- end # describe Switches
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,7 +1,7 @@
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.10.0'
4
+ s.version = '0.10.2'
5
5
  s.date = '2026-08-09'
6
6
 
7
7
  s.summary = "The easiest way to provide switches to a Ruby program."
@@ -12,12 +12,14 @@ Gem::Specification.new do |s|
12
12
  s.license = 'MIT'
13
13
  s.required_ruby_version = ">= 2.7.0"
14
14
 
15
+ s.add_development_dependency('rake')
15
16
  s.add_development_dependency('rspec')
16
17
 
17
18
  s.files = [
18
19
  'CHANGELOG',
19
20
  'Gemfile',
20
21
  'LICENSE.txt',
22
+ 'Rakefile',
21
23
  'README.md',
22
24
  'switches.rb.gemspec',
23
25
  Dir['lib/**/*.rb'],
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switches.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.10.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -9,6 +9,20 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 2026-08-09 00:00:00.000000000 Z
11
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'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: rspec
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -34,6 +48,7 @@ files:
34
48
  - Gemfile
35
49
  - LICENSE.txt
36
50
  - README.md
51
+ - Rakefile
37
52
  - lib/Kernel/require_gem.rb
38
53
  - lib/Switches.rb
39
54
  - lib/Thoran/Kernel/RequireGem/require_gem.rb
@@ -61,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
76
  - !ruby/object:Gem::Version
62
77
  version: '0'
63
78
  requirements: []
64
- rubygems_version: 4.0.17
79
+ rubygems_version: 4.0.18
65
80
  specification_version: 4
66
81
  summary: The easiest way to provide switches to a Ruby program.
67
82
  test_files: []