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
- #completion_type(formatter = nil) ⇒ Object
- #default_conversion_block ⇒ Object
- #default_value ⇒ Object
- #deprecation_message(switch) ⇒ Object
- #description ⇒ Object
- #format_description ⇒ Object
- #format_value(value) ⇒ Object
- #handles?(switch) ⇒ Boolean
- #help_lhs ⇒ Object
- #help_rhs ⇒ Object
-
#initialize(switches, type, description, options = {}) ⇒ OptionDefinition
constructor
A new instance of OptionDefinition.
- #nil_subst ⇒ Object
- #value_description ⇒ Object
Constructor Details
#initialize(switches, type, description, options = {}) ⇒ OptionDefinition
Returns a new instance of OptionDefinition.
27 28 29 30 31 32 |
# File 'lib/hammer_cli/options/option_definition.rb', line 27 def initialize(switches, type, description, = {}) @value_formatter = [:format] || HammerCLI::Options::Normalizers::Default.new @context_target = [:context_target] @deprecated_switches = [:deprecated] super end |
Instance Attribute Details
#context_target ⇒ Object
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_switches ⇒ Object
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 |
#value_formatter ⇒ Object
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
#complete(value) ⇒ Object
34 35 36 |
# File 'lib/hammer_cli/options/option_definition.rb', line 34 def complete(value) value_formatter.complete(value) end |
#completion_type(formatter = nil) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/hammer_cli/options/option_definition.rb', line 139 def completion_type(formatter = nil) return { type: :flag } if @type == :flag formatter ||= value_formatter completion_type = case formatter when HammerCLI::Options::Normalizers::Bool, HammerCLI::Options::Normalizers::Enum { type: :enum, values: value_formatter.allowed_values } when HammerCLI::Options::Normalizers::EnumList { type: :multienum, values: value_formatter.allowed_values } when HammerCLI::Options::Normalizers::ListNested { type: :schema, schema: value_formatter.schema.description(richtext: false) } when HammerCLI::Options::Normalizers::List { type: :list } when HammerCLI::Options::Normalizers::KeyValueList { type: :key_value_list } when HammerCLI::Options::Normalizers::File { type: :file } end completion_type || { type: :value } end |
#default_conversion_block ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/hammer_cli/options/option_definition.rb', line 109 def default_conversion_block if flag? Clamp.method(:truthy?) else self.method(:format_value) end end |
#default_value ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/hammer_cli/options/option_definition.rb', line 131 def default_value if defined?(@default_value) value_formatter.format(@default_value) elsif multivalued? [] end end |
#deprecation_message(switch) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/hammer_cli/options/option_definition.rb', line 63 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
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/hammer_cli/options/option_definition.rb', line 71 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
84 85 86 |
# File 'lib/hammer_cli/options/option_definition.rb', line 84 def format_description value_formatter.description end |
#format_value(value) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/hammer_cli/options/option_definition.rb', line 117 def format_value(value) if value == nil_subst HammerCLI::NilValue else value_formatter.format(value) end end |
#handles?(switch) ⇒ Boolean
53 54 55 56 57 58 59 60 61 |
# File 'lib/hammer_cli/options/option_definition.rb', line 53 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
38 39 40 |
# File 'lib/hammer_cli/options/option_definition.rb', line 38 def help_lhs super end |
#help_rhs ⇒ Object
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 |
#nil_subst ⇒ Object
125 126 127 128 129 |
# File 'lib/hammer_cli/options/option_definition.rb', line 125 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_description ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/hammer_cli/options/option_definition.rb', line 88 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 |