class HammerCLI::Options::OptionDefinition

Attributes

context_target[RW]
deprecated_switches[RW]
value_formatter[RW]

Public Class Methods

new(switches, type, description, options = {}) click to toggle source
Calls superclass method
# File lib/hammer_cli/options/option_definition.rb, line 25
def initialize(switches, type, description, options = {})
  self.value_formatter = options.delete(:format)
  self.context_target = options.delete(:context_target)
  self.deprecated_switches = options.delete(:deprecated)
  super
end

Public Instance Methods

complete(value) click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 32
def complete(value)
  if value_formatter.nil?
    []
  else
    value_formatter.complete(value)
  end
end
default_conversion_block() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 85
def default_conversion_block
  if !value_formatter.nil?
    value_formatter.method(:format)
  elsif flag?
    Clamp.method(:truthy?)
  end
end
default_value() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 93
def default_value
  if defined?(@default_value)
    if value_formatter
      value_formatter.format(@default_value)
    else
      @default_value
    end
  elsif multivalued?
    []
  end
end
format_description() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 65
def format_description
  if value_formatter.nil?
    ""
  else
    value_formatter.description
  end
end
handles?(switch) click to toggle source
Calls superclass method
# File lib/hammer_cli/options/option_definition.rb, line 55
def handles?(switch)
  message = _("Warning: Option %{option} is deprecated. %{message}")
  if deprecated_switches.class <= String && switches.include?(switch)
    warn(message % { :option => switch, :message => deprecated_switches })
  elsif deprecated_switches.class <= Hash && deprecated_switches.keys.include?(switch)
    warn(message % { :option => switch, :message => deprecated_switches[switch] })
  end
  super(switch)
end
help_lhs() click to toggle source
Calls superclass method
# File lib/hammer_cli/options/option_definition.rb, line 40
def help_lhs
  super
end
help_rhs() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 44
def help_rhs
  lines = [
    description.strip,
    format_description.strip,
    value_description.strip
  ]

  rhs = lines.reject(&:empty?).join("\n")
  rhs.empty? ? " " : rhs
end
value_description() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 73
def value_description
  default_sources = [
    ("$#{@environment_variable}" if defined?(@environment_variable)),
    (@default_value.inspect if defined?(@default_value))
  ].compact

  str = ""
  str += _("Can be specified multiple times. ") if multivalued?
  str += _("Default: ") + default_sources.join(_(", or ")) unless default_sources.empty?
  str
end

Private Instance Methods

infer_attribute_name() click to toggle source
Calls superclass method
# File lib/hammer_cli/options/option_definition.rb, line 107
def infer_attribute_name
  HammerCLI.option_accessor_name(super)
end