Class: Clamp::Option

Inherits:
Attribute 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

Attributes inherited from Attribute

#default_value, #description, #environment_variable

Instance Method Summary collapse

Methods inherited from Attribute

#default_method, #help, #help_rhs, #ivar_name, #write_method

Constructor Details

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

Returns a new instance of Option.



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

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
  if options.has_key?(:env)
    @environment_variable = options[:env]
  end
  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.



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

def switches
  @switches
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#attribute_nameObject



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

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

#extract_value(switch, arguments) ⇒ Object



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

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

#flag?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/clamp/option.rb', line 50

def flag?
  @type == :flag
end

#flag_value(switch) ⇒ Object



54
55
56
# File 'lib/clamp/option.rb', line 54

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

#handles?(switch) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#help_lhsObject



74
75
76
77
78
# File 'lib/clamp/option.rb', line 74

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

#long_switchObject



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

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

#read_methodObject



58
59
60
61
62
63
64
# File 'lib/clamp/option.rb', line 58

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

#required?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/clamp/option.rb', line 46

def required?
  @required
end