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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d995928882bad168df974b923ccb5d725eb86bbd56a694cedd9059ef568e6ad
4
- data.tar.gz: 4e4194883792088934889ebd8f7a6fdcd6126ad55cf1fa3a0eb30249ad938d0a
3
+ metadata.gz: e7ea5cf52b4dcf047e658abb12af417cbc24a055941f79645eb51f22e0697707
4
+ data.tar.gz: f5970679785663ca6ea8e831cc26f060824bc6ea8f79c8f00eebd6502b4b5201
5
5
  SHA512:
6
- metadata.gz: fb3138610aa1d243ed7bc5d7b7b782901b788d5b75b50e227f7cc0d5ba3ca0f524f8f501139bb67ac808a4b697f915afe89312a0c2f3ec4bf4396cce81d75908
7
- data.tar.gz: f662d35d67de38063f978716ca85a9e68548c71dd428867e4354d3706b6d070a3c5efd4fd18e3e55ac375aba9a7b218f5fe5fefe04e63a33e407646fbfd54671
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
- if parser
27
- syntaxes << [rgx, parser, as]
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
- def parse argv
38
- until argv.empty?
39
- argv = _parse_next argv
40
- end
41
- [options, args, errors]
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
- private
43
+ private
46
44
 
47
- def initialize &blk
48
- @args = []
49
- @conversions = {}
50
- @defined_rules = []
51
- @errors = []
52
- @options = OpenStruct.new
53
- @syntaxes = []
45
+ def initialize &blk
46
+ @args = []
47
+ @conversions = {}
48
+ @defined_rules = []
49
+ @errors = []
50
+ @options = OpenStruct.new
51
+ @syntaxes = []
54
52
 
55
- instance_exec(&blk) if blk
56
- end
53
+ instance_exec(&blk) if blk
54
+ end
57
55
 
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[1].(match.captures)
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
- def _parse_next argv
82
- first, *rest = argv
83
- if _defined_rules(first)
84
- return rest
85
- end
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
- 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
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
- def _parse_syntax first
103
- value, as = syntaxes.find_value(first) do |(rgx, converter, as)|
104
- (match = rgx.match(first)) && [converter.(match.captures), as]
105
- end
106
- if as
107
- options[as] = value
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
- def _parse_value name, rest
114
- value, *rest1 = rest
115
- if value
116
- options[name] = _convert(value, name: name)
117
- return rest1
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
@@ -0,0 +1,2 @@
1
+ class Lab42::Rgxargs::Error < RuntimeError
2
+ 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, as: nil, &blk)
14
- return PREDEFINED[key] + [as] if PREDEFINED.has_key?(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.2
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