Class: QB::Options

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

Defined Under Namespace

Modules: Types Classes: Option

Constant Summary collapse

QB_DEFAULTS =

Default initial values for #qb.

Returns:

  • (Hash)
{
  'hosts' => ['localhost'].freeze,
  'facts' => true,
  'print' => [].freeze,
  'verbose' => false,
  'run' => true,
  'ask' => false,
}.freeze
SPACER =

Appended on the end of an opts.on call to create a newline after the option (making the help output a bit easier to read)

You might think the empty string would be reasonable, but OptionParser blows up if you do that.

Returns:

  • (String)
' '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, argv) ⇒ Options

Returns a new instance of Options.

Parameters:

  • role (Role)

    the role to parse the args for.



187
188
189
190
191
192
193
# File 'lib/qb/options.rb', line 187

def initialize role, argv
  @role = role
  @argv = argv
  @qb = QB_DEFAULTS.dup
  
  parse!
end

Instance Attribute Details

#ansibleObject (readonly)

Returns the value of attribute ansible.



59
60
61
# File 'lib/qb/options.rb', line 59

def ansible
  @ansible
end

#qbObject (readonly)

Returns the value of attribute qb.



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

def qb
  @qb
end

#role_optionsObject (readonly)

Returns the value of attribute role_options.



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

def role_options
  @role_options
end

Class Method Details

.add(opts, options, role, include_path = []) ⇒ Object

Add the options from a role to the OptionParser

Parameters:

  • opts (OptionParser)

    The option parser to add options to.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/qb/options.rb', line 137

def self.add opts, options, role, include_path = []
  QB.debug "adding options", "role" => role
  
  role.option_metas.each do |option_meta|
    if option_meta.key? 'include'
      include_role opts, options, option_meta, include_path
      
    else
      # create an option
      option = Option.new role, option_meta, include_path
      
      option.option_parser_add opts, included: !include_path.empty?
      
      options[option.cli_name] = option
    end
  end # each var
end

.cli_ize_name(option_name) ⇒ String

turn a name into a "command line" version by replacing underscores with dashes.

Examples:

QB::Options.cli_ize_name "my_var" # => "my-var"

Parameters:

  • option_name (String)

    the input option name.

Returns:

  • (String)

    the CLI-ized name.



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

def self.cli_ize_name option_name
  option_name.gsub '_', '-'
end

.include_role(opts, options, include_meta, include_path) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/qb/options.rb', line 106

def self.include_role opts, options, include_meta, include_path
  role_name = include_meta['include']
  role = QB::Role.require role_name
  new_include_path = if include_meta.key? 'as'
    case include_meta['as']
    when nil, false
      # include it in with the parent role's options
      include_path
    when String
      include_path + [include_meta['as']]
    else
      raise QB::Role::MetadataError.new,
        "bad 'as' value: #{ include_meta.inspect }"
    end
  else
    include_path + [role.namespaceless]
  end
  
  QB.debug "including #{ role.name } as #{ new_include_path.join('-') }"
  
  opts.separator "Options for included #{ role.name } role:"
  
  add opts, options, role, new_include_path
end

.parse!(role, argv) ⇒ Array<Hash<String, Option|Object>>

Destructively removes options from @argv and populates ansible, role, and qb option hashes.

Parameters:

  • role (QB::Role)

    the role to parse the options for.

  • args (Array<String>)

    CLI args -- ARGV with the role arg shifted off.

Returns:

  • (Array<Hash<String, Option|Object>>)

    a two-element array:

    1. the options for the role, hash of Option#cli_name to Option instances.

    2. the general qb options, hash of String key to option values.

Raises:

  • if bad options are found.



175
176
177
178
# File 'lib/qb/options.rb', line 175

def self.parse! role, argv
  options = self.new role, argv
  [options.role_options, options.qb]
end

.var_ize_name(option_name) ⇒ String

turn a name into a "ruby / ansible variable" version by replacing dashes with underscores.

Examples:

QB::Options.cli_ize_name "my-var" # => "my_var"

Parameters:

  • option_name (String)

    the input option name.

Returns:

  • (String)

    the ruby / ansible var-ized name.



102
103
104
# File 'lib/qb/options.rb', line 102

def self.var_ize_name option_name
  option_name.gsub '-', '_'
end

Instance Method Details

#ask?return_type

TODO:

Document ask? method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



204
205
206
# File 'lib/qb/options.rb', line 204

def ask?
  @qb['ask']
end