Class: Clamp::Option::Definition

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

Overview

Represents an option of a Clamp::Command class.

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, #hidden?, #ivar_name, #multivalued?, #of, #option_missing_message, #required?, #write_method

Constructor Details

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

Returns a new instance of Definition.

Raises:

  • (ArgumentError)


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

def initialize(switches, type, description, options = {})
  @switches = Array(switches)
  @type = type
  @description = description
  super(options)
  @multivalued = options[:multivalued]

  return unless options.key?(:required)

  @required = options[:required]
  # Do some light validation for conflicting settings.
  raise ArgumentError, "Specifying a :default value with :required doesn't make sense" if options.key?(:default)
  raise ArgumentError, "A required flag (boolean) doesn't make sense." if type == :flag
end

Instance Attribute Details

#switchesObject (readonly)

Returns the value of attribute switches.



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

def switches
  @switches
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#default_conversion_blockObject



64
65
66
# File 'lib/clamp/option/definition.rb', line 64

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

#extract_value(switch, arguments) ⇒ Object



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

def extract_value(switch, arguments)
  if flag?
    flag_set?(switch)
  else
    raise ArgumentError, Clamp.message(:no_value_provided) if arguments.empty?

    arguments.shift
  end
end

#flag?Boolean

Returns:

  • (Boolean)


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

def flag?
  @type == :flag
end

#flag_set?(switch) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#handles?(switch) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#help_lhsObject



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

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

#long_switchObject



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

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

#read_methodObject



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

def read_method
  if flag?
    "#{super}?"
  else
    super
  end
end