optparse2 0.7.6 → 0.7.7
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 +4 -4
- data/lib/optparse2/builder.rb +48 -0
- data/lib/optparse2/switch-helpers.rb +23 -10
- data/lib/optparse2/version.rb +1 -1
- data/lib/optparse2.rb +15 -3
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3dcc11ed7e12ad87b21d8ebe019134d532b939cae79c8ed28ad7a7306995bfc2
|
|
4
|
+
data.tar.gz: 492b927a81e1df4d1e0cfb7aa7f91fb8e456c31be7a04052e2f870ff8862871d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 420612dd7c3d50fff1ddff3fdfa9c87bfd05bb58f01036450a348d7ee7109cd8cf320c39382566d1d7f341b0402f09c9439dde39e20a7f732bc013c2fe60cf40
|
|
7
|
+
data.tar.gz: a5e696e36c4206f75531d11157743462edc0ca1500b7100ac7f85fcf4a87b791b5a9758794d1181380e4da048ad8ef633aabf955803e3c3e745c63c7532a9f9c
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class OptParse2
|
|
2
|
+
def switch(*pos, &block)
|
|
3
|
+
Builder.new(pos, block, self)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
class Builder
|
|
7
|
+
def initialize(positional, block, optparse2)
|
|
8
|
+
@positional, @block, @optparse2 = positional, block, optparse2
|
|
9
|
+
@args = {}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def build!
|
|
13
|
+
@optparse2.on(*@positional, **@args, &@block)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def block(&block)
|
|
17
|
+
raise LocalJumpError unless defined? yield
|
|
18
|
+
@block = block
|
|
19
|
+
self
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def hidden(hidden = true)
|
|
23
|
+
@args[:hidden] = hidden
|
|
24
|
+
self
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def default(value = novalue=true, description: nodescription=true, &block)
|
|
28
|
+
if novalue.nil? != block.nil?
|
|
29
|
+
raise ArgumentError, "Exactly one of a positional argument or a block can be given"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
@args[:default] = (novalue ? block : proc { value })
|
|
33
|
+
@args[:default_description] = description unless nodescription
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def key(key)
|
|
38
|
+
@args[:key] = key
|
|
39
|
+
self
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def required(required = true)
|
|
43
|
+
@args[:required] = required
|
|
44
|
+
self
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
@@ -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
|
|
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
|
|
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
|
-
|
|
181
|
-
return
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
data/lib/optparse2/version.rb
CHANGED
data/lib/optparse2.rb
CHANGED
|
@@ -8,6 +8,7 @@ require_relative "optparse2/version"
|
|
|
8
8
|
require_relative "optparse2/fixes"
|
|
9
9
|
require_relative "optparse2/switch-helpers"
|
|
10
10
|
require_relative "optparse2/context"
|
|
11
|
+
require_relative "optparse2/builder"
|
|
11
12
|
|
|
12
13
|
class OptParse2
|
|
13
14
|
class << self
|
|
@@ -35,7 +36,7 @@ class OptParse2
|
|
|
35
36
|
key: @group,
|
|
36
37
|
default: nodefault=true,
|
|
37
38
|
default_bypass: false,
|
|
38
|
-
default_description:
|
|
39
|
+
default_description: :__TODO_DEFAULT__,
|
|
39
40
|
required: false,
|
|
40
41
|
multiple: nil
|
|
41
42
|
)
|
|
@@ -56,12 +57,12 @@ class OptParse2
|
|
|
56
57
|
|
|
57
58
|
sw.set_required true if required
|
|
58
59
|
|
|
59
|
-
if nodefault &&
|
|
60
|
+
if nodefault && default_description != :__TODO_DEFAULT__
|
|
60
61
|
raise ArgumentError, "default: not supplied, but default_description: given"
|
|
61
62
|
elsif nodefault && default_bypass
|
|
62
63
|
raise ArgumentError, "default: not supplied, but default_bypass: given"
|
|
63
64
|
elsif not nodefault
|
|
64
|
-
sw.set_default(default, default_description, default_bypass
|
|
65
|
+
sw.set_default(default, default_description, default_bypass)
|
|
65
66
|
@defaults << sw
|
|
66
67
|
end
|
|
67
68
|
|
|
@@ -270,6 +271,17 @@ class OptParse2
|
|
|
270
271
|
end
|
|
271
272
|
end
|
|
272
273
|
|
|
274
|
+
__END__
|
|
275
|
+
|
|
276
|
+
OptParse2.new do |op|
|
|
277
|
+
op.switch('-f', '--foo=BAR', 'does it')
|
|
278
|
+
.default { p $x += 1 }
|
|
279
|
+
.build!
|
|
280
|
+
op.parse! into: opts={}
|
|
281
|
+
p opts
|
|
282
|
+
p $x
|
|
283
|
+
end
|
|
284
|
+
|
|
273
285
|
__END__
|
|
274
286
|
OptParse2.new do |op|
|
|
275
287
|
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.
|
|
4
|
+
version: 0.7.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- SamW
|
|
@@ -19,6 +19,7 @@ files:
|
|
|
19
19
|
- README.md
|
|
20
20
|
- Rakefile
|
|
21
21
|
- lib/optparse2.rb
|
|
22
|
+
- lib/optparse2/builder.rb
|
|
22
23
|
- lib/optparse2/context.rb
|
|
23
24
|
- lib/optparse2/fixes.rb
|
|
24
25
|
- lib/optparse2/pathname.rb
|