optopus 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/optopus.rb +70 -39
  2. metadata +5 -7
data/lib/optopus.rb CHANGED
@@ -44,6 +44,10 @@ module Optopus
44
44
  @desc = nil
45
45
  end
46
46
 
47
+ def before(&block)
48
+ @opts.add_before(block)
49
+ end
50
+
47
51
  def after(&block)
48
52
  @opts.add_after(block)
49
53
  end
@@ -131,6 +135,10 @@ module Optopus
131
135
  @file_args = args
132
136
  end
133
137
 
138
+ def add_before(block)
139
+ @on_before = block
140
+ end
141
+
134
142
  def add_after(block)
135
143
  @on_after = block
136
144
  end
@@ -145,60 +153,77 @@ module Optopus
145
153
  has_arg_h = false
146
154
  options.instance_eval("def config_file; @__config_file__; end")
147
155
 
148
- if @file_args
149
- @parser.on(*@file_args) do |v|
156
+ file_args_checker = lambda do |v|
157
+ if v.kind_of?(Hash)
158
+ config = v
159
+ else
150
160
  config = YAML.load_file(v)
151
- options.instance_variable_set(:@__config_file__, config)
161
+ @on_before.call(config) if @on_before
162
+ end
152
163
 
153
- @opts_args.each do |name, args, defval, block, required, multiple|
154
- if args[1].kind_of?(String) and args[1] =~ /-+([^\s=]+)/
155
- key = $1
156
- else
157
- key = name.to_s
158
- end
164
+ config.keys.each do |key|
165
+ next unless key.kind_of?(Symbol)
166
+ config[key.to_s.gsub('_', '-')] = config[key]
167
+ end
168
+
169
+ options.instance_variable_set(:@__config_file__, config)
170
+
171
+ @opts_args.each do |name, args, defval, block, required, multiple|
172
+ if args[1].kind_of?(String) and args[1] =~ /-+([^\s=]+)/
173
+ key = $1
174
+ else
175
+ key = name.to_s
176
+ end
159
177
 
160
- values = nil
178
+ value = nil
161
179
 
162
- [key, key.gsub(/[-_]/, '-'), key.gsub(/[-_]/, '_')].each do |k|
163
- if values = config[k]
164
- key = k
165
- break
166
- end
180
+ [key, key.gsub(/[-_]/, '-'), key.gsub(/[-_]/, '_')].each do |k|
181
+ if value = config[k]
182
+ key = k
183
+ break
167
184
  end
185
+ end
168
186
 
169
- next unless values
187
+ next unless value
170
188
 
171
- check = lambda do |value|
172
- value = orig_val = value.to_s
173
- type = args.find {|i| i.kind_of?(Class) }
174
- pat, conv = OptionParser::DefaultList.atype[type]
189
+ value = orig_val = value.to_s
175
190
 
176
- if pat and pat !~ value
177
- raise OptionParser::InvalidArgument.new(v, "(#{key}: #{value})")
178
- end
191
+ if type = args.find {|i| i.kind_of?(Class) }
192
+ pat, conv = OptionParser::DefaultList.atype[type]
179
193
 
180
- value = conv.call(value) if conv
194
+ if pat and pat !~ value
195
+ raise OptionParser::InvalidArgument.new(v, "(#{key}: #{value})")
196
+ end
181
197
 
182
- if value and block
183
- begin
184
- CheckerContext.evaluate(v, value, &block)
185
- rescue OptionParser::ParseError => e
186
- errmsg = "#{e.message}: #{key}=#{orig_val}"
187
- raise OptionParser::ParseError, errmsg
188
- end
189
- end
198
+ value = conv.call(value) if conv
199
+ elsif type = args.find {|i| i.kind_of?(Array) }
200
+ unless type.map {|i| i.to_s }.include?(value.to_s)
201
+ raise OptionParser::InvalidArgument.new(key, value)
202
+ end
190
203
 
191
- return value
192
- end # lambda
204
+ value = value.to_s.to_sym
205
+ end
193
206
 
194
- if values.kind_of?(Array)
195
- options[name] = values.map {|i| check.call(i) }
196
- else
197
- options[name] = check.call(values)
207
+ if value and block
208
+ begin
209
+ CheckerContext.evaluate(v, value, &block)
210
+ rescue OptionParser::ParseError => e
211
+ errmsg = "#{e.message}: #{key}=#{orig_val}"
212
+ raise OptionParser::ParseError, errmsg
198
213
  end
199
214
  end
215
+
216
+ options[name] = value
200
217
  end
201
- end # if @file_args
218
+ end # file_args_checker
219
+
220
+ if @file_args
221
+ @parser.on(*@file_args, &file_args_checker)
222
+ elsif @on_before
223
+ config = {}
224
+ @on_before.call(config)
225
+ file_args_checker.call(config)
226
+ end
202
227
 
203
228
  @opts_args.each do |name, args, defval, block, required, multiple|
204
229
  options[name] = defval unless defval.nil?
@@ -242,6 +267,12 @@ module Optopus
242
267
  end
243
268
  end
244
269
 
270
+ (options.config_file || {}).each do |key, value|
271
+ next if key.kind_of?(Symbol)
272
+ key = key.to_s.gsub('-', '_').to_sym
273
+ options[key] = value unless options.has_key?(key)
274
+ end
275
+
245
276
  CheckerContext.evaluate([], options, &@on_after) if @on_after
246
277
 
247
278
  return options
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optopus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - winebarrel
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-17 00:00:00 +09:00
19
- default_executable:
18
+ date: 2011-12-26 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description:
@@ -30,7 +29,6 @@ extra_rdoc_files: []
30
29
  files:
31
30
  - README
32
31
  - lib/optopus.rb
33
- has_rdoc: true
34
32
  homepage: https://bitbucket.org/winebarrel/optopus
35
33
  licenses: []
36
34
 
@@ -60,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
58
  requirements: []
61
59
 
62
60
  rubyforge_project:
63
- rubygems_version: 1.4.2
61
+ rubygems_version: 1.8.11
64
62
  signing_key:
65
63
  specification_version: 3
66
64
  summary: optopus is an easy-to-use option purser.