lab42_rgxargs 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a8e40069cd0f641fbe3813892a34648656d0d5316f1100ad46f5a8edf7712efe
4
+ data.tar.gz: 4b2cd38e43a2329c163f7ace5d0aa1ab4e40f6375408637ee650fc6d862756a8
5
+ SHA512:
6
+ metadata.gz: a087544804de877921e4080f965edc1c78af408401f29f2b3465e7e3ceeaa3b0b470d3cc2b5bc25489a7010f6dbf5ce1f849c333797c7559ff9a03c9a672ec1e
7
+ data.tar.gz: ac73504757f56373a57e2e3bb2218e7060b797bf2b39421f3f41e22a2af8585d7a4e9ee373d5bf58b990916cd1c9a1d0aeda7d758ecd49a8760c6643fa16b1c7
@@ -0,0 +1,9 @@
1
+ module Enumerable
2
+ def find_value default=nil, &blk
3
+ each do |ele|
4
+ value = blk.(ele)
5
+ return value if value
6
+ end
7
+ default
8
+ end
9
+ end
@@ -0,0 +1,102 @@
1
+ require 'ostruct'
2
+ require 'lab42/enumerable'
3
+ class Lab42::Rgxargs
4
+ PREDEFINED = {
5
+ list: [%r{(\w+)(?:,(\w+))*}, ->(groups){ groups }],
6
+ range: [%r{\A(\d+)\.\.(\d+)\z}, ->(groups){ Range.new(*groups.map(&:to_i)) }]
7
+ }
8
+
9
+ attr_reader :args, :conversions, :defined_rules, :errors, :options, :syntaxes
10
+
11
+ def add_conversion(param, conversion)
12
+ conversions[param] = conversion
13
+ end
14
+
15
+ def add_syntax(rgx, parser=nil)
16
+ if parser
17
+ syntaxes << [rgx, parser]
18
+ else
19
+ if PREDEFINED[rgx]
20
+ syntaxes << PREDEFINED[rgx]
21
+ else
22
+ raise ArgumentError, "#{rgx} is not a predefined syntax, use one of the following:\n\t#{PREDEFINED.keys.join("\n\t")}"
23
+ end
24
+ end
25
+ end
26
+
27
+
28
+ def parse argv
29
+ until argv.empty?
30
+ argv = _parse_next argv
31
+ end
32
+ [options, args, errors]
33
+ end
34
+
35
+
36
+ private
37
+
38
+ def initialize &blk
39
+ @args = []
40
+ @conversions = {}
41
+ @defined_rules = []
42
+ @errors = []
43
+ @options = OpenStruct.new
44
+ @syntaxes = []
45
+
46
+ instance_exec(&blk) if blk
47
+ end
48
+
49
+ def _convert(value, name:)
50
+ conv = conversions.fetch(name, nil)
51
+ case conv
52
+ when Symbol
53
+ value.send conv
54
+ when Proc
55
+ conv.(value)
56
+ else
57
+ value
58
+ end
59
+ end
60
+
61
+ def _defined_rules(_arg)
62
+ false
63
+ end
64
+
65
+ def _parse_next argv
66
+ first, *rest = argv
67
+ if _defined_rules(first)
68
+ return rest
69
+ end
70
+ _parse_symbolic first, rest
71
+ end
72
+
73
+ def _parse_symbolic first, rest
74
+ case first
75
+ when %r{\A:(.*)}
76
+ options[$1.gsub('-','_').to_sym]=true
77
+ rest
78
+ when %r{(.*):\z}
79
+ _parse_value $1.gsub('-', '_').to_sym, rest
80
+ else
81
+ _parse_syntax(first)
82
+ rest
83
+ end
84
+ end
85
+
86
+ def _parse_syntax first
87
+ args << syntaxes.find_value(first) do |(rgx, converter)|
88
+ (match = rgx.match(first)) && converter.(match.captures)
89
+ end
90
+ end
91
+
92
+ def _parse_value name, rest
93
+ value, *rest1 = rest
94
+ if value
95
+ options[name] = _convert(value, name: name)
96
+ return rest1
97
+ end
98
+ errors << [:missing_required_value, name]
99
+ []
100
+ end
101
+
102
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lab42_rgxargs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ platform: ruby
6
+ authors:
7
+ - Robert Dober
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.7'
41
+ description: Parse CL args according to regexen
42
+ email: robert.dober@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/lab42/enumerable.rb
48
+ - lib/lab42/rgxargs.rb
49
+ homepage: https://github.com/robertdober/lab42_rgxargs
50
+ licenses:
51
+ - Apache-2.0
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">"
65
+ - !ruby/object:Gem::Version
66
+ version: 1.3.1
67
+ requirements: []
68
+ rubygems_version: 3.0.6
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Parse CL args according to regexen
72
+ test_files: []