switches.rb 0.10.5 → 0.11.0

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: 073acfd47105841bff0a6643eebf2a8f1af398224feac75af268715a7dc666fa
4
- data.tar.gz: ed0d87dd771ff98dbf4da3f8622fc6c6622ad78a630ec3c1f1108b07416c89cf
3
+ metadata.gz: fc49f311d672f64884c36a8b1645d17be83d83df5e630a272a4740db00a48448
4
+ data.tar.gz: 9a5b8accb318737020f7d1c03dfee96ae21529dafe357aabc62abcbd5314dc5a
5
5
  SHA512:
6
- metadata.gz: 5f4d4da5a5632926928b9bb357d1bad5bfc7e34f4e727d59887cd0ead5b7fd0b01c1de5468f563cea9137ca0bc730715026624b85ee71a7bd176ac3fe78193b6
7
- data.tar.gz: 15d4316951b1ddb6e7bb5647e90964b1df3720d7b43d3313034a99f134d41008e91711f7c603654c64b9aba3a43c4e9daa6507333ee6f6a9ee7bbe3001498e8b
6
+ metadata.gz: 0171a8d12cacc99884b6606f08fb5fbf2ac048e5f9d8ed147e3bfe224a2724d2ecb6fc6d077edc9e533a2a7d4dfd2a0fb9c8563f63824ed0cd78506f6c708795
7
+ data.tar.gz: 7888b090acfe50edc7ded58b12b005232be881e0c9489edb07dec7fd126ec7afb8ef1a1cb926682e0eec96bd19efc4a5f26e9a555341f637e3caa60a905f04b8
data/CHANGELOG CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  ## 20260816
4
4
 
5
+ 0.11.0: Forward to OptionParser, which #method_missing has never actually done.
6
+
7
+ 1. ~ lib/Switches.rb: #method_missing tested (@op.methods - Switches.instance_methods).include?(method_name.to_s), which compares a String against an array of Symbols and so was never true. Every call fell through to the settings OpenStruct, which accepts anything, so s.banner = '...' — documented in the README's "With a banner" example — silently became a setting named banner while the parser kept its default of "Usage: <program_name> [options]". The comparison is now made in symbols, and the forwarding works: #banner=, #separator, #program_name= and the other 58 OptionParser methods reach the parser.
8
+ 2. ~ lib/Switches.rb: a declared switch takes precedence over an OptionParser method of the same name, + Switches#declared_switch?. Making the forwarding work otherwise shadows any switch sharing a name with the parser, and OptionParser has version, help, release, load, order, top, base, parse, warn, abort and banner among its 61, several of which are switches a program would reasonably want. The rule is that the program's own declarations beat the wrapper's interface, which also leaves any existing program's switches reading as they did.
9
+ 3. ~ lib/Switches.rb: + Switches::NEVER_FORWARDED, the names Ruby may call implicitly, which are excluded from the forwarded set. The set has always been built by subtraction — everything the parser responds to, less everything Switches defines — which admits any such name Switches does not happen to define. OptionParser#to_a is `summarize("#{banner}".split(/^/))`, so without the exclusion Array(switches) and [*switches] answer with the parser's help. That was latent for as long as the branch was dead and became reachable the moment it was not, so it is part of this fix rather than a consequence of it. to_h was never at risk, but only because Switches happens to define it; the constant makes the exclusion deliberate rather than incidental.
10
+ 4. ~ lib/Switches.rb: + Switches#option_parser_methods, memoised, since the difference was recomputed upon every settings read, and + Switches#option_parser_method?.
11
+ 5. + spec/all.rb: forwarding of a writer, of a method taking an argument, and of a reader, each checked through the parser's own help output rather than by reading back what was written; that a declared switch wins over the OptionParser method of the same name, read and written; that a name which is neither still reaches the settings; and that #to_a, Array() and the splat are answered by the switches rather than the parser.
12
+ 6. ~ README.md: the Notes entry claiming a Switches instance can't define a method whose name matches an OptionParser method now describes what happens, which is the reverse: the switch wins.
13
+
14
+ Not done: #respond_to_missing?, which ordinarily accompanies #method_missing. Note that it would not have prevented the to_a case above — Ruby's conversion check falls back to calling #method_missing directly when the ordinary dispatch fails, so Array() reaches #to_a whatever respond_to? answers. It wants deciding alongside the collision question below, both being about which names the object should own.
15
+
16
+ Still outstanding: the converse fault, where #method_missing does not fire at all because the method exists. A switch named after a method Object already has — :tap, :class, :display, :hash, :method, :type — never reaches the settings, and Kernel#tap without a block raises LocalJumpError nowhere near the declaration.
17
+
5
18
  0.10.5: Specify and document that a multi-word switch may be supplied hyphenated, and name that form when it is missing.
6
19
 
7
20
  1. + spec/all.rb: multi-word switches, in both their underscored and their hyphenated form, for a boolean, an optional argument (#set), a required argument (#set!) and a required switch (#required); that either form counts as supplied for a required switch; and that the setting is named for the symbol declared whichever form was supplied. This behaviour is OptionParser's rather than this library's — a long switch is filed under a lookup key with its underscores turned to hyphens, and whatever is supplied is converted the same way before the lookup, so --update_template and --update-template have always been the one switch. It went untested and undocumented here, which left it resting upon an implementation detail of a library this one only wraps. The specs now pin it, and would fail here rather than in a caller's script were it ever to change.
data/README.md CHANGED
@@ -192,7 +192,7 @@ switches.class # => Hash
192
192
  ## Notes
193
193
 
194
194
  - The switch `-?` can't be used, since there is no Ruby method `#?` for it to map to.
195
- - A `Switches` instance can't define a method whose name matches an `OptionParser` method, since such calls are forwarded to the underlying `OptionParser` instance.
195
+ - A call which is neither a switch nor a `Switches` method is forwarded to the underlying `OptionParser`, which is what makes `s.banner = '...'` and `s.separator '...'` work. A declared switch wins over an `OptionParser` method of the same name, so `s.set(:version)` still reads back through `#version`; without that rule a switch called `--version`, `--help`, `--load` or `--order` would be shadowed by the parser, all four being names `OptionParser` has. The names Ruby may call implicitly — `#to_a`, `#to_s` and the rest of `Switches::NEVER_FORWARDED` — are never forwarded, since `OptionParser#to_a` answers with the help and would otherwise be what `Array(switches)` and `[*switches]` gave you.
196
196
  - There is a clear demarcation between whether a switch's argument is required and whether the switch itself is required.
197
197
  - 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.
198
198
  - A long switch whose name carries an underscore may be supplied in its hyphenated form as well, so `s.boolean(:update_template)` accepts both `--update_template` and `--update-template`. This comes from `OptionParser`, which files a long switch under a lookup key with its underscores turned to hyphens and applies the same conversion to whatever is supplied, so the two forms are one switch rather than two. The setting is named for the symbol declared, and so remains `#update_template?` whichever form was supplied. Declaring the attribute hyphenated, as `:'update-template'`, buys nothing and costs the accessor, which would then be reachable only through `#send`.
@@ -2,5 +2,5 @@
2
2
  # Switches::VERSION
3
3
 
4
4
  class Switches
5
- VERSION = '0.10.5'
5
+ VERSION = '0.11.0'
6
6
  end
data/lib/Switches.rb CHANGED
@@ -153,6 +153,9 @@ class RequiredSwitchMissing < RuntimeError; end
153
153
 
154
154
  class Switches
155
155
 
156
+ # Names Ruby may call implicitly, and which must reach the settings rather than OptionParser. #to_a would otherwise answer Array() and the splat with the parser's help.
157
+ NEVER_FORWARDED = [:to_a, :to_ary, :to_h, :to_hash, :to_s, :to_str, :pretty_print]
158
+
156
159
  class << self
157
160
 
158
161
  def as_h(*args)
@@ -277,13 +280,25 @@ class Switches
277
280
  end
278
281
 
279
282
  def method_missing(method_name, *args, &block)
280
- if (@op.methods - Switches.instance_methods).include?(method_name.to_s)
281
- @op.send(method_name.to_s, *args, &block)
283
+ if option_parser_method?(method_name) && !declared_switch?(method_name)
284
+ @op.send(method_name, *args, &block)
282
285
  else
283
- @settings.send(method_name.to_s, *args, &block)
286
+ @settings.send(method_name, *args, &block)
284
287
  end
285
288
  end
286
289
 
290
+ def option_parser_methods
291
+ @option_parser_methods ||= @op.methods - Switches.instance_methods - NEVER_FORWARDED
292
+ end
293
+
294
+ def option_parser_method?(method_name)
295
+ option_parser_methods.include?(method_name.to_sym)
296
+ end
297
+
298
+ def declared_switch?(method_name)
299
+ @all_switches.include?(method_name.to_s.delete_suffix('='))
300
+ end
301
+
287
302
  def switch_string(attr)
288
303
  attr = attr.to_s
289
304
  "-#{attr.long_switch? ? '-' : ''}#{attr.delete('?')}"
data/spec/all.rb CHANGED
@@ -164,6 +164,57 @@ describe Switches do
164
164
  end
165
165
  end
166
166
 
167
+ describe "OptionParser methods" do
168
+ def build(&block)
169
+ ARGV.clear
170
+ Switches.new(&block)
171
+ end
172
+
173
+ it "forwards a writer to the OptionParser" do
174
+ expect(build{|s| s.banner = 'Usage: tap-audit [options]'}.help).to match(/\AUsage: tap-audit \[options\]/)
175
+ end
176
+
177
+ it "forwards a method taking an argument to the OptionParser" do
178
+ expect(build{|s| s.separator 'Options:'}.help).to match(/^Options:$/)
179
+ end
180
+
181
+ it "forwards a reader to the OptionParser" do
182
+ switches = build{|s| s.program_name = 'tap-audit'}
183
+ expect(switches.program_name).to eq('tap-audit')
184
+ expect(switches.help).to match(/\AUsage: tap-audit/)
185
+ end
186
+
187
+ it "prefers a declared switch to the OptionParser method of the same name" do
188
+ ARGV.clear
189
+ %w{--version 2.1}.each{|a| ARGV << a}
190
+ expect(Switches.new{|s| s.set(:version)}.version).to eq('2.1')
191
+ end
192
+
193
+ it "prefers a declared switch when it is written to as well" do
194
+ switches = build{|s| s.set(:banner)}
195
+ switches.banner = 'a setting, not the banner'
196
+ expect(switches.to_h[:banner]).to eq('a setting, not the banner')
197
+ end
198
+
199
+ it "falls through to the settings for a name which is neither" do
200
+ expect(build{|s| s.set(:port)}.nonesuch).to eq(nil)
201
+ end
202
+
203
+ it "does not forward #to_a, which OptionParser answers with its help" do
204
+ expect(build{|s| s.set(:port)}.to_a).to eq(nil)
205
+ end
206
+
207
+ it "leaves Array() with the switches rather than the parser's help" do
208
+ switches = build{|s| s.set(:port)}
209
+ expect(Array(switches)).to eq([switches])
210
+ end
211
+
212
+ it "leaves the splat with the switches rather than the parser's help" do
213
+ switches = build{|s| s.set(:port)}
214
+ expect([*switches]).to eq([switches])
215
+ end
216
+ end
217
+
167
218
  describe "multi-word switches" do
168
219
  def build(switches_string = '')
169
220
  ARGV.clear
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.5
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran