lab42_rgxargs 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lab42/rgxargs.rb +72 -78
- data/lib/lab42/rgxargs/argument_matcher.rb +35 -0
- data/lib/lab42/rgxargs/error.rb +2 -0
- data/lib/lab42/rgxargs/predefined_matchers.rb +2 -2
- data/lib/lab42/rgxargs/syntax_definer.rb +23 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7ea5cf52b4dcf047e658abb12af417cbc24a055941f79645eb51f22e0697707
|
4
|
+
data.tar.gz: f5970679785663ca6ea8e831cc26f060824bc6ea8f79c8f00eebd6502b4b5201
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4d7c897da552ee32fa69d40fde98a398fbd274337fb64aa5739ebd01533807cc4e5d185512a5af75a561d805e82eacdad603dd9cba3275bb81866ad68bc71bc
|
7
|
+
data.tar.gz: 45854555bb805ea65e797cc3b835f440efa2505107a73e5a6b58d4a1c884b2b8776378061a4ea5e6bc50c1a493a9b230dc9b983e39e480819ca702826b86bb6b
|
data/lib/lab42/rgxargs.rb
CHANGED
@@ -5,6 +5,8 @@ module Lab42
|
|
5
5
|
|
6
6
|
class Rgxargs
|
7
7
|
require_relative 'rgxargs/predefined_matchers'
|
8
|
+
require_relative 'rgxargs/argument_matcher'
|
9
|
+
require_relative 'rgxargs/syntax_definer'
|
8
10
|
Predefined = PredefinedMatchers
|
9
11
|
|
10
12
|
extend Forwardable
|
@@ -23,102 +25,94 @@ module Lab42
|
|
23
25
|
end
|
24
26
|
|
25
27
|
def add_syntax(rgx, parser=nil, as: nil)
|
26
|
-
|
27
|
-
|
28
|
-
else
|
29
|
-
if predef = Predefined.fetch(rgx, as: as)
|
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
|
35
|
-
end
|
28
|
+
syntaxes << ArgumentMatcher.new(rgx, parser, arg_name: as)
|
29
|
+
end
|
36
30
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
31
|
+
def define_arg name, &blk
|
32
|
+
SyntaxDefiner.new(self, name).run(blk)
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse argv
|
36
|
+
until argv.empty?
|
37
|
+
argv = _parse_next argv
|
42
38
|
end
|
39
|
+
[options, args, errors]
|
40
|
+
end
|
43
41
|
|
44
42
|
|
45
|
-
|
43
|
+
private
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
45
|
+
def initialize &blk
|
46
|
+
@args = []
|
47
|
+
@conversions = {}
|
48
|
+
@defined_rules = []
|
49
|
+
@errors = []
|
50
|
+
@options = OpenStruct.new
|
51
|
+
@syntaxes = []
|
54
52
|
|
55
|
-
|
56
|
-
|
53
|
+
instance_exec(&blk) if blk
|
54
|
+
end
|
57
55
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
else
|
69
|
-
errors << [:syntax_error, name, "#{value} does not match #{conv.first}"]
|
70
|
-
nil
|
71
|
-
end
|
56
|
+
def _convert(value, name:)
|
57
|
+
conv = conversions.fetch(name, nil)
|
58
|
+
case conv
|
59
|
+
when Symbol
|
60
|
+
value.send conv
|
61
|
+
when Proc
|
62
|
+
conv.(value)
|
63
|
+
when Array
|
64
|
+
if (match = conv.first.match(value))
|
65
|
+
conv[1].(match.captures)
|
72
66
|
else
|
73
|
-
value
|
67
|
+
errors << [:syntax_error, name, "#{value} does not match #{conv.first}"]
|
68
|
+
nil
|
74
69
|
end
|
70
|
+
else
|
71
|
+
value
|
75
72
|
end
|
73
|
+
end
|
76
74
|
|
77
|
-
def _defined_rules(_arg)
|
78
|
-
false
|
79
|
-
end
|
80
75
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
_parse_symbolic first, rest
|
76
|
+
def _parse_next argv
|
77
|
+
first, *rest = argv
|
78
|
+
if first == '--'
|
79
|
+
@args += rest
|
80
|
+
return []
|
87
81
|
end
|
82
|
+
_parse_symbolic first, rest
|
83
|
+
end
|
88
84
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
85
|
+
def _parse_symbolic first, rest
|
86
|
+
case first
|
87
|
+
when %r{\A:(.*)}
|
88
|
+
options[$1.gsub('-','_').to_sym]=true
|
89
|
+
rest
|
90
|
+
when %r{(.*):\z}
|
91
|
+
_parse_value $1.gsub('-', '_').to_sym, rest
|
92
|
+
else
|
93
|
+
_parse_syntax(first)
|
94
|
+
rest
|
100
95
|
end
|
96
|
+
end
|
101
97
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
else
|
109
|
-
args << value
|
110
|
-
end
|
98
|
+
def _parse_syntax first
|
99
|
+
value, as = syntaxes.find_value(first){ |matcher| matcher.match(first) }
|
100
|
+
if as
|
101
|
+
options[as] = value
|
102
|
+
else
|
103
|
+
args << value
|
111
104
|
end
|
105
|
+
end
|
112
106
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
end
|
119
|
-
errors << [:missing_required_value, name]
|
120
|
-
[]
|
107
|
+
def _parse_value name, rest
|
108
|
+
value, *rest1 = rest
|
109
|
+
if value
|
110
|
+
options[name] = _convert(value, name: name)
|
111
|
+
return rest1
|
121
112
|
end
|
122
|
-
|
113
|
+
errors << [:missing_required_value, name]
|
114
|
+
[]
|
123
115
|
end
|
116
|
+
|
124
117
|
end
|
118
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Lab42::Rgxargs::ArgumentMatcher
|
2
|
+
require_relative './error'
|
3
|
+
require_relative './predefined_matchers'
|
4
|
+
|
5
|
+
Predefined = Lab42::Rgxargs::PredefinedMatchers
|
6
|
+
Error = Lab42::Rgxargs::Error
|
7
|
+
|
8
|
+
attr_reader :arg_name, :converter, :matcher
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
def match value
|
13
|
+
case matcher
|
14
|
+
when Regexp
|
15
|
+
match = matcher.match(value)
|
16
|
+
match && [converter.(match.captures), arg_name]
|
17
|
+
else
|
18
|
+
matcher.to_s == value && [converter.(), arg_name]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def initialize(matcher, converter, arg_name: nil)
|
25
|
+
@arg_name = arg_name
|
26
|
+
@matcher = matcher
|
27
|
+
@converter = converter || _get_predefined
|
28
|
+
end
|
29
|
+
|
30
|
+
def _get_predefined
|
31
|
+
@matcher, converter = Predefined.fetch(matcher) { raise Error, "undefined syntax #{matcher}" }
|
32
|
+
converter
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -10,8 +10,8 @@ module Lab42::Rgxargs::PredefinedMatchers extend self
|
|
10
10
|
@__defined_names__ ||= PREDEFINED.keys.join("\n\t")
|
11
11
|
end
|
12
12
|
|
13
|
-
def fetch(key, default=nil,
|
14
|
-
return PREDEFINED[key]
|
13
|
+
def fetch(key, default=nil, &blk)
|
14
|
+
return PREDEFINED[key] if PREDEFINED.has_key?(key)
|
15
15
|
blk ? blk.(key) : default
|
16
16
|
end
|
17
17
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Lab42::Rgxargs::SyntaxDefiner
|
2
|
+
|
3
|
+
attr_reader :arg_name, :parser
|
4
|
+
|
5
|
+
def run code
|
6
|
+
instance_exec(&code)
|
7
|
+
end
|
8
|
+
|
9
|
+
def syntax(matcher, value=nil, &blk)
|
10
|
+
if value
|
11
|
+
parser.add_syntax(matcher, ->(){value}, as: arg_name )
|
12
|
+
else
|
13
|
+
parser.add_syntax(matcher, blk, as: arg_name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def initialize(parser, arg_name)
|
20
|
+
@arg_name = arg_name
|
21
|
+
@parser = parser
|
22
|
+
end
|
23
|
+
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.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
@@ -46,7 +46,10 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- lib/lab42/enumerable.rb
|
48
48
|
- lib/lab42/rgxargs.rb
|
49
|
+
- lib/lab42/rgxargs/argument_matcher.rb
|
50
|
+
- lib/lab42/rgxargs/error.rb
|
49
51
|
- lib/lab42/rgxargs/predefined_matchers.rb
|
52
|
+
- lib/lab42/rgxargs/syntax_definer.rb
|
50
53
|
homepage: https://github.com/robertdober/lab42_rgxargs
|
51
54
|
licenses:
|
52
55
|
- Apache-2.0
|