lab42_rgxargs 0.1.5 → 0.1.10

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: 47d38fb3f0f3f9881fd9ccc16ca4c883b88d4c275d7e18f857bd61da29c595a6
4
- data.tar.gz: 489530b10f2d2aa4ce02c4f3766aae359b5a03742bd3109bcf902839103d93ac
3
+ metadata.gz: 5fae43fdec0a7fa2a2a63de4b8d09af54dfcc8edb0896541b34c014d1159555f
4
+ data.tar.gz: '098a4febbda2d76145e9c86e3125838ddbbdea669e1fdc682f97866f76c44974'
5
5
  SHA512:
6
- metadata.gz: a08a0dd92f963b90860f833ff070b41a1da5dc0c1725a3e67d8748277db9be7173ac2527f1f25eabeebbb7e3a67cc847b240466f2ce06da1d992d57f325fae38
7
- data.tar.gz: bcf5fdb925293e0a0ad10bfb372b6a3ae23990cb7ff72ea08baa621c45a09a181216592b36423d62663da6a85df562ab4b4f0643a95d7d8316eebc46cea72b9b
6
+ metadata.gz: 111debca1b9c019b820976c1400e4991f5e0b5ccc72167b15967d9e4ccb45770d9a97ed68551e9d997ce36f58ec180c2498b384a97cc2b0f7dd56079f4b9007d
7
+ data.tar.gz: 7d1cecefc9996d3274c0703136babdc7e6d5a03310f68c7a0639c3cc6c1da5c3df0f640f898a0b30ab5db46a39afe23aee5b6e475d6dbdad70e56729e51e9fdc
@@ -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, &block)
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] = block ? [conversion, block] : 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
@@ -0,0 +1,5 @@
1
+ module Lab42
2
+ class Rgxargs
3
+ VERSION = "0.1.10"
4
+ end
5
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_rgxargs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2020-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: lab42_speculate
14
+ name: speculate_about
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.4
19
+ version: 0.1.2
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.4
26
+ version: 0.1.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -75,15 +75,18 @@ files:
75
75
  - lib/lab42/enumerable.rb
76
76
  - lib/lab42/rgxargs.rb
77
77
  - lib/lab42/rgxargs/argument_matcher.rb
78
+ - lib/lab42/rgxargs/checker.rb
79
+ - lib/lab42/rgxargs/constrainer.rb
78
80
  - lib/lab42/rgxargs/error.rb
79
81
  - lib/lab42/rgxargs/predefined_matchers.rb
80
82
  - lib/lab42/rgxargs/syntax.rb
81
83
  - lib/lab42/rgxargs/syntax_definer.rb
84
+ - lib/lab42/rgxargs/version.rb
82
85
  homepage: https://github.com/robertdober/lab42_rgxargs
83
86
  licenses:
84
87
  - Apache-2.0
85
88
  metadata: {}
86
- post_install_message:
89
+ post_install_message:
87
90
  rdoc_options: []
88
91
  require_paths:
89
92
  - lib
@@ -99,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
102
  version: '0'
100
103
  requirements: []
101
104
  rubygems_version: 3.1.2
102
- signing_key:
105
+ signing_key:
103
106
  specification_version: 4
104
107
  summary: Parse CL args according to regexen
105
108
  test_files: []