optopus 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +1 -1
  2. data/lib/optopus.rb +23 -11
  3. metadata +6 -6
data/README CHANGED
@@ -35,7 +35,7 @@ gem install optopus
35
35
  option :protocol, '-P', '--protocol PROTO', :type => [:http, :ftp]
36
36
 
37
37
  desc 'access timestamp'
38
- option :timestamp, '-T', '--timestamp TIME', :type => Time
38
+ option :timestamp, '-T', '--timestamp TIME', :type => Time, :required => true
39
39
 
40
40
  # read yaml config file and overwrite options
41
41
  config_file '-c', '--config-file FILE'
data/lib/optopus.rb CHANGED
@@ -5,6 +5,12 @@ require 'optparse/time'
5
5
  require 'optparse/uri'
6
6
  require 'yaml'
7
7
 
8
+ class OptionParser
9
+ class NotGiven < ParseError
10
+ const_set(:Reason, 'required option was not given'.freeze)
11
+ end
12
+ end
13
+
8
14
  module Optopus
9
15
  class DefinerContext
10
16
  def self.evaluate(opts, &block)
@@ -54,10 +60,7 @@ module Optopus
54
60
 
55
61
  def initialize(value, &block)
56
62
  @args = value ? [value] : []
57
-
58
- if block
59
- (class<<self; self; end).send(:define_method, :evaluate, &block)
60
- end
63
+ (class<<self; self; end).send(:define_method, :evaluate, &block)
61
64
  end
62
65
 
63
66
  def parse_error(reason)
@@ -106,13 +109,13 @@ module Optopus
106
109
  def release=(v) ; @parser.release = v ; end
107
110
 
108
111
  def add(name, args, desc, block)
109
- args, defval = fix_args(args, desc)
110
- @opts_args << [name.to_sym, args, defval, block]
112
+ args, defval, required = fix_args(args, desc)
113
+ @opts_args << [name.to_sym, args, defval, block, required]
111
114
  end
112
115
 
113
116
  def add_file(args, desc)
114
117
  raise 'two or more config_file is defined' if @file_args
115
- args, defval = fix_args(args, desc)
118
+ args, defval, required = fix_args(args, desc)
116
119
  @file_args = args
117
120
  end
118
121
 
@@ -129,8 +132,8 @@ module Optopus
129
132
  has_arg_v = false
130
133
  has_arg_h = false
131
134
 
132
- @opts_args.each do |name, args, defval, block|
133
- options[name] = defval
135
+ @opts_args.each do |name, args, defval, block, required|
136
+ options[name] = defval unless defval.nil?
134
137
  has_arg_v = (args.first == '-v')
135
138
  has_arg_h = (args.first == '-h')
136
139
 
@@ -145,7 +148,7 @@ module Optopus
145
148
  @parser.on(*@file_args) do |v|
146
149
  config = YAML.load_file(v)
147
150
 
148
- @opts_args.each do |name, args, defval, block|
151
+ @opts_args.each do |name, args, defval, block, required|
149
152
  if args[1].kind_of?(String) and args[1] =~ /-+([^\s=]+)/
150
153
  key = $1
151
154
  else
@@ -187,6 +190,13 @@ module Optopus
187
190
  end
188
191
 
189
192
  @parser.parse!
193
+
194
+ @opts_args.each do |name, args, defval, block, required|
195
+ if required and not options.has_key?(name)
196
+ raise OptionParser::NotGiven, args.first
197
+ end
198
+ end
199
+
190
200
  CheckerContext.evaluate([], options, &@on_after) if @on_after
191
201
 
192
202
  return options
@@ -201,16 +211,18 @@ module Optopus
201
211
  private
202
212
  def fix_args(args, desc)
203
213
  defval = nil
214
+ required = false
204
215
 
205
216
  if args.last.kind_of?(Hash)
206
217
  hash = args.pop
207
218
  args = (args.slice(0, 2) + [hash[:type], hash[:desc] || desc]).select {|i| i }
208
219
  defval = hash[:default]
220
+ required = hash[:required]
209
221
  elsif desc
210
222
  args = args + [desc]
211
223
  end
212
224
 
213
- return [args, defval]
225
+ return [args, defval, required]
214
226
  end
215
227
  end # Options
216
228
  end # Optopus
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: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - winebarrel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-31 00:00:00 Z
18
+ date: 2011-11-01 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description:
@@ -58,9 +58,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  requirements: []
59
59
 
60
60
  rubyforge_project:
61
- rubygems_version: 1.7.2
61
+ rubygems_version: 1.8.11
62
62
  signing_key:
63
63
  specification_version: 3
64
- summary: Enhanced option parser.
64
+ summary: optopus is an easy-to-use option purser.
65
65
  test_files: []
66
66