Class: HammerCLI::Options::OptionDefinition

Inherits:
Clamp::Option::Definition
  • Object
show all
Defined in:
lib/hammer_cli/options/option_definition.rb

Direct Known Subclasses

Apipie::OptionDefinition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OptionDefinition.



24
25
26
27
28
# File 'lib/hammer_cli/options/option_definition.rb', line 24

def initialize(switches, type, description, options = {})
  self.value_formatter = options.delete(:format)
  self.context_target = options.delete(:context_target)
  super
end

Instance Attribute Details

#context_targetObject

Returns the value of attribute context_target.



22
23
24
# File 'lib/hammer_cli/options/option_definition.rb', line 22

def context_target
  @context_target
end

#value_formatterObject

Returns the value of attribute value_formatter.



21
22
23
# File 'lib/hammer_cli/options/option_definition.rb', line 21

def value_formatter
  @value_formatter
end

Instance Method Details

#complete(value) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/hammer_cli/options/option_definition.rb', line 30

def complete(value)
  if value_formatter.nil?
    []
  else
    value_formatter.complete(value)
  end
end

#default_conversion_blockObject



73
74
75
76
77
78
79
# File 'lib/hammer_cli/options/option_definition.rb', line 73

def default_conversion_block
  if !value_formatter.nil?
    value_formatter.method(:format)
  elsif flag?
    Clamp.method(:truthy?)
  end
end

#default_valueObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hammer_cli/options/option_definition.rb', line 81

def default_value
  if defined?(@default_value)
    if value_formatter
      value_formatter.format(@default_value)
    else
      @default_value
    end
  elsif multivalued?
    []
  end
end

#format_descriptionObject



53
54
55
56
57
58
59
# File 'lib/hammer_cli/options/option_definition.rb', line 53

def format_description
  if value_formatter.nil?
    ""
  else
    value_formatter.description
  end
end

#help_lhsObject



38
39
40
# File 'lib/hammer_cli/options/option_definition.rb', line 38

def help_lhs
  super
end

#help_rhsObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/hammer_cli/options/option_definition.rb', line 42

def help_rhs
  lines = [
    description.strip,
    format_description.strip,
    value_description.strip
  ]

  rhs = lines.reject(&:empty?).join("\n")
  rhs.empty? ? " " : rhs
end

#value_descriptionObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hammer_cli/options/option_definition.rb', line 61

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