lab42_rgxargs 0.2.1 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9f2ee6bdaddb5db512e1cf55c65f42699b2441ac0878760f44c8083ff12f023
4
- data.tar.gz: 8e4175e4906cc5b5a3511f413b96f27f63cf7eaeaec7c07fe5e851ee2faa6e08
3
+ metadata.gz: fa12272618cb7bd23bf218ce2405f99119f1686352b7ddfc622e5697caa22dbf
4
+ data.tar.gz: 5f6c5ebf2b1ff5bdfa3d701b493359aff5fdea521f221b78d0f5f3e3838060a9
5
5
  SHA512:
6
- metadata.gz: 33a12fa2ce1f98386d28bdd1c99a18fc982545fb26030c5a6dd618f5ce75e2276dfc06f91a4c9bbe6bb6867550be9a9a2aa95ca18d67244f4c3861409d4d58fe
7
- data.tar.gz: 022b992b251e8027bb19e1a91810abc066fac549b4d74ff77d43ecfa3c8ceae7bd41311b3161d7f7546ba187673307385c522f272a677aa0e81c54250c4ca0af
6
+ metadata.gz: bb9e676b416e06afdb02c10740026afd313f41aaf17ce60824054309327915e00dd4883901d311bb96ff15d376793fdf27a9cc6be7a1c2ee6d8074a75ff302bc
7
+ data.tar.gz: 1a40c994e1b1943c070f6b2969c1b4b59ab832e17e79908cd7218210bbf12e916c1521ea5758aa0e7330515606dd4745a18941771f8bd9e3d57d775b941791a2
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ostruct'
4
+ class OpenStruct
5
+ def deconstruct_keys(_keys)
6
+ to_h
7
+ end
8
+ end
9
+ # SPDX-License-Identifier: Apache-2.0
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  class Rgxargs
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.4"
4
4
  end
5
5
  end
data/lib/lab42/rgxargs.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require 'forwardable'
2
- require 'ostruct'
3
2
  require 'lab42/enumerable'
4
3
  require 'set'
4
+
5
+ require_relative 'monkey/open_struct'
5
6
  module Lab42
6
7
 
7
8
  class Rgxargs
@@ -18,7 +19,7 @@ module Lab42
18
19
  extend Forwardable
19
20
  def_delegators Predefined, :list_matcher
20
21
 
21
- attr_reader :allowed, :args, :conversions, :defaults, :defined_rules, :errors, :options, :required, :syntaxes
22
+ attr_reader :allowed, :args, :conversions, :defaults, :defined_rules, :errors, :open_struct, :options, :posix, :required, :syntaxes
22
23
 
23
24
  def predefined_matchers
24
25
  Predefined.defined_names
@@ -52,11 +53,11 @@ module Lab42
52
53
  end
53
54
 
54
55
  def parse argv
55
- until argv.empty?
56
- argv = _parse_next argv
56
+ if posix
57
+ _parse_posix argv
58
+ else
59
+ _parse_ruby_syntax argv
57
60
  end
58
- _check_required_kwds
59
- [options, args, errors]
60
61
  end
61
62
 
62
63
  def allows name, matcher=nil, &converter
@@ -69,18 +70,20 @@ module Lab42
69
70
 
70
71
  private
71
72
 
72
- def initialize &blk
73
+ def initialize(open_struct: true, posix: false, &blk)
73
74
  @args = []
74
75
  @allowed = nil
75
76
  @conversions = {}
76
77
  @defaults = {}
77
78
  @defined_rules = []
78
79
  @errors = []
80
+ @posix = posix
81
+ @open_struct = open_struct
79
82
  @required = ::Set.new
80
83
  @syntaxes = []
81
84
 
82
85
  instance_exec(&blk) if blk
83
- @options = ::OpenStruct.new(defaults)
86
+ @options = defaults
84
87
  end
85
88
 
86
89
  def _convert(value, name:)
@@ -122,7 +125,16 @@ module Lab42
122
125
  end
123
126
  end
124
127
 
125
- def _parse_next argv
128
+ def _parse_next_posix argv
129
+ first, *rest = argv
130
+ if first == '--'
131
+ @args += rest
132
+ return []
133
+ end
134
+ _parse_posix_param first, rest
135
+ end
136
+
137
+ def _parse_next_ruby_syntax argv
126
138
  first, *rest = argv
127
139
  if first == '--'
128
140
  @args += rest
@@ -131,12 +143,49 @@ module Lab42
131
143
  _parse_symbolic first, rest
132
144
  end
133
145
 
146
+ def _parse_posix argv
147
+ until argv.empty?
148
+ argv = _parse_next_posix argv
149
+ end
150
+ _check_required_kwds
151
+ _return_values
152
+ end
153
+
154
+ def _parse_posix_param first, rest
155
+ case first
156
+ when %r{\A--([-_[:alnum:]]+)=(.*)\z}
157
+ Regexp.last_match.to_a => _, name, value
158
+ name = name.gsub("-", "_")
159
+ options[name.to_sym] = _convert(value, name: name)
160
+ when %r{\A--([-_[:alnum:]]+)\z}
161
+ Regexp.last_match.to_a => _, name
162
+ name = name.gsub("-", "_")
163
+ options[name.to_sym] = true
164
+ when %r{\A-([[:alnum:]]+)\z}
165
+ $1.grapheme_clusters.each do |shopt|
166
+ options[shopt.to_sym]=true
167
+ end
168
+ else
169
+ args << first
170
+ # _parse_syntax(first)
171
+ end
172
+ rest
173
+ end
174
+
175
+ def _parse_ruby_syntax argv
176
+ until argv.empty?
177
+ argv = _parse_next_ruby_syntax argv
178
+ end
179
+ _check_required_kwds
180
+ _return_values
181
+ end
182
+
134
183
  def _parse_symbolic first, rest
135
184
  case first
136
185
  when %r{\A:(.*)}
137
186
  switch = $1.gsub('-','_').to_sym
138
187
  _check_switch(switch)
139
- options[switch]=true
188
+ options[switch.to_sym]=true
140
189
  rest
141
190
  when %r{(.*):\z}
142
191
  kwd = $1.gsub('-', '_').to_sym
@@ -154,7 +203,7 @@ module Lab42
154
203
  def _parse_syntax first
155
204
  value, as = syntaxes.find_value(first){ |matcher| matcher.match(first) }
156
205
  if as
157
- options[as] = value
206
+ options[as.to_sym] = value
158
207
  else
159
208
  args << value
160
209
  end
@@ -163,12 +212,19 @@ module Lab42
163
212
  def _parse_value name, rest
164
213
  value, *rest1 = rest
165
214
  if value
166
- options[name] = _convert(value, name: name)
215
+ options[name.to_sym] = _convert(value, name: name)
167
216
  return rest1
168
217
  end
169
218
  errors << [:missing_required_value, name]
170
219
  []
171
220
  end
172
221
 
222
+ def _return_values
223
+ if open_struct
224
+ [::OpenStruct.new(options), args, errors]
225
+ else
226
+ [options, args, errors]
227
+ end
228
+ end
173
229
  end
174
230
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_rgxargs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
@@ -73,6 +73,7 @@ extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
75
  - lib/lab42/enumerable.rb
76
+ - lib/lab42/monkey/open_struct.rb
76
77
  - lib/lab42/rgxargs.rb
77
78
  - lib/lab42/rgxargs/argument_matcher.rb
78
79
  - lib/lab42/rgxargs/checker.rb
@@ -101,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
102
  - !ruby/object:Gem::Version
102
103
  version: '0'
103
104
  requirements: []
104
- rubygems_version: 3.3.3
105
+ rubygems_version: 3.3.7
105
106
  signing_key:
106
107
  specification_version: 4
107
108
  summary: Parse CL args according to regexen