Class: Clamp::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/clamp/option.rb,
lib/clamp/option/parsing.rb,
lib/clamp/option/declaration.rb

Defined Under Namespace

Modules: Declaration, Parsing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(switches, type, description, options = {}) ⇒ Option

Returns a new instance of Option.



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

def initialize(switches, type, description, options = {})
  @switches = Array(switches)
  @type = type
  @description = description
  if options.has_key?(:attribute_name)
    @attribute_name = options[:attribute_name].to_s 
  end
  if options.has_key?(:default)
    @default_value = options[:default]
  end
end

Instance Attribute Details

#default_valueObject (readonly)

Returns the value of attribute default_value.



17
18
19
# File 'lib/clamp/option.rb', line 17

def default_value
  @default_value
end

#descriptionObject (readonly)

Returns the value of attribute description.



17
18
19
# File 'lib/clamp/option.rb', line 17

def description
  @description
end

#switchesObject (readonly)

Returns the value of attribute switches.



17
18
19
# File 'lib/clamp/option.rb', line 17

def switches
  @switches
end

#typeObject (readonly)

Returns the value of attribute type.



17
18
19
# File 'lib/clamp/option.rb', line 17

def type
  @type
end

Instance Method Details

#attribute_nameObject



19
20
21
# File 'lib/clamp/option.rb', line 19

def attribute_name
  @attribute_name ||= long_switch.sub(/^--(\[no-\])?/, '').tr('-', '_')
end

#extract_value(switch, arguments) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/clamp/option.rb', line 39

def extract_value(switch, arguments)
  if flag?
    flag_value(switch)
  else
    arguments.shift
  end
end

#flag?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/clamp/option.rb', line 31

def flag?
  @type == :flag
end

#flag_value(switch) ⇒ Object



35
36
37
# File 'lib/clamp/option.rb', line 35

def flag_value(switch)
  !(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{$1}"))
end

#handles?(switch) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/clamp/option.rb', line 27

def handles?(switch)
  recognised_switches.member?(switch)
end

#helpObject



47
48
49
50
51
52
53
54
55
# File 'lib/clamp/option.rb', line 47

def help
  lhs = switches.join(", ")
  lhs += " " + type unless flag?
  rhs = description
  if defined?(@default_value)
    rhs += " (default: #{@default_value.inspect})"
  end
  [lhs, rhs]
end

#long_switchObject



23
24
25
# File 'lib/clamp/option.rb', line 23

def long_switch
  switches.find { |switch| switch =~ /^--/ }
end