lab42_rgxargs 0.1.4 → 0.1.9

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: dfe60f4e05870ad8a1766abbe5b06b0a6925276696285bb19139ef11aeee70a3
4
- data.tar.gz: 682386ba4e34b99f53877e0826b0e10f824c3750198c1b6bf66d634de3104d46
3
+ metadata.gz: 63983b6491bb1ce75a959a0bbf95c22d2146468ed7ea45c5281065d96b0c79ba
4
+ data.tar.gz: d70cbfbcd144f2d1fe639347592a80d3e3667f62ddb411dc1d3b9eb231492ee1
5
5
  SHA512:
6
- metadata.gz: 87cba6d04bfe08e2df0b1f90ccb273e2dd37594f4b44b15814a0c6def76466e244ae164f6876a9ad83dceee3ee43ffa815a2e5aa1ad877d929d352febdafab32
7
- data.tar.gz: 75744824c57165417049bc8415315efc29de23a3af3c88a94b4cada576652a47e54ae472097a9819020c563bea93ec0e357fa7dfc3672a65b4e7700deaa402ed
6
+ metadata.gz: 45318701acaf53c2afe7ee571d2a38af1743bedd3554c514c1a06512ad16c0cb7afed0a3cf427ff44e0c67f8bf75e7b21f63703c18ac892dea562b712cbccb7d
7
+ data.tar.gz: 19d9bf8c3d67f10a6848a368996717418eec67873141edc862d4a4cc1ece1703acddc041fa25393d05eaabc8fca77142d5c3213b99ffce0012d8e2d9d60556ff
@@ -4,28 +4,42 @@ require 'lab42/enumerable'
4
4
  module Lab42
5
5
 
6
6
  class Rgxargs
7
+ require_relative 'rgxargs/checker'
8
+ require_relative 'rgxargs/constrainer'
7
9
  require_relative 'rgxargs/predefined_matchers'
8
10
  require_relative 'rgxargs/argument_matcher'
9
11
  require_relative 'rgxargs/syntax_definer'
12
+ include Checker
13
+ include Constrainer
14
+
10
15
  Predefined = PredefinedMatchers
11
16
 
12
17
  extend Forwardable
13
18
  def_delegators Predefined, :list_matcher
14
19
 
15
- attr_reader :args, :conversions, :defined_rules, :errors, :options, :syntaxes
20
+ attr_reader :allowed, :args, :conversions, :defaults, :defined_rules, :errors, :options, :required, :syntaxes
16
21
 
17
22
 
18
- def add_conversion(param, conversion)
23
+ def add_conversion(param, conversion=nil, required=nil, &block)
19
24
  case conversion
20
25
  when Symbol
21
- conversions[param] = Predefined.fetch(conversion, conversion)
26
+ _add_symbolic_conversion(param, conversion, required)
27
+ when NilClass
28
+ _add_simple_conversion(param, block)
22
29
  else
23
- conversions[param] = conversion
30
+ _add_proc_conversion(param, conversion, block, required)
24
31
  end
25
32
  end
26
33
 
27
34
  def add_syntax(rgx, parser=nil, as: nil)
28
- syntaxes << ArgumentMatcher.new(rgx, parser, arg_name: as)
35
+ case rgx
36
+ when Symbol, Regexp
37
+ syntaxes << ArgumentMatcher.new(rgx, parser, arg_name: as)
38
+ when Array
39
+ rgx.each do |rg1|
40
+ add_syntax( rg1, parser, as: as)
41
+ end
42
+ end
29
43
  end
30
44
 
31
45
  def define_arg name, &blk
@@ -36,24 +50,36 @@ module Lab42
36
50
  until argv.empty?
37
51
  argv = _parse_next argv
38
52
  end
53
+ _check_required_kwds
39
54
  [options, args, errors]
40
55
  end
41
56
 
42
57
 
58
+ def allows name, matcher=nil, &converter
59
+ add_conversion(name, matcher, &converter)
60
+ end
61
+
62
+ def needs name, matcher=nil, &converter
63
+ add_conversion(name, matcher, :required, &converter)
64
+ end
65
+
43
66
  private
44
67
 
45
68
  def initialize &blk
46
69
  @args = []
70
+ @allowed = nil
47
71
  @conversions = {}
72
+ @defaults = {}
48
73
  @defined_rules = []
49
74
  @errors = []
50
- @options = OpenStruct.new
75
+ @required = Set.new
51
76
  @syntaxes = []
52
77
 
53
78
  instance_exec(&blk) if blk
79
+ @options = OpenStruct.new(defaults)
54
80
  end
55
81
 
56
- def _convert(value, name:)
82
+ def _convert(value, name:)
57
83
  conv = conversions.fetch(name, nil)
58
84
  case conv
59
85
  when Symbol
@@ -62,7 +88,7 @@ module Lab42
62
88
  conv.(value)
63
89
  when Array
64
90
  if (match = conv.first.match(value))
65
- conv[1].(*match.captures)
91
+ conv[1].(*match.captures)
66
92
  else
67
93
  errors << [:syntax_error, name, "#{value} does not match #{conv.first}"]
68
94
  nil
@@ -72,6 +98,25 @@ module Lab42
72
98
  end
73
99
  end
74
100
 
101
+ def _add_proc_conversion(param, conversion, block, required)
102
+ Array(param).each do |para|
103
+ @required.add para if required == :required
104
+ conversions[para] = block ? [conversion, block] : conversion
105
+ end
106
+ end
107
+
108
+ def _add_simple_conversion(param, block)
109
+ Array(param).each do |para|
110
+ conversions[para] = block
111
+ end
112
+ end
113
+
114
+ def _add_symbolic_conversion(param, conversion, required)
115
+ Array(param).each do |para|
116
+ @required.add para if required == :required
117
+ conversions[para] = Predefined.fetch(conversion, conversion)
118
+ end
119
+ end
75
120
 
76
121
  def _parse_next argv
77
122
  first, *rest = argv
@@ -85,10 +130,14 @@ module Lab42
85
130
  def _parse_symbolic first, rest
86
131
  case first
87
132
  when %r{\A:(.*)}
88
- options[$1.gsub('-','_').to_sym]=true
133
+ switch = $1.gsub('-','_').to_sym
134
+ _check_switch(switch)
135
+ options[switch]=true
89
136
  rest
90
137
  when %r{(.*):\z}
91
- _parse_value $1.gsub('-', '_').to_sym, rest
138
+ kwd = $1.gsub('-', '_').to_sym
139
+ _check_kwd(kwd)
140
+ _parse_value kwd, rest
92
141
  when %r{\A\\(.*)}
93
142
  args << $1
94
143
  rest
@@ -0,0 +1,33 @@
1
+ module Lab42
2
+ class Rgxargs
3
+ module Checker
4
+
5
+ private
6
+
7
+ def _check_kwd(kwd)
8
+ return if allowed.nil?
9
+ return if allowed.member? kwd
10
+ return if required.member? kwd
11
+ errors << [:unauthorized_kwd, kwd]
12
+ end
13
+
14
+ def _check_required_kwds
15
+ missing = required - options.to_h.keys
16
+ @errors += missing.map(&_mk_pair(:required_kwd_missing))
17
+ end
18
+
19
+
20
+ def _mk_pair(prefix)
21
+ -> element do
22
+ [prefix, element]
23
+ end
24
+ end
25
+
26
+ def _check_switch(_)
27
+
28
+ end
29
+
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ module Lab42
2
+ class Rgxargs
3
+ module Constrainer
4
+
5
+ def allow_kwds(*kwds)
6
+ @allowed = (allowed||Set.new).union(Set.new(kwds.flatten))
7
+ end
8
+
9
+ def require_kwds(*kwds)
10
+ @required = (required||Set.new).union(Set.new(kwds.flatten))
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,6 @@
1
1
  module Lab42::Rgxargs::PredefinedMatchers extend self
2
2
  PREDEFINED = {
3
+ int: [%r{\A([-+]?\d+)\z}, :to_i.to_proc],
3
4
  int_list: [%r{\A(-?\d+(?:,-?\d+)*)\z}, ->(*groups){ groups.first.split(",").map(&:to_i)}],
4
5
  int_range: [%r{\A(-?\d+)(?:-|\.\.)(-?\d+)\z}, ->(f, l){ Range.new(f.to_i, l.to_i) }],
5
6
  list: [%r{(\w+)(?:,(\w+))*}, ->(*groups){ groups.compact }],
@@ -16,12 +17,11 @@ module Lab42::Rgxargs::PredefinedMatchers extend self
16
17
  end
17
18
 
18
19
  def list_matcher values
19
- [%r{\A((?:#{values.join("|")})(?:,(?:#{values.join("|")}))*)\z}, _list_extractor]
20
+ [%r{\A((?:#{values.join("|")})(?:,(?:#{values.join("|")}))*)\z}, method(:_list_extractor)]
20
21
  end
21
22
 
22
23
 
23
- # Memoized
24
- def _list_extractor
25
- @__list_extractor__ ||= ->(*groups){ groups.first.split(",") }
24
+ def _list_extractor(*groups)
25
+ groups.first.split(",")
26
26
  end
27
27
  end
@@ -0,0 +1,19 @@
1
+ class Lab42::Rgxargs::Syntax
2
+
3
+ attr_reader :converter, :matcher
4
+
5
+ def matches? value
6
+ if match = matcher.match(value)
7
+ yield converter.(*match.captures)
8
+ true
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def initialize matcher, converter
15
+ @converter = converter
16
+ @matcher = matcher
17
+ end
18
+
19
+ end
@@ -1,5 +1,16 @@
1
+ # Instanciated with an Rgxargs parser it can run a block
2
+ # in its own context so that the block accesses the
3
+ # parser's functionality with syntactic sugar w/o cluttering
4
+ # ther parser itself with too many methods
5
+ # E.g.:
6
+ #
7
+ # ```ruby
8
+ # syntax
9
+ #
10
+ # ```
1
11
  class Lab42::Rgxargs::SyntaxDefiner
2
12
 
13
+
3
14
  attr_reader :arg_name, :parser
4
15
 
5
16
  def run code
@@ -0,0 +1,5 @@
1
+ module Lab42
2
+ class Rgxargs
3
+ VERSION = "0.1.9"
4
+ end
5
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_rgxargs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-24 00:00:00.000000000 Z
11
+ date: 2020-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: speculate_about
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '3.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-doc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
41
69
  description: Parse CL args according to regexen
42
70
  email: robert.dober@gmail.com
43
71
  executables: []
@@ -47,14 +75,18 @@ files:
47
75
  - lib/lab42/enumerable.rb
48
76
  - lib/lab42/rgxargs.rb
49
77
  - lib/lab42/rgxargs/argument_matcher.rb
78
+ - lib/lab42/rgxargs/checker.rb
79
+ - lib/lab42/rgxargs/constrainer.rb
50
80
  - lib/lab42/rgxargs/error.rb
51
81
  - lib/lab42/rgxargs/predefined_matchers.rb
82
+ - lib/lab42/rgxargs/syntax.rb
52
83
  - lib/lab42/rgxargs/syntax_definer.rb
84
+ - lib/lab42/rgxargs/version.rb
53
85
  homepage: https://github.com/robertdober/lab42_rgxargs
54
86
  licenses:
55
87
  - Apache-2.0
56
88
  metadata: {}
57
- post_install_message:
89
+ post_install_message:
58
90
  rdoc_options: []
59
91
  require_paths:
60
92
  - lib
@@ -62,15 +94,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
94
  requirements:
63
95
  - - ">="
64
96
  - !ruby/object:Gem::Version
65
- version: 2.6.0
97
+ version: 2.7.0
66
98
  required_rubygems_version: !ruby/object:Gem::Requirement
67
99
  requirements:
68
100
  - - ">="
69
101
  - !ruby/object:Gem::Version
70
102
  version: '0'
71
103
  requirements: []
72
- rubygems_version: 3.0.6
73
- signing_key:
104
+ rubygems_version: 3.1.2
105
+ signing_key:
74
106
  specification_version: 4
75
107
  summary: Parse CL args according to regexen
76
108
  test_files: []