switches.rb 0.10.3 → 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: f5b9c36e31d7118656c21aabaf9ba11bbce49bf412c8162bc846b498e2df4436
4
- data.tar.gz: 9e80ffa19bdba18e233a545823feda717cc929d894520c25e6c4c47727725a79
3
+ metadata.gz: 073acfd47105841bff0a6643eebf2a8f1af398224feac75af268715a7dc666fa
4
+ data.tar.gz: ed0d87dd771ff98dbf4da3f8622fc6c6622ad78a630ec3c1f1108b07416c89cf
5
5
  SHA512:
6
- metadata.gz: 17a0546157353bbbd9de4a0139097cff70d4551c528e1203749c7bafe0195c4553ead66fd229082e6bc9b02eb43d9cacaaed3927279640608dca6bdfcb178c13
7
- data.tar.gz: 07cd0a479fe6e0ec7887e01e5b96652d1ba80d3062fc76d43e3433990751b806236ce9902531c7b8dc750560fb320c96ba39445d02c6a8fcf7fe03afef9e04f2
6
+ metadata.gz: 5f4d4da5a5632926928b9bb357d1bad5bfc7e34f4e727d59887cd0ead5b7fd0b01c1de5468f563cea9137ca0bc730715026624b85ee71a7bd176ac3fe78193b6
7
+ data.tar.gz: 15d4316951b1ddb6e7bb5647e90964b1df3720d7b43d3313034a99f134d41008e91711f7c603654c64b9aba3a43c4e9daa6507333ee6f6a9ee7bbe3001498e8b
data/CHANGELOG CHANGED
@@ -1,5 +1,24 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 20260816
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
+
12
+ 0.10.4: Declare ostruct as a runtime dependency, in place of installing it at require time.
13
+
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.
15
+ 2. ~ switches.rb.gemspec: + s.dependencies = ['ostruct'], declared as data via a dependencies= setter added to the local Gem::Specification reopening, alongside the development_dependencies= setter added in 0.10.3.
16
+ 3. - lib/Kernel/require_gem.rb and - lib/Thoran/Kernel/RequireGem/require_gem.rb, vendored in 0.10.0 for the require above and with no remaining user.
17
+ 4. ~ Gemfile: - gem 'ostruct', which the gemspec now supplies.
18
+ 5. ~ spec/all.rb: the gemspec spec asserted that no runtime dependencies were declared, and now asserts that ostruct is the only one.
19
+ 6. ~ lib/Switches.rb: the header's Dependencies section names ostruct, since the Standard Ruby Library no longer covers it.
20
+ 7. ~ .gitignore: + a header, + .claude, ~ /tmp/ to tmp/, and - /test/tmp/, which tmp/ now covers.
21
+
3
22
  ## 20260812
4
23
 
5
24
  0.10.3: Move the version into a constant, and add the standard version and gemspec tests.
data/Gemfile CHANGED
@@ -1,7 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
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
@@ -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.3'
5
+ VERSION = '0.10.5'
6
6
  end
data/lib/Switches.rb CHANGED
@@ -12,13 +12,12 @@
12
12
  # 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.
13
13
  # 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?...
14
14
 
15
- # Dependencies:
16
- # 1. Standard Ruby Library.
15
+ # Dependencies:
16
+ # 1. Standard Ruby Library.
17
+ # 2. ostruct, which ceased to be a default gem as of Ruby 4.0 and so is declared in the gemspec.
17
18
 
18
19
  require 'optparse'
19
- require 'Kernel/require_gem'
20
-
21
- require_gem 'ostruct'
20
+ require 'ostruct'
22
21
 
23
22
  require_relative './Switches/VERSION'
24
23
 
@@ -285,12 +284,17 @@ class Switches
285
284
  end
286
285
  end
287
286
 
287
+ def switch_string(attr)
288
+ attr = attr.to_s
289
+ "-#{attr.long_switch? ? '-' : ''}#{attr.delete('?')}"
290
+ end
291
+
288
292
  def on_args(requires_argument, options, *attrs, &block)
289
293
  on_args = []
290
294
  attrs.collect{|e| e.to_s}.each do |attr|
291
295
  @defaults[attr] = options[:default] if options[:default]
292
296
  @castings[attr] = (options[:cast] || options[:type] || options[:class]) if (options[:cast] || options[:type] || options[:class])
293
- on_args << "-#{attr.long_switch? ? '-' : ''}#{attr.to_s.delete('?')}"
297
+ on_args << switch_string(attr)
294
298
  end
295
299
  if requires_argument
296
300
  on_args << (on_args.pop + ' REQUIRED_ARGUMENT')
@@ -311,7 +315,7 @@ class Switches
311
315
  messages = []
312
316
  @required_switches.each do |required_switch|
313
317
  unless supplied_switches.include?(required_switch)
314
- 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"
315
319
  end
316
320
  end
317
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
@@ -257,8 +340,8 @@ describe 'switches.rb.gemspec' do
257
340
  expect(spec.version.to_s).to eq(Switches::VERSION)
258
341
  end
259
342
 
260
- it "declares no runtime dependencies" do
261
- expect(spec.runtime_dependencies.map(&:name).sort).to eq([])
343
+ it "declares ostruct as its only runtime dependency" do
344
+ expect(spec.runtime_dependencies.map(&:name).sort).to eq(%w{ostruct})
262
345
  end
263
346
 
264
347
  it "declares its development dependencies" do
data/switches.rb.gemspec CHANGED
@@ -1,13 +1,17 @@
1
1
  require_relative './lib/Switches/VERSION'
2
2
 
3
3
  class Gem::Specification
4
+ def dependencies=(gems)
5
+ gems.each{|gem| add_dependency(*gem)}
6
+ end
7
+
4
8
  def development_dependencies=(gems)
5
9
  gems.each{|gem| add_development_dependency(*gem)}
6
10
  end
7
11
  end
8
12
 
9
13
  Gem::Specification.new do |s|
10
- s.name = 'switches.rb' # I would have preferred 'switches', but there's a gem with the name of switches.
14
+ s.name = 'switches.rb'
11
15
  s.version = Switches::VERSION
12
16
 
13
17
  s.summary = "The easiest way to provide switches to a Ruby program."
@@ -19,11 +23,7 @@ Gem::Specification.new do |s|
19
23
  s.license = 'MIT'
20
24
 
21
25
  s.required_ruby_version = ">= 2.7.0"
22
-
23
- s.development_dependencies = [
24
- 'rake',
25
- 'rspec'
26
- ]
26
+ s.require_paths = ['lib']
27
27
 
28
28
  s.files = [
29
29
  'CHANGELOG',
@@ -36,7 +36,14 @@ Gem::Specification.new do |s|
36
36
  Dir['spec/**/*.rb'],
37
37
  ].flatten
38
38
 
39
- s.require_paths = ['lib']
39
+ s.dependencies = [
40
+ 'ostruct'
41
+ ]
42
+
43
+ s.development_dependencies = [
44
+ 'rake',
45
+ 'rspec'
46
+ ]
40
47
 
41
48
  s.metadata = {
42
49
  "bug_tracker_uri" => "https://github.com/thoran/switches/issues",
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.3
4
+ version: 0.10.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -9,6 +9,20 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ostruct
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
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: rake
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -49,10 +63,8 @@ files:
49
63
  - LICENSE.txt
50
64
  - README.md
51
65
  - Rakefile
52
- - lib/Kernel/require_gem.rb
53
66
  - lib/Switches.rb
54
67
  - lib/Switches/VERSION.rb
55
- - lib/Thoran/Kernel/RequireGem/require_gem.rb
56
68
  - spec/all.rb
57
69
  - switches.rb.gemspec
58
70
  homepage: https://github.com/thoran/switches
@@ -1,7 +0,0 @@
1
- # Kernel/require_gem.rb
2
- # Kernel#require_gem
3
-
4
- # 20240613
5
- # 0.0.0
6
-
7
- require 'Thoran/Kernel/RequireGem/require_gem'
@@ -1,27 +0,0 @@
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)