config_parser 0.3.0 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History +14 -0
- data/lib/config_parser/flag.rb +55 -14
- data/lib/config_parser/list.rb +11 -5
- data/lib/config_parser/option.rb +1 -1
- data/lib/config_parser/version.rb +1 -1
- data/lib/config_parser.rb +40 -19
- metadata +19 -4
data/History
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
== 0.3.3 / 2010-10-24
|
2
|
+
|
3
|
+
* made options aware of whether or not they have assigned a value (fixes
|
4
|
+
a bug whereby lists would not override defaults)
|
5
|
+
* added ability to specify a hint string
|
6
|
+
|
7
|
+
== 0.3.2 / 2010-10-16
|
8
|
+
|
9
|
+
* allow specification of any callback that responds to call
|
10
|
+
|
11
|
+
== 0.3.1 / 2010-10-07
|
12
|
+
|
13
|
+
* added nest keys to default guess for long
|
14
|
+
|
1
15
|
== 0.3.0 / 2010-10-02
|
2
16
|
|
3
17
|
* Cleanup of some variable and option names
|
data/lib/config_parser/flag.rb
CHANGED
@@ -8,35 +8,45 @@ class ConfigParser
|
|
8
8
|
class Flag
|
9
9
|
include Utils
|
10
10
|
|
11
|
-
# The config key
|
11
|
+
# The config key.
|
12
12
|
attr_reader :key
|
13
13
|
|
14
|
-
# The config nesting keys
|
14
|
+
# The config nesting keys.
|
15
15
|
attr_reader :nest_keys
|
16
16
|
|
17
|
-
# The default value
|
17
|
+
# The default value.
|
18
18
|
attr_reader :default
|
19
19
|
|
20
|
-
# The short flag mapping to self
|
20
|
+
# The short flag mapping to self.
|
21
21
|
attr_reader :short
|
22
22
|
|
23
|
-
# The long flag mapping to self
|
23
|
+
# The long flag mapping to self.
|
24
24
|
attr_reader :long
|
25
25
|
|
26
|
-
# The description printed by to_s
|
26
|
+
# The description printed by to_s.
|
27
27
|
attr_reader :desc
|
28
28
|
|
29
|
-
# A
|
29
|
+
# A hint printed by to_s, after desc.
|
30
|
+
attr_reader :hint
|
31
|
+
|
32
|
+
# A callback for processing values (must respond to call, or be nil).
|
30
33
|
attr_reader :callback
|
31
34
|
|
32
|
-
|
35
|
+
# A tracking flag set to true when assign is called. Useful when assign
|
36
|
+
# works differently for the first assignment than later assignments. See
|
37
|
+
# reset.
|
38
|
+
attr_reader :assigned
|
39
|
+
|
40
|
+
def initialize(attrs={})
|
33
41
|
@key = attrs[:key]
|
34
42
|
@nest_keys = attrs[:nest_keys]
|
35
43
|
@default = attrs[:default]
|
36
44
|
@short = shortify(attrs[:short])
|
37
|
-
@long = longify(attrs.has_key?(:long) ? attrs[:long] :
|
45
|
+
@long = longify(attrs.has_key?(:long) ? attrs[:long] : default_long)
|
38
46
|
@desc = attrs[:desc]
|
39
|
-
@
|
47
|
+
@hint = attrs[:hint]
|
48
|
+
@callback = attrs[:callback]
|
49
|
+
reset
|
40
50
|
end
|
41
51
|
|
42
52
|
# Returns an array of flags mapping to self (ie [long, short]).
|
@@ -96,13 +106,24 @@ class ConfigParser
|
|
96
106
|
callback ? callback.call(value) : value
|
97
107
|
end
|
98
108
|
|
99
|
-
#
|
100
|
-
|
109
|
+
# Assigns the default value into config and resets the assigned flag to
|
110
|
+
# false, such that the next assign behaves as if self has not put a value
|
111
|
+
# into config. Returns config.
|
112
|
+
def assign_default(config)
|
113
|
+
assign(config, default)
|
114
|
+
reset
|
115
|
+
config
|
116
|
+
end
|
117
|
+
|
118
|
+
# Assign the value to the config hash, if key is set, and flips assigned
|
119
|
+
# to true. Returns config.
|
120
|
+
def assign(config, value)
|
101
121
|
if key
|
102
122
|
nest_config = nest(config)
|
103
123
|
nest_config[key] = value
|
104
124
|
end
|
105
125
|
|
126
|
+
@assigned = true
|
106
127
|
config
|
107
128
|
end
|
108
129
|
|
@@ -115,11 +136,16 @@ class ConfigParser
|
|
115
136
|
config
|
116
137
|
end
|
117
138
|
|
139
|
+
# Resets assigned to false.
|
140
|
+
def reset
|
141
|
+
@assigned = false
|
142
|
+
end
|
143
|
+
|
118
144
|
# Formats self as a help string for use on the command line.
|
119
145
|
def to_s
|
120
|
-
lines = wrap(
|
146
|
+
lines = wrap(desc_str, 43)
|
121
147
|
|
122
|
-
header =
|
148
|
+
header = header_str
|
123
149
|
header = header.length > 36 ? header.ljust(80) : (LINE_FORMAT % [header, lines.shift])
|
124
150
|
|
125
151
|
if lines.empty?
|
@@ -152,5 +178,20 @@ class ConfigParser
|
|
152
178
|
def long_str # :nodoc:
|
153
179
|
long
|
154
180
|
end
|
181
|
+
|
182
|
+
def desc_str # :nodoc:
|
183
|
+
case
|
184
|
+
when hint.nil? && desc.nil?
|
185
|
+
''
|
186
|
+
when hint.nil?
|
187
|
+
desc.to_s
|
188
|
+
else
|
189
|
+
"#{desc} (#{hint})"
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def default_long # :nodoc:
|
194
|
+
nest_keys ? (nest_keys + [key]).join(':') : key
|
195
|
+
end
|
155
196
|
end
|
156
197
|
end
|
data/lib/config_parser/list.rb
CHANGED
@@ -22,19 +22,25 @@ class ConfigParser
|
|
22
22
|
|
23
23
|
# Splits the value into multiple values, and then process as usual.
|
24
24
|
def process(value)
|
25
|
-
|
25
|
+
split(value).collect {|val| super(val) }
|
26
26
|
end
|
27
27
|
|
28
|
-
# Assigns the values to config
|
29
|
-
#
|
30
|
-
# not set.
|
31
|
-
def assign(config, values
|
28
|
+
# Assigns the values to config. Multiple calls to assign will concatenate
|
29
|
+
# (ie when assigned is true) new values onto the existing values. As
|
30
|
+
# usual, no values are assigned if key is not set. Returns config.
|
31
|
+
def assign(config, values)
|
32
32
|
if key
|
33
33
|
nest_config = nest(config)
|
34
|
+
|
35
|
+
unless assigned
|
36
|
+
nest_config.delete(key)
|
37
|
+
end
|
38
|
+
|
34
39
|
array = (nest_config[key] ||= [])
|
35
40
|
array.concat(values)
|
36
41
|
end
|
37
42
|
|
43
|
+
@assigned = true
|
38
44
|
config
|
39
45
|
end
|
40
46
|
|
data/lib/config_parser/option.rb
CHANGED
data/lib/config_parser.rb
CHANGED
@@ -17,16 +17,16 @@ class ConfigParser
|
|
17
17
|
# '--long' to the Option that handles them.
|
18
18
|
attr_reader :options
|
19
19
|
|
20
|
-
# The hash receiving configs.
|
20
|
+
# The hash receiving parsed configs.
|
21
21
|
attr_accessor :config
|
22
22
|
|
23
|
-
# The argument to stop processing options
|
23
|
+
# The argument to stop processing options.
|
24
24
|
attr_accessor :option_break
|
25
25
|
|
26
|
-
# Set to true to preserve the option break
|
26
|
+
# Set to true to preserve the option break.
|
27
27
|
attr_accessor :preserve_option_break
|
28
28
|
|
29
|
-
# Set to true to assign config defaults on parse
|
29
|
+
# Set to true to assign config defaults on parse.
|
30
30
|
attr_accessor :assign_defaults
|
31
31
|
|
32
32
|
# Initializes a new ConfigParser and passes it to the block, if given.
|
@@ -130,7 +130,8 @@ class ConfigParser
|
|
130
130
|
|
131
131
|
# Constructs an Option using args and registers it with self. The args may
|
132
132
|
# contain (in any order) a short switch, a long switch, and a description
|
133
|
-
# string. A
|
133
|
+
# string. A callback may be provided as a block to process values for the
|
134
|
+
# option.
|
134
135
|
#
|
135
136
|
# The option type (flag, switch, list, or option) is guessed from the args,
|
136
137
|
# and affects what is passed to the block.
|
@@ -147,10 +148,10 @@ class ConfigParser
|
|
147
148
|
# psr.on('-o ARG_NAME') do |arg|
|
148
149
|
# # ...
|
149
150
|
# end
|
150
|
-
#
|
151
|
+
#
|
151
152
|
# # use an argname with commas to make a list,
|
152
|
-
# #
|
153
|
-
# psr.on('--list A,B,C') do |
|
153
|
+
# # each arg is passed to the block separately
|
154
|
+
# psr.on('--list A,B,C') do |arg|
|
154
155
|
# # ...
|
155
156
|
# end
|
156
157
|
#
|
@@ -167,14 +168,18 @@ class ConfigParser
|
|
167
168
|
# end
|
168
169
|
#
|
169
170
|
# If this is too ambiguous (and at times it is), provide a trailing hash
|
170
|
-
# defining all or part of the option
|
171
|
+
# defining all or part of the option. Note any object that responds to call
|
172
|
+
# may be set as the callback:
|
173
|
+
#
|
174
|
+
# psr.on('-k', 'description',
|
175
|
+
# :long => '--key',
|
176
|
+
# :option_type => :list,
|
177
|
+
# :callback => lambda {|args| ... }
|
178
|
+
# )
|
171
179
|
#
|
172
|
-
# psr.on('-k', 'description', :long => '--key', :option_type => :list) do |args|
|
173
|
-
# # ...
|
174
|
-
# end
|
175
|
-
#
|
176
180
|
# The trailing hash wins if there is any overlap in the parsed attributes
|
177
|
-
# and those provided by the hash.
|
181
|
+
# and those provided by the hash. The block wins if both a block and a
|
182
|
+
# callback are given.
|
178
183
|
def on(*args, &block)
|
179
184
|
register new_option(args, &block)
|
180
185
|
end
|
@@ -229,10 +234,9 @@ class ConfigParser
|
|
229
234
|
def parse!(argv=ARGV)
|
230
235
|
argv = Shellwords.shellwords(argv) if argv.kind_of?(String)
|
231
236
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
option.assign(config)
|
237
|
+
registry.each do |option|
|
238
|
+
if assign_defaults && option.respond_to?(:assign_default)
|
239
|
+
option.assign_default(config)
|
236
240
|
end
|
237
241
|
end
|
238
242
|
|
@@ -280,6 +284,22 @@ class ConfigParser
|
|
280
284
|
argv
|
281
285
|
end
|
282
286
|
|
287
|
+
# Resets each option and clears the config (if specified). Returns self.
|
288
|
+
def reset(options={})
|
289
|
+
options = {
|
290
|
+
:clear => true
|
291
|
+
}.merge(options)
|
292
|
+
|
293
|
+
registry.each do |option|
|
294
|
+
if option.respond_to?(:reset)
|
295
|
+
option.reset
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
config.clear if options[:clear]
|
300
|
+
self
|
301
|
+
end
|
302
|
+
|
283
303
|
# Converts the options and separators in self into a help string suitable
|
284
304
|
# for display on the command line.
|
285
305
|
def to_s
|
@@ -308,6 +328,7 @@ class ConfigParser
|
|
308
328
|
def new_option(argv, &block) # :nodoc:
|
309
329
|
attrs = argv.last.kind_of?(Hash) ? argv.pop : {}
|
310
330
|
attrs = parse_attrs(argv).merge(attrs)
|
311
|
-
|
331
|
+
attrs[:callback] = block if block
|
332
|
+
option_class(attrs).new(attrs)
|
312
333
|
end
|
313
334
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 3
|
10
|
+
version: 0.3.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Simon Chiang
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-24 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -33,6 +33,21 @@ dependencies:
|
|
33
33
|
version: "1.0"
|
34
34
|
type: :runtime
|
35
35
|
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 15
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
version: "1.0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
36
51
|
description:
|
37
52
|
email: simon.a.chiang@gmail.com
|
38
53
|
executables: []
|