anyway_config 1.4.0 → 1.4.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 +4 -4
- data/README.md +10 -2
- data/lib/anyway/config.rb +23 -10
- data/lib/anyway/env.rb +2 -2
- data/lib/anyway/ext/{string.rb → string_serialize.rb} +6 -6
- data/lib/anyway/option_parser_builder.rb +6 -9
- data/lib/anyway/version.rb +1 -1
- data/spec/config_spec.rb +14 -2
- data/spec/ext/{string_spec.rb → string_serialize_spec.rb} +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e1fad9e1aac1823b53c56253352f21964432c8c93fc7515308ac729aaead836f
|
|
4
|
+
data.tar.gz: 172f7208a1df440f37e531801623e54a31b65524b321e22ea2f821418a0b6e73
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a29addaa15fd5c9476476a4a3d3e9b4652116314f606c07b88dbc223cee8d626014a8d0ef18d8ca8c10ffbe355c760b462dfcb27d9299df399404d4c4187e149
|
|
7
|
+
data.tar.gz: c30bfbae04f2da1367f213be8fdb1312aed3c198472bb3a66695c85b59bcee1c0aca5b0c7ab099ca463c4d3c67b28c538950b5f9b9294887896bf707872303eb
|
data/README.md
CHANGED
|
@@ -164,7 +164,7 @@ Example usage:
|
|
|
164
164
|
|
|
165
165
|
```ruby
|
|
166
166
|
class MyConfig < Anyway::Config
|
|
167
|
-
attr_config :host, :log_level, :concurrency, server_args: {}
|
|
167
|
+
attr_config :host, :log_level, :concurrency, :debug, server_args: {}
|
|
168
168
|
|
|
169
169
|
# specify which options shouldn't be handled by option parser
|
|
170
170
|
ignore_options :server_args
|
|
@@ -174,9 +174,17 @@ class MyConfig < Anyway::Config
|
|
|
174
174
|
concurrency: "number of threads to use"
|
|
175
175
|
)
|
|
176
176
|
|
|
177
|
+
# mark some options as flag
|
|
178
|
+
flag_options :debug
|
|
179
|
+
|
|
177
180
|
# extend an option parser object (i.e. add banner or version/help handlers)
|
|
178
|
-
extend_options do |parser|
|
|
181
|
+
extend_options do |parser, config|
|
|
179
182
|
parser.banner = "mycli [options]"
|
|
183
|
+
|
|
184
|
+
parser.on("--server-args VALUE") do |value|
|
|
185
|
+
config.server_args = JSON.parse(value)
|
|
186
|
+
end
|
|
187
|
+
|
|
180
188
|
parser.on_tail "-h", "--help" do
|
|
181
189
|
puts parser
|
|
182
190
|
end
|
data/lib/anyway/config.rb
CHANGED
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
require 'anyway/ext/deep_dup'
|
|
4
4
|
require 'anyway/ext/deep_freeze'
|
|
5
5
|
require 'anyway/ext/hash'
|
|
6
|
+
require 'anyway/ext/string_serialize'
|
|
6
7
|
require 'anyway/option_parser_builder'
|
|
7
8
|
|
|
8
9
|
module Anyway # :nodoc:
|
|
9
10
|
using Anyway::Ext::DeepDup
|
|
10
11
|
using Anyway::Ext::DeepFreeze
|
|
11
12
|
using Anyway::Ext::Hash
|
|
13
|
+
using Anyway::Ext::StringSerialize
|
|
12
14
|
|
|
13
15
|
# Base config class
|
|
14
16
|
# Provides `attr_config` method to describe
|
|
@@ -38,13 +40,21 @@ module Anyway # :nodoc:
|
|
|
38
40
|
end
|
|
39
41
|
|
|
40
42
|
def ignore_options(*args)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
args.each do |name|
|
|
44
|
+
option_parser_descriptors[name.to_s][:ignore] = true
|
|
45
|
+
end
|
|
43
46
|
end
|
|
44
47
|
|
|
45
48
|
def describe_options(**hargs)
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
hargs.each do |name, desc|
|
|
50
|
+
option_parser_descriptors[name.to_s][:desc] = desc
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def flag_options(*args)
|
|
55
|
+
args.each do |name|
|
|
56
|
+
option_parser_descriptors[name.to_s][:flag] = true
|
|
57
|
+
end
|
|
48
58
|
end
|
|
49
59
|
|
|
50
60
|
def extend_options(&block)
|
|
@@ -52,12 +62,11 @@ module Anyway # :nodoc:
|
|
|
52
62
|
end
|
|
53
63
|
|
|
54
64
|
def option_parser_options
|
|
55
|
-
ignored_options = @ignore_options || []
|
|
56
|
-
descriptions = @option_parser_descriptions || {}
|
|
57
65
|
config_attributes.each_with_object({}) do |key, result|
|
|
58
|
-
|
|
66
|
+
descriptor = option_parser_descriptors[key.to_s]
|
|
67
|
+
next if descriptor[:ignore] == true
|
|
59
68
|
|
|
60
|
-
result[key]
|
|
69
|
+
result[key] = descriptor
|
|
61
70
|
end
|
|
62
71
|
end
|
|
63
72
|
|
|
@@ -79,6 +88,10 @@ module Anyway # :nodoc:
|
|
|
79
88
|
|
|
80
89
|
private
|
|
81
90
|
|
|
91
|
+
def option_parser_descriptors
|
|
92
|
+
@option_parser_descriptors ||= Hash.new { |h, k| h[k] = {} }
|
|
93
|
+
end
|
|
94
|
+
|
|
82
95
|
def underscore_name
|
|
83
96
|
return unless name
|
|
84
97
|
|
|
@@ -152,9 +165,9 @@ module Anyway # :nodoc:
|
|
|
152
165
|
def option_parser
|
|
153
166
|
@option_parser ||= begin
|
|
154
167
|
parser = OptionParserBuilder.call(self.class.option_parser_options) do |key, arg|
|
|
155
|
-
set_value(key, arg)
|
|
168
|
+
set_value(key, arg.is_a?(String) ? arg.serialize : arg)
|
|
156
169
|
end
|
|
157
|
-
self.class.option_parser_extension&.call(parser) || parser
|
|
170
|
+
self.class.option_parser_extension&.call(parser, self) || parser
|
|
158
171
|
end
|
|
159
172
|
end
|
|
160
173
|
|
data/lib/anyway/env.rb
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'anyway/ext/deep_dup'
|
|
4
|
-
require 'anyway/ext/
|
|
4
|
+
require 'anyway/ext/string_serialize'
|
|
5
5
|
|
|
6
6
|
module Anyway
|
|
7
7
|
# Parses environment variables and provides
|
|
8
8
|
# method-like access
|
|
9
9
|
class Env
|
|
10
10
|
using Anyway::Ext::DeepDup
|
|
11
|
-
using Anyway::Ext::
|
|
11
|
+
using Anyway::Ext::StringSerialize
|
|
12
12
|
|
|
13
13
|
def initialize
|
|
14
14
|
@data = {}
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
module Anyway
|
|
4
4
|
module Ext
|
|
5
5
|
# Extend String through refinements
|
|
6
|
-
module
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/
|
|
6
|
+
module StringSerialize
|
|
7
|
+
# Regexp to detect array values
|
|
8
|
+
# Array value is a values that contains at least one comma
|
|
9
|
+
# and doesn't start/end with quote
|
|
10
|
+
ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/
|
|
12
11
|
|
|
12
|
+
refine ::String do
|
|
13
13
|
# rubocop:disable Metrics/MethodLength
|
|
14
14
|
# rubocop:disable Metrics/CyclomaticComplexity
|
|
15
15
|
def serialize
|
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'optparse'
|
|
4
|
-
require 'anyway/ext/string'
|
|
5
4
|
|
|
6
5
|
module Anyway # :nodoc:
|
|
7
|
-
using Anyway::Ext::String
|
|
8
|
-
|
|
9
6
|
# Initializes the OptionParser instance using the given configuration
|
|
10
7
|
class OptionParserBuilder
|
|
11
8
|
class << self
|
|
12
9
|
def call(options)
|
|
13
10
|
OptionParser.new do |opts|
|
|
14
|
-
options.each do |key,
|
|
15
|
-
opts.on(*option_parser_on_args(key,
|
|
16
|
-
yield [key, arg
|
|
11
|
+
options.each do |key, descriptor|
|
|
12
|
+
opts.on(*option_parser_on_args(key, **descriptor)) do |arg|
|
|
13
|
+
yield [key, arg]
|
|
17
14
|
end
|
|
18
15
|
end
|
|
19
16
|
end
|
|
@@ -21,9 +18,9 @@ module Anyway # :nodoc:
|
|
|
21
18
|
|
|
22
19
|
private
|
|
23
20
|
|
|
24
|
-
def option_parser_on_args(key,
|
|
25
|
-
on_args = ["--#{key.to_s.tr('_', '-')} VALUE"]
|
|
26
|
-
on_args <<
|
|
21
|
+
def option_parser_on_args(key, flag: false, desc: nil)
|
|
22
|
+
on_args = ["--#{key.to_s.tr('_', '-')}#{flag ? '' : ' VALUE'}"]
|
|
23
|
+
on_args << desc unless desc.nil?
|
|
27
24
|
on_args
|
|
28
25
|
end
|
|
29
26
|
end
|
data/lib/anyway/version.rb
CHANGED
data/spec/config_spec.rb
CHANGED
|
@@ -241,11 +241,12 @@ describe Anyway::Config do
|
|
|
241
241
|
Class.new(described_class) do
|
|
242
242
|
config_name 'optparse'
|
|
243
243
|
attr_config :host, :port, :log_level, :debug
|
|
244
|
+
flag_options :debug
|
|
244
245
|
end
|
|
245
246
|
end
|
|
246
247
|
|
|
247
248
|
it "parses ARGC string" do
|
|
248
|
-
config_instance.parse_options!(%w[--host localhost --port 3333 --log-level debug --debug
|
|
249
|
+
config_instance.parse_options!(%w[--host localhost --port 3333 --log-level debug --debug])
|
|
249
250
|
expect(config_instance.host).to eq("localhost")
|
|
250
251
|
expect(config_instance.port).to eq(3333)
|
|
251
252
|
expect(config_instance.log_level).to eq("debug")
|
|
@@ -300,9 +301,13 @@ describe Anyway::Config do
|
|
|
300
301
|
config_name 'optparse'
|
|
301
302
|
attr_config :host, :log_level, :concurrency, server_args: {}
|
|
302
303
|
|
|
303
|
-
extend_options do |parser|
|
|
304
|
+
extend_options do |parser, config|
|
|
304
305
|
parser.banner = "mycli [options]"
|
|
305
306
|
|
|
307
|
+
parser.on("--server-args VALUE") do |value|
|
|
308
|
+
config.server_args = JSON.parse(value)
|
|
309
|
+
end
|
|
310
|
+
|
|
306
311
|
parser.on_tail "-h", "--help" do
|
|
307
312
|
puts parser
|
|
308
313
|
end
|
|
@@ -313,6 +318,13 @@ describe Anyway::Config do
|
|
|
313
318
|
it "allows to customize the parser" do
|
|
314
319
|
expect(config_instance.option_parser.help).to include("mycli [options]")
|
|
315
320
|
end
|
|
321
|
+
|
|
322
|
+
it "passes config to extension" do
|
|
323
|
+
config_instance.parse_options!(
|
|
324
|
+
['--server-args', '{"host":"0.0.0.0"}']
|
|
325
|
+
)
|
|
326
|
+
expect(config_instance.server_args["host"]).to eq "0.0.0.0"
|
|
327
|
+
end
|
|
316
328
|
end
|
|
317
329
|
end
|
|
318
330
|
end
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
|
-
describe Anyway::Ext::
|
|
6
|
-
using Anyway::Ext::
|
|
5
|
+
describe Anyway::Ext::StringSerialize do
|
|
6
|
+
using Anyway::Ext::StringSerialize
|
|
7
7
|
|
|
8
8
|
it "serializes a string", :aggregate_failures do
|
|
9
9
|
expect("1,2, 3".serialize).to eq [1, 2, 3]
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: anyway_config
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.4.
|
|
4
|
+
version: 1.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vladimir Dementyev
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2018-10-
|
|
11
|
+
date: 2018-10-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|
|
@@ -82,7 +82,7 @@ files:
|
|
|
82
82
|
- lib/anyway/ext/deep_dup.rb
|
|
83
83
|
- lib/anyway/ext/deep_freeze.rb
|
|
84
84
|
- lib/anyway/ext/hash.rb
|
|
85
|
-
- lib/anyway/ext/
|
|
85
|
+
- lib/anyway/ext/string_serialize.rb
|
|
86
86
|
- lib/anyway/option_parser_builder.rb
|
|
87
87
|
- lib/anyway/rails/config.rb
|
|
88
88
|
- lib/anyway/version.rb
|
|
@@ -103,7 +103,7 @@ files:
|
|
|
103
103
|
- spec/ext/deep_dup_spec.rb
|
|
104
104
|
- spec/ext/deep_freeze_spec.rb
|
|
105
105
|
- spec/ext/hash_spec.rb
|
|
106
|
-
- spec/ext/
|
|
106
|
+
- spec/ext/string_serialize_spec.rb
|
|
107
107
|
- spec/spec_helper.rb
|
|
108
108
|
- spec/spec_norails_helper.rb
|
|
109
109
|
- spec/support/cool_config.rb
|