Class: Opts::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/opts/option_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, &block) ⇒ OptionParser

Returns a new instance of OptionParser.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/opts/option_parser.rb', line 5

def initialize(app=nil, &block)
  @options = {}
  @short_names = {}
  @app = app
  
  if block and block.arity == -1
    instance_eval(&block)
  elsif block_given?
    yield(self)
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



3
4
5
# File 'lib/opts/option_parser.rb', line 3

def app
  @app
end

Instance Method Details

#call(env, args) ⇒ Object



17
18
19
20
21
# File 'lib/opts/option_parser.rb', line 17

def call(env, args)
  new_env, new_args = env.dup, args.dup
  parse(new_env, new_args)
  @app.call(new_env, new_args)
end

#opt(name, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/opts/option_parser.rb', line 24

def opt(name, options={})
  if short = options[:short]
    @short_names[short.to_s] = name.to_s
  end
  
  @options[name.to_s] = {
    :type => :boolean
  }.merge(options)
end