lab42_rgxargs 0.1.5 → 0.1.6
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 +43 -10
- data/lib/lab42/rgxargs/checker.rb +33 -0
- data/lib/lab42/rgxargs/constrainer.rb +15 -0
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c2b779615c5996b97aaa6fbb336adaecaba2119168d9cd6e28665bca4f26587
|
4
|
+
data.tar.gz: d1f125fb9a12342f54c3f6748d0a93c3782fc46d0abefd0330324a1ebe3aee3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dab565b74468cc86dfdedcc162343fc9b85eae244d908a0422b78ef68c5cae65c49e1eb0acf8463bc0a492a645f89441c2f71438e7671eac4534aca89b19bbdf
|
7
|
+
data.tar.gz: 7bb8d96d57afeb88a720c6500dcefda5b2f8fc24e877112c0930e6062e0918b5fa304aa3816e757a1885e51567ed24c3cfec238b73d30678d6e99fe7be4b8410
|
data/lib/lab42/rgxargs.rb
CHANGED
@@ -4,28 +4,40 @@ 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, required=nil, &block)
|
19
24
|
case conversion
|
20
25
|
when Symbol
|
21
|
-
|
26
|
+
_add_symbolic_conversion(param, conversion, required)
|
22
27
|
else
|
23
|
-
|
28
|
+
_add_proc_conversion(param, conversion, block, required)
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
27
32
|
def add_syntax(rgx, parser=nil, as: nil)
|
28
|
-
|
33
|
+
case rgx
|
34
|
+
when Symbol, Regexp
|
35
|
+
syntaxes << ArgumentMatcher.new(rgx, parser, arg_name: as)
|
36
|
+
when Array
|
37
|
+
rgx.each do |rg1|
|
38
|
+
add_syntax( rg1, parser, as: as)
|
39
|
+
end
|
40
|
+
end
|
29
41
|
end
|
30
42
|
|
31
43
|
def define_arg name, &blk
|
@@ -36,6 +48,7 @@ module Lab42
|
|
36
48
|
until argv.empty?
|
37
49
|
argv = _parse_next argv
|
38
50
|
end
|
51
|
+
_check_required_kwds
|
39
52
|
[options, args, errors]
|
40
53
|
end
|
41
54
|
|
@@ -44,16 +57,19 @@ module Lab42
|
|
44
57
|
|
45
58
|
def initialize &blk
|
46
59
|
@args = []
|
60
|
+
@allowed = nil
|
47
61
|
@conversions = {}
|
62
|
+
@defaults = {}
|
48
63
|
@defined_rules = []
|
49
64
|
@errors = []
|
50
|
-
@
|
65
|
+
@required = Set.new
|
51
66
|
@syntaxes = []
|
52
67
|
|
53
68
|
instance_exec(&blk) if blk
|
69
|
+
@options = OpenStruct.new(defaults)
|
54
70
|
end
|
55
71
|
|
56
|
-
def _convert(value, name:)
|
72
|
+
def _convert(value, name:)
|
57
73
|
conv = conversions.fetch(name, nil)
|
58
74
|
case conv
|
59
75
|
when Symbol
|
@@ -62,7 +78,7 @@ module Lab42
|
|
62
78
|
conv.(value)
|
63
79
|
when Array
|
64
80
|
if (match = conv.first.match(value))
|
65
|
-
conv[1].(*match.captures)
|
81
|
+
conv[1].(*match.captures)
|
66
82
|
else
|
67
83
|
errors << [:syntax_error, name, "#{value} does not match #{conv.first}"]
|
68
84
|
nil
|
@@ -72,6 +88,19 @@ module Lab42
|
|
72
88
|
end
|
73
89
|
end
|
74
90
|
|
91
|
+
def _add_proc_conversion(param, conversion, block, required)
|
92
|
+
Array(param).each do |para|
|
93
|
+
@required.add para if required == :required
|
94
|
+
conversions[para] = block ? [conversion, block] : conversion
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def _add_symbolic_conversion(param, conversion, required)
|
99
|
+
Array(param).each do |para|
|
100
|
+
@required.add para if required == :required
|
101
|
+
conversions[para] = Predefined.fetch(conversion, conversion)
|
102
|
+
end
|
103
|
+
end
|
75
104
|
|
76
105
|
def _parse_next argv
|
77
106
|
first, *rest = argv
|
@@ -85,10 +114,14 @@ module Lab42
|
|
85
114
|
def _parse_symbolic first, rest
|
86
115
|
case first
|
87
116
|
when %r{\A:(.*)}
|
88
|
-
|
117
|
+
switch = $1.gsub('-','_').to_sym
|
118
|
+
_check_switch(switch)
|
119
|
+
options[switch]=true
|
89
120
|
rest
|
90
121
|
when %r{(.*):\z}
|
91
|
-
|
122
|
+
kwd = $1.gsub('-', '_').to_sym
|
123
|
+
_check_kwd(kwd)
|
124
|
+
_parse_value kwd, rest
|
92
125
|
when %r{\A\\(.*)}
|
93
126
|
args << $1
|
94
127
|
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
|
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.
|
4
|
+
version: 0.1.6
|
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:
|
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.
|
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.
|
26
|
+
version: 0.1.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,6 +75,8 @@ 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
|
@@ -83,7 +85,7 @@ homepage: https://github.com/robertdober/lab42_rgxargs
|
|
83
85
|
licenses:
|
84
86
|
- Apache-2.0
|
85
87
|
metadata: {}
|
86
|
-
post_install_message:
|
88
|
+
post_install_message:
|
87
89
|
rdoc_options: []
|
88
90
|
require_paths:
|
89
91
|
- lib
|
@@ -99,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
101
|
version: '0'
|
100
102
|
requirements: []
|
101
103
|
rubygems_version: 3.1.2
|
102
|
-
signing_key:
|
104
|
+
signing_key:
|
103
105
|
specification_version: 4
|
104
106
|
summary: Parse CL args according to regexen
|
105
107
|
test_files: []
|