lab42_rgxargs 0.1.3 → 0.1.8
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 +54 -10
- data/lib/lab42/rgxargs/argument_matcher.rb +1 -1
- data/lib/lab42/rgxargs/checker.rb +33 -0
- data/lib/lab42/rgxargs/constrainer.rb +15 -0
- data/lib/lab42/rgxargs/predefined_matchers.rb +8 -8
- data/lib/lab42/rgxargs/syntax.rb +19 -0
- data/lib/lab42/rgxargs/syntax_definer.rb +11 -0
- data/lib/lab42/rgxargs/version.rb +5 -0
- metadata +39 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 859bcfceec315a00418253c045cb8819276bafd306e52c36a9f3c1f2f0c657aa
|
4
|
+
data.tar.gz: 1cb197a190eada29279019ef914e98af7015589a8aacfd1e16e3a4c728615892
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8c31ee9df616a66e6a0463a3778b227395cee9acabed1203d4ce46d840208d65829d9c4b48218453cd74d8f770e03538a737d5c6946cdb6486872fd964cd320
|
7
|
+
data.tar.gz: d817a9984f1853028979f179f97fd4b421a58deed2b081fa359f35351e73209b7183aabd1cf5466b86e4e0a20383d7f0b9927d369645ceaf0a47731085e5af7b
|
data/lib/lab42/rgxargs.rb
CHANGED
@@ -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
|
-
|
26
|
+
_add_symbolic_conversion(param, conversion, required)
|
27
|
+
when NilClass
|
28
|
+
_add_simple_conversion(param, block)
|
22
29
|
else
|
23
|
-
|
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
|
-
|
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,6 +50,7 @@ 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
|
|
@@ -44,16 +59,19 @@ module Lab42
|
|
44
59
|
|
45
60
|
def initialize &blk
|
46
61
|
@args = []
|
62
|
+
@allowed = nil
|
47
63
|
@conversions = {}
|
64
|
+
@defaults = {}
|
48
65
|
@defined_rules = []
|
49
66
|
@errors = []
|
50
|
-
@
|
67
|
+
@required = Set.new
|
51
68
|
@syntaxes = []
|
52
69
|
|
53
70
|
instance_exec(&blk) if blk
|
71
|
+
@options = OpenStruct.new(defaults)
|
54
72
|
end
|
55
73
|
|
56
|
-
def _convert(value, name:)
|
74
|
+
def _convert(value, name:)
|
57
75
|
conv = conversions.fetch(name, nil)
|
58
76
|
case conv
|
59
77
|
when Symbol
|
@@ -62,7 +80,7 @@ module Lab42
|
|
62
80
|
conv.(value)
|
63
81
|
when Array
|
64
82
|
if (match = conv.first.match(value))
|
65
|
-
conv[1].(match.captures)
|
83
|
+
conv[1].(*match.captures)
|
66
84
|
else
|
67
85
|
errors << [:syntax_error, name, "#{value} does not match #{conv.first}"]
|
68
86
|
nil
|
@@ -72,6 +90,25 @@ module Lab42
|
|
72
90
|
end
|
73
91
|
end
|
74
92
|
|
93
|
+
def _add_proc_conversion(param, conversion, block, required)
|
94
|
+
Array(param).each do |para|
|
95
|
+
@required.add para if required == :required
|
96
|
+
conversions[para] = block ? [conversion, block] : conversion
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def _add_simple_conversion(param, block)
|
101
|
+
Array(param).each do |para|
|
102
|
+
conversions[para] = block
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def _add_symbolic_conversion(param, conversion, required)
|
107
|
+
Array(param).each do |para|
|
108
|
+
@required.add para if required == :required
|
109
|
+
conversions[para] = Predefined.fetch(conversion, conversion)
|
110
|
+
end
|
111
|
+
end
|
75
112
|
|
76
113
|
def _parse_next argv
|
77
114
|
first, *rest = argv
|
@@ -85,10 +122,17 @@ module Lab42
|
|
85
122
|
def _parse_symbolic first, rest
|
86
123
|
case first
|
87
124
|
when %r{\A:(.*)}
|
88
|
-
|
125
|
+
switch = $1.gsub('-','_').to_sym
|
126
|
+
_check_switch(switch)
|
127
|
+
options[switch]=true
|
89
128
|
rest
|
90
129
|
when %r{(.*):\z}
|
91
|
-
|
130
|
+
kwd = $1.gsub('-', '_').to_sym
|
131
|
+
_check_kwd(kwd)
|
132
|
+
_parse_value kwd, rest
|
133
|
+
when %r{\A\\(.*)}
|
134
|
+
args << $1
|
135
|
+
rest
|
92
136
|
else
|
93
137
|
_parse_syntax(first)
|
94
138
|
rest
|
@@ -13,7 +13,7 @@ class Lab42::Rgxargs::ArgumentMatcher
|
|
13
13
|
case matcher
|
14
14
|
when Regexp
|
15
15
|
match = matcher.match(value)
|
16
|
-
match && [converter.(match.captures), arg_name]
|
16
|
+
match && [converter.(*match.captures), arg_name]
|
17
17
|
else
|
18
18
|
matcher.to_s == value && [converter.(), arg_name]
|
19
19
|
end
|
@@ -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,9 +1,10 @@
|
|
1
1
|
module Lab42::Rgxargs::PredefinedMatchers extend self
|
2
2
|
PREDEFINED = {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
int: [%r{\A([-+]?\d+)\z}, :to_i.to_proc],
|
4
|
+
int_list: [%r{\A(-?\d+(?:,-?\d+)*)\z}, ->(*groups){ groups.first.split(",").map(&:to_i)}],
|
5
|
+
int_range: [%r{\A(-?\d+)(?:-|\.\.)(-?\d+)\z}, ->(f, l){ Range.new(f.to_i, l.to_i) }],
|
6
|
+
list: [%r{(\w+)(?:,(\w+))*}, ->(*groups){ groups.compact }],
|
7
|
+
range: [%r{\A(\d+)\.\.(\d+)\z}, ->(*groups){ Range.new(*groups.map(&:to_i)) }]
|
7
8
|
}
|
8
9
|
|
9
10
|
def defined_names
|
@@ -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
|
-
|
24
|
-
|
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
|
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
|
+
version: 0.1.8
|
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-
|
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:
|
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.
|
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: []
|