uniparser 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/LICENSE.md +9 -0
- data/README.md +5 -0
- data/lib/uniparser.rb +261 -0
- data/uniparser.gemspec +21 -0
- metadata +76 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: cbc17bb048980448779f025667ef8a053000f6026cfafa52a3fd134a559aff8c
|
|
4
|
+
data.tar.gz: 0b8884ae523153e6328cab7dd0ba9a016e6610d94854b249ee41258490767dcd
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5a285c20c5759077b73d1577978eeff8efb932c9094531c5e6195f23c538947671c3ac05ef08da9c0dcf9b86f1de0686e0a364fed6f3587188f59deb844f551e
|
|
7
|
+
data.tar.gz: fc90daf52ab7f3464d987d64955cf4bc0975399b103f3303b5d826d3c96581a13f4a581545992d7c1b5452a32302314979f6e55ce1b67c414a2842575a8cdd4e
|
data/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.gem
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Pavel Nazarov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/lib/uniparser.rb
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# Author:: Pavel Nazarov <pnazarov@gmail.com>
|
|
2
|
+
# Copyright:: Copyright (c) 2017 Pavel Nazarov
|
|
3
|
+
# License:: MIT
|
|
4
|
+
# URL:: https://github.com/alsvartr
|
|
5
|
+
|
|
6
|
+
# Simple and straightforward config & cli options parser
|
|
7
|
+
# Based on ruby-parseconfig class by BJ Dierkes <derks@datafolklabs.com>
|
|
8
|
+
# https://github.com/datafolklabs/ruby-parseconfig
|
|
9
|
+
|
|
10
|
+
class UniParser
|
|
11
|
+
|
|
12
|
+
attr_accessor :config_file, :delim, :cli_desc, :banner, :groups, :cli, :config, :params, :merged
|
|
13
|
+
|
|
14
|
+
def initialize(config_file = nil)
|
|
15
|
+
self.config_file = config_file
|
|
16
|
+
|
|
17
|
+
self.groups = []
|
|
18
|
+
self.cli = Hash[:free => ARGV]
|
|
19
|
+
self.config = Hash[]
|
|
20
|
+
self.merged = Hash[]
|
|
21
|
+
self.params = Hash[:cli => self.cli, :config => self.config, :merged => self.merged]
|
|
22
|
+
self.cli_desc = Array.new
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def parse_all(config_file = nil, cli = ARGV)
|
|
26
|
+
config_file = self.config_file if self.config_file != nil
|
|
27
|
+
return nil if config_file == nil
|
|
28
|
+
self.config_file = config_file
|
|
29
|
+
|
|
30
|
+
self.cli[:free] = cli
|
|
31
|
+
|
|
32
|
+
self.parse_file(config_file)
|
|
33
|
+
self.parse_cli([], nil)
|
|
34
|
+
#self.merge()
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def parse_file(config_file = nil)
|
|
38
|
+
config_file = self.config_file if self.config_file != nil
|
|
39
|
+
return nil if config_file == nil
|
|
40
|
+
self.config_file = config_file
|
|
41
|
+
|
|
42
|
+
self.validate_config
|
|
43
|
+
self.import_config
|
|
44
|
+
#self.merge()
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def parse_cli(names, desc, next_is_val = true, merge_with = nil)
|
|
48
|
+
merge = lambda { |val|
|
|
49
|
+
# FIXME: add dynamic hash merging
|
|
50
|
+
if merge_with.size == 1
|
|
51
|
+
self.merged[:"#{merge_with[0]}"] = self.cli[:"#{gsub_param}"]
|
|
52
|
+
else
|
|
53
|
+
self.merged[:"#{merge_with[0]}"] = Hash[] if self.merged[:"#{merge_with[0]}"].class.to_s != "Hash"
|
|
54
|
+
self.merged[:"#{merge_with[0]}"][:"#{merge_with[1]}"] = val
|
|
55
|
+
end
|
|
56
|
+
}
|
|
57
|
+
merge.call( self.config[:"#{merge_with[0]}"][:"#{merge_with[1]}"] ) if merge_with != nil
|
|
58
|
+
|
|
59
|
+
self.cli_desc.push ( Array[names, desc, next_is_val] ) if names.size != 0
|
|
60
|
+
return nil if self.cli[:free].size == 0
|
|
61
|
+
|
|
62
|
+
self.cli[:free].each_with_index do |param, index|
|
|
63
|
+
next if not names.include? param
|
|
64
|
+
|
|
65
|
+
gsub_param = names[1].gsub(/^-+/, "")
|
|
66
|
+
if next_is_val
|
|
67
|
+
self.cli[:"#{gsub_param}"] = self.cli[:free][index + 1]
|
|
68
|
+
self.cli[:free].delete_at( index + 1 )
|
|
69
|
+
else
|
|
70
|
+
self.cli[:"#{gsub_param}"] = true
|
|
71
|
+
end
|
|
72
|
+
self.cli[:free].delete_at(index)
|
|
73
|
+
|
|
74
|
+
merge.call( self.cli[:"#{gsub_param}"] ) if merge_with != nil
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def bound_cli(opt_name, opt_index)
|
|
79
|
+
return nil if opt_index >= self.cli[:free].size
|
|
80
|
+
self.cli[:"#{opt_name}"] = self.cli[:free][opt_index]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def types(val)
|
|
84
|
+
return nil if val == nil
|
|
85
|
+
|
|
86
|
+
if val.class.to_s == "Hash"
|
|
87
|
+
val.each do |h_var, h_val|
|
|
88
|
+
val[h_var] = self.types(h_val) # recursion rocks!
|
|
89
|
+
end
|
|
90
|
+
return val
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
case val
|
|
94
|
+
when /^[0-9]+$/
|
|
95
|
+
val = val.to_i
|
|
96
|
+
when /^true$/
|
|
97
|
+
val = true
|
|
98
|
+
when /^false$/
|
|
99
|
+
val = false
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
return val
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def types!()
|
|
106
|
+
self.params.each {|var, val| self.params[var] = self.types(val) }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def show_banner(sort = false)
|
|
110
|
+
puts "#{self.banner}\n\n" if self.banner
|
|
111
|
+
|
|
112
|
+
out = Array.new
|
|
113
|
+
out_desc = Array.new
|
|
114
|
+
str_len = 0
|
|
115
|
+
self.cli_desc.sort! if sort
|
|
116
|
+
|
|
117
|
+
self.cli_desc.each do |arr|
|
|
118
|
+
all_names = ""
|
|
119
|
+
names = arr[0]
|
|
120
|
+
desc = arr[1]
|
|
121
|
+
next_is_val = arr[2]
|
|
122
|
+
|
|
123
|
+
names.each {|name| all_names += name.ljust(7) }
|
|
124
|
+
all_names += " [value]" if next_is_val
|
|
125
|
+
str_len = all_names.size if all_names.size > str_len
|
|
126
|
+
|
|
127
|
+
out.push(all_names)
|
|
128
|
+
out_desc.push(desc)
|
|
129
|
+
end
|
|
130
|
+
out.each_with_index {|str, index| print "".ljust(5); print "#{str.ljust(str_len*2)}#{out_desc[index]}\n" }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def validate_config()
|
|
134
|
+
unless File.readable?(self.config_file)
|
|
135
|
+
raise Errno::EACCES, "#{self.config_file} is not readable"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# FIXME: need to validate contents/structure?
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def import_config()
|
|
142
|
+
# The config is top down.. anything after a [group] gets added as part
|
|
143
|
+
# of that group until a new [group] is found.
|
|
144
|
+
group = nil
|
|
145
|
+
open(self.config_file) { |f| f.each_with_index do |line, i|
|
|
146
|
+
line.strip!
|
|
147
|
+
|
|
148
|
+
# force_encoding not available in all versions of ruby
|
|
149
|
+
begin
|
|
150
|
+
if i.eql? 0 and line.include?("\xef\xbb\xbf".force_encoding("UTF-8"))
|
|
151
|
+
line.delete!("\xef\xbb\xbf".force_encoding("UTF-8"))
|
|
152
|
+
end
|
|
153
|
+
rescue NoMethodError
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
unless (/^\#/.match(line))
|
|
157
|
+
if(/\s*=\s*/.match(line))
|
|
158
|
+
param, value = line.split(/\s*=\s*/, 2)
|
|
159
|
+
var_name = "#{param}".chomp.strip
|
|
160
|
+
value = value.chomp.strip
|
|
161
|
+
new_value = ''
|
|
162
|
+
if (value)
|
|
163
|
+
if value =~ /^['"](.*)['"]$/
|
|
164
|
+
new_value = $1
|
|
165
|
+
else
|
|
166
|
+
new_value = value
|
|
167
|
+
end
|
|
168
|
+
else
|
|
169
|
+
new_value = ''
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
if group
|
|
173
|
+
self.add_to_group(group, var_name, new_value)
|
|
174
|
+
else
|
|
175
|
+
self.add(var_name, new_value)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
elsif(/^\[(.+)\]$/.match(line).to_a != [])
|
|
179
|
+
group = /^\[(.+)\]$/.match(line).to_a[1]
|
|
180
|
+
self.add(group, {})
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end }
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def get_value(param)
|
|
187
|
+
puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \
|
|
188
|
+
"config['param'] or config['group']['param'] instead."
|
|
189
|
+
return self.params[param]
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def [](param)
|
|
193
|
+
return self.params[param]
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def get_params()
|
|
197
|
+
return self.params.keys
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def get_groups()
|
|
201
|
+
return self.groups
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def add(param_name, value)
|
|
205
|
+
if value.class == Hash
|
|
206
|
+
if self.config.has_key?(param_name)
|
|
207
|
+
if self.config[:"#{param_name}"].class == Hash
|
|
208
|
+
self.config[:"#{param_name}"].merge!(value)
|
|
209
|
+
elsif self.config.has_key?(param_name)
|
|
210
|
+
if self.config[:"#{param_name}"].class != value.class
|
|
211
|
+
raise ArgumentError, "#{param_name} already exists, and is of different type!"
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
else
|
|
215
|
+
self.config[:"#{param_name}"] = value
|
|
216
|
+
end
|
|
217
|
+
if ! self.groups.include?(param_name)
|
|
218
|
+
self.groups.push(param_name)
|
|
219
|
+
end
|
|
220
|
+
else
|
|
221
|
+
self.config[:"#{param_name}"] = value
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def add_to_group(group, param_name, value)
|
|
226
|
+
if ! self.groups.include?(group)
|
|
227
|
+
self.add(group, {})
|
|
228
|
+
end
|
|
229
|
+
self.config[:"#{group}"][:"#{param_name}"] = value
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def write(output_stream=STDOUT, quoted=true)
|
|
233
|
+
self.params.each do |name,value|
|
|
234
|
+
if value.class.to_s != 'Hash'
|
|
235
|
+
if quoted == true
|
|
236
|
+
output_stream.puts "#{name} = \"#{value}\""
|
|
237
|
+
else
|
|
238
|
+
output_stream.puts "#{name} = #{value}"
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
output_stream.puts "\n"
|
|
243
|
+
|
|
244
|
+
self.groups.each do |group|
|
|
245
|
+
output_stream.puts "[#{group}]"
|
|
246
|
+
self.params[group].each do |param, value|
|
|
247
|
+
if quoted == true
|
|
248
|
+
output_stream.puts "#{param} = \"#{value}\""
|
|
249
|
+
else
|
|
250
|
+
output_stream.puts "#{param} = #{value}"
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
output_stream.puts "\n"
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def eql?(other)
|
|
258
|
+
self.params == other.params && self.groups == other.groups
|
|
259
|
+
end
|
|
260
|
+
alias == eql?
|
|
261
|
+
end
|
data/uniparser.gemspec
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Gem::Specification.new do |spec|
|
|
2
|
+
spec.name = 'uniparser'
|
|
3
|
+
spec.version = '0.1'
|
|
4
|
+
spec.authors = ['Pavel Nazarov']
|
|
5
|
+
spec.email = ['nazarov.pn@gmail.com']
|
|
6
|
+
spec.licenses = ['MIT']
|
|
7
|
+
|
|
8
|
+
spec.summary = 'Simple and straightforward config & cli options parser library'
|
|
9
|
+
spec.description = 'Simple and straightforward config & cli options parser library'
|
|
10
|
+
spec.homepage = 'https://github.com/alsvartr/ruby-uniparser'
|
|
11
|
+
|
|
12
|
+
spec.required_ruby_version = '>= 2.0.0'
|
|
13
|
+
|
|
14
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
15
|
+
spec.bindir = 'exe'
|
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
17
|
+
spec.require_paths = ['lib']
|
|
18
|
+
|
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1'
|
|
20
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
|
21
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: uniparser
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: '0.1'
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Pavel Nazarov
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-09-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3'
|
|
41
|
+
description: Simple and straightforward config & cli options parser library
|
|
42
|
+
email:
|
|
43
|
+
- nazarov.pn@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".gitignore"
|
|
49
|
+
- LICENSE.md
|
|
50
|
+
- README.md
|
|
51
|
+
- lib/uniparser.rb
|
|
52
|
+
- uniparser.gemspec
|
|
53
|
+
homepage: https://github.com/alsvartr/ruby-uniparser
|
|
54
|
+
licenses:
|
|
55
|
+
- MIT
|
|
56
|
+
metadata: {}
|
|
57
|
+
post_install_message:
|
|
58
|
+
rdoc_options: []
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: 2.0.0
|
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
requirements: []
|
|
72
|
+
rubygems_version: 3.4.13
|
|
73
|
+
signing_key:
|
|
74
|
+
specification_version: 4
|
|
75
|
+
summary: Simple and straightforward config & cli options parser library
|
|
76
|
+
test_files: []
|