Class: OptionParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pObject (readonly)

Returns the value of attribute p.



5
6
7
# File 'lib/optparse_ex.rb', line 5

def p
  @p
end

Instance Method Details

#_onObject



6
# File 'lib/optparse_ex.rb', line 6

alias :_on :on

#on(attr, *args, default: nil, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/optparse_ex.rb', line 8

def on(attr, *args, default: nil, &block)
  if attr.is_a?(Symbol)
    self.class.class_eval do
      unless method_defined?(attr)
        attr_accessor attr 
      else
        raise "Method #{attr} is already defined in OptionParser class"
      end
    end

    self.send("#{attr}=", default) if default

    unless args.first.to_s =~ /^\-/
      self.send("#{attr}=", args.shift)
    end

    _on(*args) do |i|
      self.send("#{attr}=", i)
      block.call(i) if block
    end
  else
    args.unshift attr
    _on(*args, block)
  end
end