xoptparse 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +2 -2
- data/lib/xoptparse.rb +47 -43
- data/lib/xoptparse/version.rb +1 -1
- data/xoptparse.gemspec +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ba4ac7ea8acae37ac7b9a7d824d6bb1e0c71e38eefcad9d51ae2b577bd2637b
|
4
|
+
data.tar.gz: b46b96861f8273625d5934b06e3db2b01831015ee611a1c9a9ab6e46e8febf5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 694618d261b60dc6e25f319d208e6f994d86362ee561dfaf754b8174a37611fe2653c41805dc5b257d505c82e52d9801b414c8ecef6bbc28465ea7dbe15ae0c2
|
7
|
+
data.tar.gz: 1a91cac3019d7509b23d564e92428014cbe44dc40f7fec7db556407ec97aabc73bcfc858715cb8e3c7d3c6a67772070bd7488d60cfb810f785330cfd387f297b
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
xoptparse (0.
|
4
|
+
xoptparse (0.5.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -52,7 +52,7 @@ DEPENDENCIES
|
|
52
52
|
minitest-power_assert
|
53
53
|
minitest-reporters
|
54
54
|
rake (~> 12.3.3)
|
55
|
-
rubocop
|
55
|
+
rubocop
|
56
56
|
simplecov
|
57
57
|
xoptparse!
|
58
58
|
|
data/lib/xoptparse.rb
CHANGED
@@ -78,42 +78,48 @@ class XOptionParser < ::OptionParser
|
|
78
78
|
end
|
79
79
|
|
80
80
|
def parse_arguments(argv, setter = nil, opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
81
|
-
arg_sws = select { |sw| sw.is_a?(Switch::
|
81
|
+
arg_sws = select { |sw| sw.is_a?(Switch::SummarizeArgument) && !opts.include?(sw.switch_name) }
|
82
82
|
return argv if arg_sws.empty?
|
83
83
|
|
84
84
|
sws_ranges = arg_sws.map(&:ranges).flatten(1)
|
85
85
|
req_count = sws_ranges.sum(&:begin)
|
86
|
-
raise MissingArgument, argv.join(' ') if argv.size < req_count
|
87
|
-
|
88
86
|
opt_count = sws_ranges.sum(&:size) - sws_ranges.size
|
89
87
|
opt_index = argv[req_count...].index { |arg| @commands.include?(arg) } unless @commands.empty?
|
90
88
|
opt_count = [opt_count, opt_index || Float::INFINITY, argv.size - req_count].min
|
91
89
|
|
92
|
-
arg_sws.
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
if r.end.nil?
|
97
|
-
rest_size = r.begin + opt_count
|
98
|
-
req_count -= r.begin
|
99
|
-
opt_count = 0
|
100
|
-
callable = true if rest_size.positive?
|
101
|
-
argv.slice!(0...rest_size).map(&conv)
|
102
|
-
elsif r.begin.zero?
|
103
|
-
next nil if opt_count.zero?
|
104
|
-
|
105
|
-
opt_count -= 1
|
106
|
-
callable = true
|
107
|
-
conv.call(argv.shift)
|
108
|
-
else
|
109
|
-
req_count -= 1
|
90
|
+
arg_sws.each_with_index do |sw, i|
|
91
|
+
if sw.is_a?(Switch::SimpleArgument)
|
92
|
+
callable = false
|
93
|
+
conv = proc do |v|
|
110
94
|
callable = true
|
111
|
-
|
95
|
+
sw.send(:conv_arg, *sw.send(:parse_arg, v))[2]
|
112
96
|
end
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
97
|
+
a = sw.ranges.map do |r|
|
98
|
+
raise MissingArgument if r.begin.positive? && argv.empty?
|
99
|
+
|
100
|
+
if r.end.nil?
|
101
|
+
rest_size = r.begin + opt_count
|
102
|
+
req_count -= r.begin
|
103
|
+
opt_count = 0
|
104
|
+
argv.slice!(0...rest_size).map(&conv)
|
105
|
+
elsif r.begin.positive?
|
106
|
+
req_count -= 1
|
107
|
+
conv.call(argv.shift)
|
108
|
+
elsif opt_count.positive?
|
109
|
+
opt_count -= 1
|
110
|
+
conv.call(argv.shift)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
if callable
|
114
|
+
val = sw.block.call(*a)
|
115
|
+
setter&.call(sw.switch_name, val)
|
116
|
+
end
|
117
|
+
elsif sw.arg == argv.first
|
118
|
+
argv.shift
|
119
|
+
@command_switch = sw
|
120
|
+
break
|
121
|
+
elsif !argv.empty? && i == arg_sws.size - 1
|
122
|
+
raise MissingArgument
|
117
123
|
end
|
118
124
|
end
|
119
125
|
|
@@ -138,22 +144,15 @@ class XOptionParser < ::OptionParser
|
|
138
144
|
end
|
139
145
|
private :parse_in_order
|
140
146
|
|
141
|
-
def order!(*args, into: nil, **kwargs)
|
147
|
+
def order!(*args, into: nil, **kwargs)
|
142
148
|
return super(*args, into: into, **kwargs) if @commands.empty?
|
143
149
|
|
150
|
+
@command_switch = nil
|
144
151
|
argv = super(*args, into: into, **kwargs, &nil)
|
145
|
-
return argv
|
146
|
-
|
147
|
-
name = argv.shift
|
148
|
-
sw = @commands[name]
|
149
|
-
if sw
|
150
|
-
into = into[name.to_sym] = {} if into
|
151
|
-
return sw.block.call.send(block_given? ? :permute! : :order!, *args, into: into, **kwargs)
|
152
|
-
end
|
152
|
+
return argv unless @command_switch
|
153
153
|
|
154
|
-
|
155
|
-
|
156
|
-
exit
|
154
|
+
into = into[@command_switch.arg.to_sym] = {} if into
|
155
|
+
@command_switch.block.call.send(block_given? ? :permute! : :order!, *args, into: into, **kwargs)
|
157
156
|
end
|
158
157
|
|
159
158
|
def command(name, desc = nil, *args, &block)
|
@@ -172,10 +171,12 @@ class XOptionParser < ::OptionParser
|
|
172
171
|
class SummarizeArgument < self
|
173
172
|
undef_method :add_banner
|
174
173
|
|
174
|
+
attr_reader :ranges
|
175
175
|
attr_reader :arg_parameters
|
176
176
|
|
177
177
|
def initialize(*)
|
178
178
|
super
|
179
|
+
@ranges = []
|
179
180
|
@arg_parameters = arg.scan(/\[\s*(.*?)\s*\]|(\S+)/).map do |opt, req|
|
180
181
|
name = opt || req
|
181
182
|
[name.sub(/\s*\.\.\.$/, ''), opt ? :opt : :req, name.end_with?('...') ? :rest : nil]
|
@@ -202,11 +203,13 @@ class XOptionParser < ::OptionParser
|
|
202
203
|
def switch_name
|
203
204
|
arg_parameters.first.first
|
204
205
|
end
|
206
|
+
|
207
|
+
def parse(_arg, _argv)
|
208
|
+
raise XOptionParser::InvalidOption
|
209
|
+
end
|
205
210
|
end
|
206
211
|
|
207
212
|
class SimpleArgument < SummarizeArgument
|
208
|
-
attr_reader :ranges
|
209
|
-
|
210
213
|
def initialize(*)
|
211
214
|
super
|
212
215
|
@ranges = arg_parameters.map do |_name, type, rest|
|
@@ -218,10 +221,11 @@ class XOptionParser < ::OptionParser
|
|
218
221
|
to << " #{arg}"
|
219
222
|
end
|
220
223
|
|
221
|
-
def parse(arg, argv)
|
224
|
+
def parse(arg, argv) # rubocop:disable Metrics/CyclomaticComplexity
|
222
225
|
case ranges.size
|
223
226
|
when 0
|
224
|
-
|
227
|
+
yield(NeedlessArgument, arg) if arg
|
228
|
+
conv_arg(arg)
|
225
229
|
when 1
|
226
230
|
unless arg
|
227
231
|
raise XOptionParser::MissingArgument if argv.empty?
|
@@ -231,7 +235,7 @@ class XOptionParser < ::OptionParser
|
|
231
235
|
arg = [arg] if ranges.first.end.nil?
|
232
236
|
conv_arg(*parse_arg(arg, &method(:raise)))
|
233
237
|
else
|
234
|
-
|
238
|
+
super(arg, argv)
|
235
239
|
end
|
236
240
|
end
|
237
241
|
end
|
data/lib/xoptparse/version.rb
CHANGED
data/xoptparse.gemspec
CHANGED
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_development_dependency 'minitest-power_assert'
|
30
30
|
spec.add_development_dependency 'minitest-reporters'
|
31
31
|
spec.add_development_dependency 'rake', '~> 12.3.3'
|
32
|
-
spec.add_development_dependency 'rubocop'
|
32
|
+
spec.add_development_dependency 'rubocop'
|
33
33
|
spec.add_development_dependency 'simplecov'
|
34
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xoptparse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ofk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,16 +84,16 @@ dependencies:
|
|
84
84
|
name: rubocop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0
|
89
|
+
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0
|
96
|
+
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: simplecov
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|