Class: QB::Options::Option

Inherits:
Object
  • Object
show all
Includes:
NRSER::Log::Mixin, OptionParserConcern
Defined in:
lib/qb/options/option.rb,
lib/qb/options/option/option_parser_concern.rb

Overview

Definitions

Defined Under Namespace

Modules: OptionParserConcern

Constant Summary collapse

EXAMPLES_KEYS =

Constants

['examples', 'example']

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionParserConcern

#option_parser_add, #option_parser_args, #option_parser_bool_args, #option_parser_default, #option_parser_description, #option_parser_examples, #option_parser_format_multiline, #option_parser_non_bool_args, #option_parser_spacer, #option_parser_type, #option_parser_type_acceptable, #option_parser_value_name

Constructor Details

#initialize(role, meta, include_path) ⇒ Option

Construction



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/qb/options/option.rb', line 92

def initialize role, meta, include_path
  @role = role
  @meta = meta.with_indifferent_access
  @include_path = include_path
  
  @meta_name = meta.fetch 'name'
  
  @cli_name = if @include_path.empty?
    QB::Options.cli_ize_name @meta_name
  else
    QB::Options.cli_ize_name "#{ @include_path.join('-') }-#{ @meta_name }"
  end
  
  @var_name = if self.meta?( :var_name )
    # prefer an explicit, exact variable name if provided
    self.meta( :var_name, type: Types.var_name )
  elsif role.var_prefix
    QB::Options.var_ize_name "#{ role.var_prefix }_#{ meta_name }"
  else
    QB::Options.var_ize_name meta_name
  end
  
  # Will be set when we find it out!
  @value = nil
  
  # Initialize `@type` var
  init_type!
end

Instance Attribute Details

#cli_nameObject (readonly)

the name that this option will be available in the cli as



73
74
75
# File 'lib/qb/options/option.rb', line 73

def cli_name
  @cli_name
end

#include_pathObject (readonly)

array of strings representing how this option was included empty for top-level options



67
68
69
# File 'lib/qb/options/option.rb', line 67

def include_path
  @include_path
end

#meta_nameObject (readonly)

the name of the option in the qb metadata, equal to #meta['name']



70
71
72
# File 'lib/qb/options/option.rb', line 70

def meta_name
  @meta_name
end

#roleObject (readonly)

the role that this option is for



63
64
65
# File 'lib/qb/options/option.rb', line 63

def role
  @role
end

#typeattr_type (readonly)

TODO document type attribute.

Returns:

  • (attr_type)


86
87
88
# File 'lib/qb/options/option.rb', line 86

def type
  @type
end

#valueObject

the value of the option, or nil if we never assign one



79
80
81
# File 'lib/qb/options/option.rb', line 79

def value
  @value
end

#var_nameObject (readonly)

the name that the value will be passed to ansible as



76
77
78
# File 'lib/qb/options/option.rb', line 76

def var_name
  @var_name
end

Instance Method Details

#accept_false?Boolean

Does the option accept false as value?

If it does, and is not a boolean option, we also accept a --no-<name> option format to set the value to false.

This is useful to explicitly tell QB "no, I don't want this", since we treat nil/null as the same as absent, which will cause a default value to be used (if available).

This feature does not apply to #boolean? options themselves, only options that accept other values (though this method will of course return true for #boolean? options, since they do accept false).

Returns:

  • (Boolean)


305
306
307
308
309
310
311
# File 'lib/qb/options/option.rb', line 305

def accept_false?
  return true if meta[:accept_false]
  
  return false if type.is_a?( Class ) && type < NRSER::Props
  
  type.test?( false )
end

#boolean?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/qb/options/option.rb', line 258

def boolean?
  type == t.bool
end

#descriptionString

Description of the option.

Returns:

  • (String)


250
251
252
253
254
255
# File 'lib/qb/options/option.rb', line 250

def description
  meta(
    :description,
    default: "Set the #{ @var_name } role variable"
  ).to_s
end

#examplesArray<String>

get an array of examples for the option. returns [] if no examples are defined.

Returns:

  • (Array<String>)


285
286
287
# File 'lib/qb/options/option.rb', line 285

def examples
  Array meta( *EXAMPLES_KEYS, type: (t.nil | t.str | t.array( t.str )) )
end

#has_examples?Boolean

test if the option has any examples.

Returns:

  • (Boolean)


276
277
278
# File 'lib/qb/options/option.rb', line 276

def has_examples?
  meta? *EXAMPLES_KEYS
end

#meta(*keys, type: t.any, default: nil) ⇒ Object

Instance Methods



203
204
205
206
207
208
209
210
211
# File 'lib/qb/options/option.rb', line 203

def meta *keys, type: t.any, default: nil
  return @meta if keys.empty?
  
  keys.each do |key|
    return type.check!( @meta[key] ) unless @meta[key].nil?
  end
  
  type.check! default
end

#meta?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/qb/options/option.rb', line 214

def meta? *keys
  keys.any? { |key| @meta.key? key }
end

#required?Boolean

Is the option is required in the CLI?

Returns:

  • (Boolean)


232
233
234
# File 'lib/qb/options/option.rb', line 232

def required?
  meta :required, :require, type: t.bool, default: false
end

#save?Boolean

Should we save the option value in ./.qb-options.yml?

Returns:

  • (Boolean)


241
242
243
# File 'lib/qb/options/option.rb', line 241

def save?
  meta :save, type: t.bool, default: true
end

#usageObject



263
264
265
266
267
268
269
# File 'lib/qb/options/option.rb', line 263

def usage
  if boolean?
    "--[no-]#{ cli_name }"
  else
    "--#{ cli_name }=#{ meta_name.upcase }"
  end
end

#value_dataObject



219
220
221
222
223
224
225
# File 'lib/qb/options/option.rb', line 219

def value_data
  if value.respond_to? :to_data
    value.to_data
  else
    value
  end
end