class HammerCLI::Options::OptionDefinition

Attributes

context_target[RW]
deprecated_switches[RW]
family[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 28
def initialize(switches, type, description, options = {})
  @value_formatter = options[:format] || HammerCLI::Options::Normalizers::Default.new
  @context_target = options[:context_target]
  @deprecated_switches = options[:deprecated]
  @family = options[:family]
  # We expect a value from API param docs, but in case it's not there, we want to show it in help by default
  @show = options.fetch(:show, true)
  super
end

Public Instance Methods

child?() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 152
def child?
  return unless @family

  @family.children.include?(self)
end
complete(value) click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 38
def complete(value)
  value_formatter.complete(value)
end
completion_type(formatter = nil) click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 145
def completion_type(formatter = nil)
  return { type: :flag } if @type == :flag

  formatter ||= value_formatter
  formatter.completion_type
end
default_conversion_block() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 115
def default_conversion_block
  if flag?
    Clamp.method(:truthy?)
  else
    self.method(:format_value)
  end
end
default_value() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 137
def default_value
  if defined?(@default_value)
    value_formatter.format(@default_value)
  elsif multivalued?
    []
  end
end
deprecation_message(switch) click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 69
def deprecation_message(switch)
  if deprecated_switches.class <= String && switches.include?(switch)
    deprecated_switches
  elsif deprecated_switches.class <= Hash && deprecated_switches.keys.include?(switch)
    deprecated_switches[switch]
  end
end
description() click to toggle source
Calls superclass method
# File lib/hammer_cli/options/option_definition.rb, line 77
def description
  if deprecated_switches.class <= String
    format_deprecation_msg(super, _('Deprecated: %{deprecated_msg}') % { deprecated_msg: deprecated_switches })
  elsif deprecated_switches.class <= Hash
    full_msg = deprecated_switches.map do |flag, msg|
      _('%{flag} is deprecated: %{deprecated_msg}') % { flag: flag, deprecated_msg: msg }
    end.join(', ')
    format_deprecation_msg(super, full_msg)
  else
    super
  end
end
extract_value(switch, arguments) click to toggle source
Calls superclass method
# File lib/hammer_cli/options/option_definition.rb, line 59
def extract_value(switch, arguments)
  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, arguments)
end
format_description() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 90
def format_description
  value_formatter.description
end
format_value(value) click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 123
def format_value(value)
  if value == nil_subst
    HammerCLI::NilValue
  else
    value_formatter.format(value)
  end
end
help_lhs() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 42
def help_lhs
  lhs = switches.join(', ')
  lhs += " #{completion_type[:type]}".upcase unless flag?
  lhs
end
help_rhs() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 48
def help_rhs
  lines = [
    description.strip,
    format_description.strip,
    value_description.strip
  ]

  rhs = lines.reject(&:empty?).join("\n")
  rhs.empty? ? " " : rhs
end
nil_subst() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 131
def nil_subst
  nil_subst = ENV['HAMMER_NIL'] || HammerCLI::Options::NIL_SUBST
  raise _('Environment variable HAMMER_NIL can not be empty.') if nil_subst.empty?
  nil_subst
end
show?() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 158
def show?
  @show
end
value_description() click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 94
def value_description
  default_sources = [
    ("$#{@environment_variable}" if defined?(@environment_variable)),
    (@default_value.inspect if defined?(@default_value))
  ].compact

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

Private Instance Methods

format_deprecation_msg(option_desc, deprecation_msg) click to toggle source
# File lib/hammer_cli/options/option_definition.rb, line 164
def format_deprecation_msg(option_desc, deprecation_msg)
  "#{option_desc} (#{deprecation_msg})"
end
infer_attribute_name() click to toggle source
Calls superclass method
# File lib/hammer_cli/options/option_definition.rb, line 168
def infer_attribute_name
  HammerCLI.option_accessor_name(super)
end