lab42_rgxargs 0.1.0 → 0.1.1
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/lab42/rgxargs.rb +93 -91
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad2ab026f6192cc81dd04008d035a79188ed42723b4d687a536a49c010d8a0db
|
4
|
+
data.tar.gz: b4503cbb9403aec807fb871c5d901690d6ba026edb8c6b6080e0af4506b6e2a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e41fc413182c5a6e6ca070c8980b9356738df2b956915c1fdf55849136642ff36b97cef4ee01e1fc3505269a7f9c7b9328044bd8c7a1d4b3401e08aa8820a5b8
|
7
|
+
data.tar.gz: e75cbfc5b427e68838513444862d0e3ccca90fe5363f517fe24f40beae2655c830c020f2c55559375069bfe837c4ab55940559f3625e318656b12c9c4ff3d5cc
|
data/lib/lab42/rgxargs.rb
CHANGED
@@ -1,117 +1,119 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
require 'ostruct'
|
3
3
|
require 'lab42/enumerable'
|
4
|
+
module Lab42
|
4
5
|
|
5
|
-
class
|
6
|
-
|
7
|
-
|
6
|
+
class Rgxargs
|
7
|
+
require_relative 'rgxargs/predefined_matchers'
|
8
|
+
Predefined = PredefinedMatchers
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
extend Forwardable
|
11
|
+
def_delegators Predefined, :list_matcher
|
11
12
|
|
12
|
-
|
13
|
+
attr_reader :args, :conversions, :defined_rules, :errors, :options, :syntaxes
|
13
14
|
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
def add_conversion(param, conversion)
|
17
|
+
case conversion
|
18
|
+
when Symbol
|
19
|
+
conversions[param] = Predefined.fetch(conversion, conversion)
|
20
|
+
else
|
21
|
+
conversions[param] = conversion
|
22
|
+
end
|
21
23
|
end
|
22
|
-
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
else
|
28
|
-
if predef = Predefined.fetch(rgx)
|
29
|
-
syntaxes << predef
|
25
|
+
def add_syntax(rgx, parser=nil)
|
26
|
+
if parser
|
27
|
+
syntaxes << [rgx, parser]
|
30
28
|
else
|
31
|
-
|
29
|
+
if predef = Predefined.fetch(rgx)
|
30
|
+
syntaxes << predef
|
31
|
+
else
|
32
|
+
raise ArgumentError, "#{rgx} is not a predefined syntax, use one of the following:\n\t#{Predefined.defined_names}"
|
33
|
+
end
|
34
|
+
end
|
32
35
|
end
|
33
|
-
end
|
34
|
-
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
def parse argv
|
38
|
+
until argv.empty?
|
39
|
+
argv = _parse_next argv
|
40
|
+
end
|
41
|
+
[options, args, errors]
|
42
|
+
end
|
42
43
|
|
43
44
|
|
44
|
-
|
45
|
+
private
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
def initialize &blk
|
48
|
+
@args = []
|
49
|
+
@conversions = {}
|
50
|
+
@defined_rules = []
|
51
|
+
@errors = []
|
52
|
+
@options = OpenStruct.new
|
53
|
+
@syntaxes = []
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
instance_exec(&blk) if blk
|
56
|
+
end
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
58
|
+
def _convert(value, name:)
|
59
|
+
conv = conversions.fetch(name, nil)
|
60
|
+
case conv
|
61
|
+
when Symbol
|
62
|
+
value.send conv
|
63
|
+
when Proc
|
64
|
+
conv.(value)
|
65
|
+
when Array
|
66
|
+
if (match = conv.first.match(value))
|
67
|
+
conv.last.(match.captures)
|
68
|
+
else
|
69
|
+
errors << [:syntax_error, name, "#{value} does not match #{conv.first}"]
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
else
|
73
|
+
value
|
74
|
+
end
|
70
75
|
end
|
71
|
-
else
|
72
|
-
value
|
73
|
-
end
|
74
|
-
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
77
|
+
def _defined_rules(_arg)
|
78
|
+
false
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
81
|
+
def _parse_next argv
|
82
|
+
first, *rest = argv
|
83
|
+
if _defined_rules(first)
|
84
|
+
return rest
|
85
|
+
end
|
86
|
+
_parse_symbolic first, rest
|
87
|
+
end
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
89
|
+
def _parse_symbolic first, rest
|
90
|
+
case first
|
91
|
+
when %r{\A:(.*)}
|
92
|
+
options[$1.gsub('-','_').to_sym]=true
|
93
|
+
rest
|
94
|
+
when %r{(.*):\z}
|
95
|
+
_parse_value $1.gsub('-', '_').to_sym, rest
|
96
|
+
else
|
97
|
+
_parse_syntax(first)
|
98
|
+
rest
|
99
|
+
end
|
100
|
+
end
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
102
|
+
def _parse_syntax first
|
103
|
+
args << syntaxes.find_value(first) do |(rgx, converter)|
|
104
|
+
(match = rgx.match(first)) && converter.(match.captures)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def _parse_value name, rest
|
109
|
+
value, *rest1 = rest
|
110
|
+
if value
|
111
|
+
options[name] = _convert(value, name: name)
|
112
|
+
return rest1
|
113
|
+
end
|
114
|
+
errors << [:missing_required_value, name]
|
115
|
+
[]
|
116
|
+
end
|
106
117
|
|
107
|
-
def _parse_value name, rest
|
108
|
-
value, *rest1 = rest
|
109
|
-
if value
|
110
|
-
options[name] = _convert(value, name: name)
|
111
|
-
return rest1
|
112
118
|
end
|
113
|
-
errors << [:missing_required_value, name]
|
114
|
-
[]
|
115
119
|
end
|
116
|
-
|
117
|
-
end
|