class Clamp::Option::Definition

Attributes

switches[R]
type[R]

Public Class Methods

new(switches, type, description, options = {}) click to toggle source
Calls superclass method Clamp::Attribute::Definition.new
# 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

Public Instance Methods

default_conversion_block() click to toggle source
# File lib/clamp/option/definition.rb, line 61
def default_conversion_block
  if flag?
    Clamp.method(:truthy?)
  end
end
extract_value(switch, arguments) click to toggle source
# File lib/clamp/option/definition.rb, line 53
def extract_value(switch, arguments)
  if flag?
    flag_value(switch)
  else
    arguments.shift
  end
end
flag?() click to toggle source
# File lib/clamp/option/definition.rb, line 37
def flag?
  @type == :flag
end
flag_value(switch) click to toggle source
# File lib/clamp/option/definition.rb, line 41
def flag_value(switch)
  !(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{$1}"))
end
handles?(switch) click to toggle source
# File lib/clamp/option/definition.rb, line 33
def handles?(switch)
  recognised_switches.member?(switch)
end
help_lhs() click to toggle source
# File lib/clamp/option/definition.rb, line 67
def help_lhs
  lhs = switches.join(", ")
  lhs += " " + type unless flag?
  lhs
end
long_switch() click to toggle source
# File lib/clamp/option/definition.rb, line 29
def long_switch
  switches.find { |switch| switch =~ /^--/ }
end
read_method() click to toggle source
# File lib/clamp/option/definition.rb, line 45
def read_method
  if flag?
    super + "?"
  else
    super
  end
end

Private Instance Methods

infer_attribute_name() click to toggle source
# File lib/clamp/option/definition.rb, line 85
def infer_attribute_name
  inferred_name = long_switch.sub(/^--(\[no-\])?/, '').tr('-', '_')
  inferred_name += "_list" if multivalued?
  inferred_name
end
recognised_switches() click to toggle source
# File lib/clamp/option/definition.rb, line 75
def recognised_switches
  switches.map do |switch|
    if switch =~ /^--\[no-\](.*)/
      ["--#{$1}", "--no-#{$1}"]
    else
      switch
    end
  end.flatten
end