Class: Clamp::Option::Definition

Inherits:
Attribute::Definition show all
Defined in:
lib/clamp/option/definition.rb

Instance Attribute Summary collapse

Attributes inherited from Attribute::Definition

#description, #environment_variable

Instance Method Summary collapse

Methods inherited from Attribute::Definition

#append_method, #attribute_name, #default_method, #default_value, #help, #help_rhs, #ivar_name, #multivalued?, #of, #required?, #write_method

Constructor Details

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

Returns a new instance of Definition.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/clamp/option/definition.rb', line 9

def initialize(switches, type, description, options = {})
  @switches = Array(switches)
  @type = type
  @description = description
  super(options)
  @multivalued = options[:multivalued]
  if options.has_key?(:required)
    @required = options[:required]
    # Do some light validation for conflicting settings.
    if options.has_key?(:default)
      raise ArgumentError, "Specifying a :default value also :required doesn't make sense"
    end
    if type == :flag
      raise ArgumentError, "A required flag (boolean) doesn't make sense."
    end
  end
end

Instance Attribute Details

#switchesObject (readonly)

Returns the value of attribute switches.



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

def switches
  @switches
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#default_conversion_blockObject



61
62
63
64
65
# File 'lib/clamp/option/definition.rb', line 61

def default_conversion_block
  if flag?
    Clamp.method(:truthy?)
  end
end

#extract_value(switch, arguments) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/clamp/option/definition.rb', line 53

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

#flag?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/clamp/option/definition.rb', line 37

def flag?
  @type == :flag
end

#flag_value(switch) ⇒ Object



41
42
43
# File 'lib/clamp/option/definition.rb', line 41

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

#handles?(switch) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/clamp/option/definition.rb', line 33

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

#help_lhsObject



67
68
69
70
71
# File 'lib/clamp/option/definition.rb', line 67

def help_lhs
  lhs = switches.join(", ")
  lhs += " " + type unless flag?
  lhs
end

#long_switchObject



29
30
31
# File 'lib/clamp/option/definition.rb', line 29

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

#read_methodObject



45
46
47
48
49
50
51
# File 'lib/clamp/option/definition.rb', line 45

def read_method
  if flag?
    super + "?"
  else
    super
  end
end