Class: HammerCLI::Options::OptionDefinition
- Inherits:
-
Clamp::Option::Definition
- Object
- Clamp::Option::Definition
- HammerCLI::Options::OptionDefinition
- Defined in:
- lib/hammer_cli/options/option_definition.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context_target ⇒ Object
Returns the value of attribute context_target.
-
#deprecated_switches ⇒ Object
Returns the value of attribute deprecated_switches.
-
#value_formatter ⇒ Object
Returns the value of attribute value_formatter.
Instance Method Summary collapse
- #complete(value) ⇒ Object
- #default_conversion_block ⇒ Object
- #default_value ⇒ Object
- #deprecation_message(switch) ⇒ Object
- #description ⇒ Object
- #format_description ⇒ Object
- #handles?(switch) ⇒ Boolean
- #help_lhs ⇒ Object
- #help_rhs ⇒ Object
-
#initialize(switches, type, description, options = {}) ⇒ OptionDefinition
constructor
A new instance of OptionDefinition.
- #value_description ⇒ Object
Constructor Details
#initialize(switches, type, description, options = {}) ⇒ OptionDefinition
Returns a new instance of OptionDefinition.
25 26 27 28 29 30 |
# File 'lib/hammer_cli/options/option_definition.rb', line 25 def initialize(switches, type, description, = {}) self.value_formatter = .delete(:format) self.context_target = .delete(:context_target) self.deprecated_switches = .delete(:deprecated) super end |
Instance Attribute Details
#context_target ⇒ Object
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 |
#deprecated_switches ⇒ Object
Returns the value of attribute deprecated_switches.
23 24 25 |
# File 'lib/hammer_cli/options/option_definition.rb', line 23 def deprecated_switches @deprecated_switches end |
#value_formatter ⇒ Object
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
32 33 34 35 36 37 38 |
# 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 ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/hammer_cli/options/option_definition.rb', line 106 def default_conversion_block if !value_formatter.nil? value_formatter.method(:format) elsif flag? Clamp.method(:truthy?) end end |
#default_value ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/hammer_cli/options/option_definition.rb', line 114 def default_value if defined?(@default_value) if value_formatter value_formatter.format(@default_value) else @default_value end elsif multivalued? [] end end |
#deprecation_message(switch) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/hammer_cli/options/option_definition.rb', line 65 def (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 ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hammer_cli/options/option_definition.rb', line 73 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 |
#format_description ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/hammer_cli/options/option_definition.rb', line 86 def format_description if value_formatter.nil? "" else value_formatter.description end end |
#handles?(switch) ⇒ Boolean
55 56 57 58 59 60 61 62 63 |
# File 'lib/hammer_cli/options/option_definition.rb', line 55 def handles?(switch) = _("Warning: Option %{option} is deprecated. %{message}") if deprecated_switches.class <= String && switches.include?(switch) warn( % { :option => switch, :message => deprecated_switches }) elsif deprecated_switches.class <= Hash && deprecated_switches.keys.include?(switch) warn( % { :option => switch, :message => deprecated_switches[switch] }) end super(switch) end |
#help_lhs ⇒ Object
40 41 42 |
# File 'lib/hammer_cli/options/option_definition.rb', line 40 def help_lhs super end |
#help_rhs ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# 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 ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# 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 = "" str += _("Can be specified multiple times. ") if multivalued? str += _("Default: ") + default_sources.join(_(", or ")) unless default_sources.empty? str end |