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.



28
29
30
31
32
33
34
# 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]
  super
end

Instance Attribute Details

#context_targetObject

Returns the value of attribute context_target.



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

def context_target
  @context_target
end

#deprecated_switchesObject

Returns the value of attribute deprecated_switches.



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

def deprecated_switches
  @deprecated_switches
end

#familyObject

Returns the value of attribute family.



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

def family
  @family
end

#value_formatterObject

Returns the value of attribute value_formatter.



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

def value_formatter
  @value_formatter
end

Instance Method Details

#child?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
# File 'lib/hammer_cli/options/option_definition.rb', line 150

def child?
  return unless @family

  @family.children.include?(self)
end

#complete(value) ⇒ Object



36
37
38
# File 'lib/hammer_cli/options/option_definition.rb', line 36

def complete(value)
  value_formatter.complete(value)
end

#completion_type(formatter = nil) ⇒ Object



143
144
145
146
147
148
# File 'lib/hammer_cli/options/option_definition.rb', line 143

def completion_type(formatter = nil)
  return { type: :flag } if @type == :flag

  formatter ||= value_formatter
  formatter.completion_type
end

#default_conversion_blockObject



113
114
115
116
117
118
119
# File 'lib/hammer_cli/options/option_definition.rb', line 113

def default_conversion_block
  if flag?
    Clamp.method(:truthy?)
  else
    self.method(:format_value)
  end
end

#default_valueObject



135
136
137
138
139
140
141
# File 'lib/hammer_cli/options/option_definition.rb', line 135

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

#deprecation_message(switch) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/hammer_cli/options/option_definition.rb', line 67

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

#descriptionObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hammer_cli/options/option_definition.rb', line 75

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) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/hammer_cli/options/option_definition.rb', line 57

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_descriptionObject



88
89
90
# File 'lib/hammer_cli/options/option_definition.rb', line 88

def format_description
  value_formatter.description
end

#format_value(value) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/hammer_cli/options/option_definition.rb', line 121

def format_value(value)
  if value == nil_subst
    HammerCLI::NilValue
  else
    value_formatter.format(value)
  end
end

#help_lhsObject



40
41
42
43
44
# File 'lib/hammer_cli/options/option_definition.rb', line 40

def help_lhs
  lhs = switches.join(', ')
  lhs += " #{completion_type[:type]}".upcase unless flag?
  lhs
end

#help_rhsObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/hammer_cli/options/option_definition.rb', line 46

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

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

#nil_substObject



129
130
131
132
133
# File 'lib/hammer_cli/options/option_definition.rb', line 129

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

#value_descriptionObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hammer_cli/options/option_definition.rb', line 92

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