optparse2 0.7.6 → 0.8.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: '089aa30260171fcc53efa5d5099338973990015878df07b4a3c9ae023ae7d0c6'
4
- data.tar.gz: 2e46af429af2a2c3576bb5bfd2f32bc4bcb448fb2ecb73c34f419de705d502d6
3
+ metadata.gz: 7af25aed35f6e83c5d21f2243c8cc714f182f32a5f012ad93dd04486e8ba5865
4
+ data.tar.gz: c7804d256d8ead6a97f9edbcd6049ccb9d9226a297341a8fe6888b39133023c3
5
5
  SHA512:
6
- metadata.gz: c2f53a2bc1382025cdc0c66ddbfe2878bbcec9a21ddb66b5c54e6952fdef9a32d4b9d9c11ea68875ec2b1e875f135cd33c48ec54d138cc450c7d92428e9a8ed0
7
- data.tar.gz: 66b6650a5aa9b38020ccb8a053268037188897b7b6d2514b14b384fb488b1d7553f005bd9ed266b4ae293ad506cc45ad6f8fe6384125bbc9928e1b9b79b1d42b
6
+ metadata.gz: 00fe7339a1b17afec474b3e1719ea7b774c2ffb5ae55c73310d81c9907a804958909160d7c04df4efbd626ee6a8eb3f85d969b28508f57ed74c0a8090ea2384b
7
+ data.tar.gz: c01111aa1350878a7a060a3bec5a283465349a0c4bbe498a48d22b59d1c3f15736e9d180b9eed127c02a5c6a67e0e2ff973e3aad31e54e3109203ab7a5a9d155
@@ -11,9 +11,4 @@ class OptParse2
11
11
  def define_head(*opts, **, &block) base.append(*(sw = make_switch(opts, block, **))); sw[0] end
12
12
  alias def_tail_option define_tail
13
13
  def on_tail(...) define_tail(...); self end
14
-
15
- def initialize(*)
16
- @defaults = {}
17
- super
18
- end
19
14
  end
@@ -150,7 +150,7 @@ class OptParse2
150
150
  # Default values of switches are used when the switch is never passed in.
151
151
  # If the `value` that's provided doesn't respond to `.call`, it's converted to a proc.
152
152
  # If `bypass` is truthy, then the default value is never passed to the block's proc (if any)
153
- def set_default(value, description, bypass, no_default_description)
153
+ def set_default(value, description, bypass)
154
154
  if required?
155
155
  raise ArgumentError, 'cannot supply a default value for a required argument'
156
156
  end
@@ -165,7 +165,6 @@ class OptParse2
165
165
  @default_proc = proc { |_switch_name| value }
166
166
  end
167
167
 
168
- @no_default_description = no_default_description
169
168
  @default_description = description
170
169
  @default_bypass = bypass
171
170
  end
@@ -173,16 +172,30 @@ class OptParse2
173
172
  # Calls the default proc to figure out what the default value is for this switch
174
173
  def default_bypass? = @default_bypass
175
174
  def default? = defined?(@default_proc)
176
- def default = @default_proc&.call(switch_name)
175
+ def default
176
+ return unless default?
177
+ if defined? @__default_value_after_calling_proc__
178
+ @__default_value_after_calling_proc__
179
+ else
180
+ @__default_value_after_calling_proc__ = @default_proc&.call(switch_name)
181
+ end
182
+ end
177
183
 
178
- def default_description = @default_description || default.to_s
179
184
  def desc
180
- return super unless defined? @default_proc
181
- return super if @no_default_description
182
- x = super
183
- x << '' if x.empty?
184
- x[-1] += " [default: #{default_description}]"
185
- x
185
+ super_desc = super
186
+ return super_desc unless defined? @default_proc
187
+
188
+ if !@default_description
189
+ return super_desc
190
+ end
191
+
192
+ super_desc << '' if super_desc.empty?
193
+ super_desc[-1] += " [default"
194
+ if @default_description != true
195
+ super_desc.last.concat ": #{@default_description == :__TODO_DEFAULT__ ? default : @default_description}"
196
+ end
197
+ super_desc.last.concat ']'
198
+ super_desc
186
199
  end
187
200
  end
188
201
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class OptParse2
4
- VERSION = "0.7.6"
4
+ VERSION = "0.8.0"
5
5
  end
data/lib/optparse2.rb CHANGED
@@ -35,7 +35,7 @@ class OptParse2
35
35
  key: @group,
36
36
  default: nodefault=true,
37
37
  default_bypass: false,
38
- default_description: no_default_description=true,
38
+ default_description: :__TODO_DEFAULT__,
39
39
  required: false,
40
40
  multiple: nil
41
41
  )
@@ -56,12 +56,12 @@ class OptParse2
56
56
 
57
57
  sw.set_required true if required
58
58
 
59
- if nodefault && !no_default_description
59
+ if nodefault && default_description != :__TODO_DEFAULT__
60
60
  raise ArgumentError, "default: not supplied, but default_description: given"
61
61
  elsif nodefault && default_bypass
62
62
  raise ArgumentError, "default: not supplied, but default_bypass: given"
63
63
  elsif not nodefault
64
- sw.set_default(default, default_description, default_bypass, !no_default_description)
64
+ sw.set_default(default, default_description, default_bypass)
65
65
  @defaults << sw
66
66
  end
67
67
 
@@ -270,6 +270,17 @@ class OptParse2
270
270
  end
271
271
  end
272
272
 
273
+ __END__
274
+
275
+ OptParse2.new do |op|
276
+ op.switch('-f', '--foo=BAR', 'does it')
277
+ .default { p $x += 1 }
278
+ .build!
279
+ op.parse! into: opts={}
280
+ p opts
281
+ p $x
282
+ end
283
+
273
284
  __END__
274
285
  OptParse2.new do |op|
275
286
  op.on '-f', '--foo=BAR', 'does it', default: 10, default_description: nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optparse2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.6
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SamW