class Clamp::Option::Definition

Represents an option of a Clamp::Command class.

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 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

Public Instance Methods

default_conversion_block() click to toggle source
# File lib/clamp/option/definition.rb, line 64
def default_conversion_block
  Clamp.method(:truthy?) if flag?
end
extract_value(switch, arguments) click to toggle source
# 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?() click to toggle source
# File lib/clamp/option/definition.rb, line 38
def flag?
  @type == :flag
end
flag_set?(switch) click to toggle source
# File lib/clamp/option/definition.rb, line 42
def flag_set?(switch)
  !(switch =~ /^--no-(.*)/ && switches.member?("--[no-]#{Regexp.last_match(1)}"))
end
handles?(switch) click to toggle source
# File lib/clamp/option/definition.rb, line 34
def handles?(switch)
  recognised_switches.member?(switch)
end
help_lhs() click to toggle source
# File lib/clamp/option/definition.rb, line 68
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 30
def long_switch
  switches.find { |switch| switch =~ /^--/ }
end
read_method() click to toggle source
# File lib/clamp/option/definition.rb, line 46
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 86
def infer_attribute_name
  raise Clamp::DeclarationError, "You must specify either a long-switch or an :attribute_value" unless long_switch

  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 76
def recognised_switches
  switches.map do |switch|
    if switch =~ /^--\[no-\](.*)/
      ["--#{Regexp.last_match(1)}", "--no-#{Regexp.last_match(1)}"]
    else
      switch
    end
  end.flatten
end
required_indicator() click to toggle source
# File lib/clamp/option/definition.rb, line 94
def required_indicator
  Clamp.message(:required) if required?
end