Class: Ergane::SwitchDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, short: nil, argument: nil, kind: nil, description: nil, default: nil, &block) ⇒ SwitchDefinition

Returns a new instance of SwitchDefinition.



29
30
31
32
# File 'lib/ergane/switch_definition.rb', line 29

def initialize(label, short: nil, argument: nil, kind: nil, description: nil, default: nil, &block)
  @label, @short, @argument, @kind, @description, @default = label.to_sym, short, argument, kind, description, default
  @run_block = block if block_given?
end

Instance Attribute Details

#argumentObject (readonly)

Returns the value of attribute argument.



24
25
26
# File 'lib/ergane/switch_definition.rb', line 24

def argument
  @argument
end

#defaultObject (readonly)

Returns the value of attribute default.



26
27
28
# File 'lib/ergane/switch_definition.rb', line 26

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



25
26
27
# File 'lib/ergane/switch_definition.rb', line 25

def description
  @description
end

#kindObject (readonly)

Returns the value of attribute kind.



23
24
25
# File 'lib/ergane/switch_definition.rb', line 23

def kind
  @kind
end

#labelObject (readonly)

Returns the value of attribute label.



21
22
23
# File 'lib/ergane/switch_definition.rb', line 21

def label
  @label
end

#run_blockObject (readonly)

Returns the value of attribute run_block.



27
28
29
# File 'lib/ergane/switch_definition.rb', line 27

def run_block
  @run_block
end

#shortObject (readonly)

Returns the value of attribute short.



22
23
24
# File 'lib/ergane/switch_definition.rb', line 22

def short
  @short
end

Instance Method Details

#attach(option_parser, command, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ergane/switch_definition.rb', line 34

def attach(option_parser, command, &block)
  flag_arg = argument ? "=#{argument}" : ""
  args = []
  args << "-#{short}#{flag_arg}" if short
  args << "--#{label.to_s.gsub(/_/, '-')}#{flag_arg}"
  args << kind if kind
  args << description if description

  option_parser.on(*args, Proc.new { |value|
    command.instance_exec(value, &block) if block_given?
    command.instance_exec(value, &run_block) if run_block
  })
end