switches.rb 0.10.4 → 0.10.5

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: ea74840ee7d4c44d9a22e7e6c4464eb0417fd4ac0d65e0683465600188e2d903
4
- data.tar.gz: 7f68a8a35001b0ff0445d605f276b3205c0651e3ab5a582e970fe5d4a771ea0a
3
+ metadata.gz: 073acfd47105841bff0a6643eebf2a8f1af398224feac75af268715a7dc666fa
4
+ data.tar.gz: ed0d87dd771ff98dbf4da3f8622fc6c6622ad78a630ec3c1f1108b07416c89cf
5
5
  SHA512:
6
- metadata.gz: 284df213837788db6c63752a357d763e16a84ea37331eb405365a6754335e8372a70a7dc880caddd3f6220d4530ccf901a4bfff12e3e9482ebbc7b22433e0bf2
7
- data.tar.gz: 2b067b99909da06acaa0e366dae1ac41307f5347cd44383a3a036bac06e21eac521b6d670ac2ff07b6eac6f3332f9b07ddcee590c51e1466f039823569465f94
6
+ metadata.gz: 5f4d4da5a5632926928b9bb357d1bad5bfc7e34f4e727d59887cd0ead5b7fd0b01c1de5468f563cea9137ca0bc730715026624b85ee71a7bd176ac3fe78193b6
7
+ data.tar.gz: 15d4316951b1ddb6e7bb5647e90964b1df3720d7b43d3313034a99f134d41008e91711f7c603654c64b9aba3a43c4e9daa6507333ee6f6a9ee7bbe3001498e8b
data/CHANGELOG CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## 20260816
4
4
 
5
+ 0.10.5: Specify and document that a multi-word switch may be supplied hyphenated, and name that form when it is missing.
6
+
7
+ 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.
8
+ 2. ~ lib/Switches.rb: the missing-switch message names the hyphenated form, so a switch declared :access_token is reported as --access-token, in place of the --access_token built from the symbol verbatim. Cosmetic, and it says the form to prefer at a command line.
9
+ 3. ~ lib/Switches.rb: + Switches#switch_string, which builds the switch string for an attribute, in place of the identical construction #on_args and #check_required_switches each carried.
10
+ 4. ~ README.md: + a multi-word switch to the Usage section, and + a note recording where the behaviour comes from, that the setting is named for the symbol declared, and that declaring the attribute hyphenated buys nothing and costs the accessor.
11
+
5
12
  0.10.4: Declare ostruct as a runtime dependency, in place of installing it at require time.
6
13
 
7
14
  1. ~ lib/Switches.rb: require 'ostruct', in place of the require_gem 'ostruct' introduced in 0.10.0. require_gem rescues the LoadError and shells out to gem install, so the dependency went unannounced and was satisfied silently into whatever GEM_HOME was set — which, under a Homebrew formula's wrapper, means the program installing a gem into its own keg. A library should announce what it needs and fail loudly without it.
data/README.md CHANGED
@@ -135,6 +135,12 @@ switches = Switches.new do |s|
135
135
  s.set(:a){'This is the -a switch.'}
136
136
  end
137
137
 
138
+ # With a multi-word switch, which may be supplied in either its underscored or its hyphenated form
139
+ switches = Switches.new do |s|
140
+ s.boolean(:update_template)
141
+ end
142
+ # --update_template and --update-template both set it, and the setting remains #update_template?
143
+
138
144
  # With a banner
139
145
  switches = Switches.new do |s|
140
146
  s.banner = 'Here is a banner for the switches.'
@@ -189,6 +195,7 @@ switches.class # => Hash
189
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.
190
196
  - There is a clear demarcation between whether a switch's argument is required and whether the switch itself is required.
191
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
+ - 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`.
192
199
 
193
200
  ## Contributing
194
201
 
@@ -2,5 +2,5 @@
2
2
  # Switches::VERSION
3
3
 
4
4
  class Switches
5
- VERSION = '0.10.4'
5
+ VERSION = '0.10.5'
6
6
  end
data/lib/Switches.rb CHANGED
@@ -284,12 +284,17 @@ class Switches
284
284
  end
285
285
  end
286
286
 
287
+ def switch_string(attr)
288
+ attr = attr.to_s
289
+ "-#{attr.long_switch? ? '-' : ''}#{attr.delete('?')}"
290
+ end
291
+
287
292
  def on_args(requires_argument, options, *attrs, &block)
288
293
  on_args = []
289
294
  attrs.collect{|e| e.to_s}.each do |attr|
290
295
  @defaults[attr] = options[:default] if options[:default]
291
296
  @castings[attr] = (options[:cast] || options[:type] || options[:class]) if (options[:cast] || options[:type] || options[:class])
292
- on_args << "-#{attr.long_switch? ? '-' : ''}#{attr.to_s.delete('?')}"
297
+ on_args << switch_string(attr)
293
298
  end
294
299
  if requires_argument
295
300
  on_args << (on_args.pop + ' REQUIRED_ARGUMENT')
@@ -310,7 +315,7 @@ class Switches
310
315
  messages = []
311
316
  @required_switches.each do |required_switch|
312
317
  unless supplied_switches.include?(required_switch)
313
- messages << "required switch, -#{required_switch.long_switch? ? '-' : ''}#{required_switch.to_s.delete('?')}, is missing"
318
+ messages << "required switch, #{switch_string(required_switch).tr('_', '-')}, is missing"
314
319
  end
315
320
  end
316
321
  unless messages.empty?
data/spec/all.rb CHANGED
@@ -164,6 +164,89 @@ describe Switches do
164
164
  end
165
165
  end
166
166
 
167
+ describe "multi-word switches" do
168
+ def build(switches_string = '')
169
+ ARGV.clear
170
+ switches_string.split.each{|a| ARGV << a}
171
+ Switches.new do |s|
172
+ s.boolean :update_template
173
+ s.set :access_token
174
+ end
175
+ end
176
+
177
+ it "sets a boolean switch supplied in its underscored form" do
178
+ expect(build('--update_template').update_template?).to eq(true)
179
+ end
180
+
181
+ it "sets a boolean switch supplied in its hyphenated form" do
182
+ expect(build('--update-template').update_template?).to eq(true)
183
+ end
184
+
185
+ it "sets a switch to the argument supplied in its underscored form" do
186
+ expect(build('--access_token abc123').access_token).to eq('abc123')
187
+ end
188
+
189
+ it "sets a switch to the argument supplied in its hyphenated form" do
190
+ expect(build('--access-token abc123').access_token).to eq('abc123')
191
+ end
192
+
193
+ it "names the setting for the symbol declared, whichever form is supplied" do
194
+ expect(build('--update-template --access-token abc123').to_h).to eq({update_template?: true, access_token: 'abc123'})
195
+ end
196
+ end
197
+
198
+ describe "multi-word switches with a required argument" do
199
+ def build(switches_string = '')
200
+ ARGV.clear
201
+ switches_string.split.each{|a| ARGV << a}
202
+ Switches.new do |s|
203
+ s.set! :access_token
204
+ end
205
+ end
206
+
207
+ it "sets the switch to the argument supplied in its underscored form" do
208
+ expect(build('--access_token abc123').access_token).to eq('abc123')
209
+ end
210
+
211
+ it "sets the switch to the argument supplied in its hyphenated form" do
212
+ expect(build('--access-token abc123').access_token).to eq('abc123')
213
+ end
214
+
215
+ it "raises OptionParser::MissingArgument when no argument is supplied to the underscored form" do
216
+ expect{build('--access_token')}.to raise_error(OptionParser::MissingArgument)
217
+ end
218
+
219
+ it "raises OptionParser::MissingArgument when no argument is supplied to the hyphenated form" do
220
+ expect{build('--access-token')}.to raise_error(OptionParser::MissingArgument)
221
+ end
222
+ end
223
+
224
+ describe "multi-word required switches" do
225
+ def build(switches_string = '')
226
+ ARGV.clear
227
+ switches_string.split.each{|a| ARGV << a}
228
+ Switches.new do |s|
229
+ s.required :access_token
230
+ end
231
+ end
232
+
233
+ it "counts the underscored form as supplied" do
234
+ expect(build('--access_token abc123').access_token).to eq('abc123')
235
+ end
236
+
237
+ it "counts the hyphenated form as supplied" do
238
+ expect(build('--access-token abc123').access_token).to eq('abc123')
239
+ end
240
+
241
+ it "raises RequiredSwitchMissing when neither form is supplied" do
242
+ expect{build}.to raise_error(RequiredSwitchMissing)
243
+ end
244
+
245
+ it "names the hyphenated form in the RequiredSwitchMissing message" do
246
+ expect{build}.to raise_error(RequiredSwitchMissing, /--access-token/)
247
+ end
248
+ end
249
+
167
250
  describe "#perform" do
168
251
  def build(switches_string = '', &block)
169
252
  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.4
4
+ version: 0.10.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran