Class: Opts::ArgumentParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ArgumentParser.



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

def initialize(app=nil, &block)
  @app = app
  @args = []
  
  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/argument_parser.rb', line 3

def app
  @app
end

Instance Method Details

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



16
17
18
19
20
# File 'lib/opts/argument_parser.rb', line 16

def arg(name, options={})
  options = { :type => :string, :required => true, :min => 1, :max => 1 }.merge(options)
  options[:max] = nil if options.delete(:splat)
  @args << [name, options]
end

#call(env, args) ⇒ Object



22
23
24
25
26
# File 'lib/opts/argument_parser.rb', line 22

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