Method: Beaker::Options::Parser#parse_args

Defined in:
lib/beaker/options/parser.rb

#parse_args(args = ARGV) ⇒ Object

Parses ARGV or provided arguments array, file options, hosts options and combines with environment variables and preset defaults to generate a Hash representing the Beaker options for a given test run

Order of priority is as follows:

1.  environment variables are given top priority
2.  ARGV or provided arguments array
3.  the 'CONFIG' section of the hosts file
4.  options file values
5.  subcommand options, if executing beaker subcommands
6.  subcommand options from $HOME/.beaker/subcommand_options.yaml
7.  project values in .beaker.yml
8.  default or preset values are given the lowest priority

Parameters:

  • args (Array) (defaults to: ARGV)

    ARGV or a provided arguments array

Raises:

  • (ArgumentError)

    Raises error on bad input



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/beaker/options/parser.rb', line 214

def parse_args(args = ARGV)
  @options                        = @presets.presets
  @attribution = @attribution.merge(tag_sources(@presets.presets, "preset"))
  cmd_line_options                = @command_line_parser.parse(args)
  cmd_line_options[:command_line] = ([$0] + args).join(' ')
  @attribution = @attribution.merge(tag_sources(cmd_line_options, "flag"))

  # Merge options in reverse precedence order. First project options,
  # then global options from $HOME/.beaker/subcommand_options.yaml,
  # then subcommand options in the project.
  subcommand_options_file = Beaker::Subcommands::SubcommandUtil::SUBCOMMAND_OPTIONS
  {
    "project" => ".beaker.yml",
    "homedir" => "#{ENV['HOME']}/#{subcommand_options_file}",
    "subcommand" => subcommand_options_file,
  }.each_pair do |src, path|
    opts = if src == "project"
             Beaker::Options::SubcommandOptionsParser.parse_options_file(path)
           else
             Beaker::Options::SubcommandOptionsParser.parse_subcommand_options(args, path)
           end
    @attribution = @attribution.merge(tag_sources(opts, src))
    @options.merge!(opts)
  end

  file_options                    = Beaker::Options::OptionsFileParser.parse_options_file(cmd_line_options[:options_file] || options[:options_file])
  @attribution = @attribution.merge(tag_sources(file_options, "options_file"))

  # merge together command line and file_options
  #   overwrite file options with command line options
  cmd_line_and_file_options       = file_options.merge(cmd_line_options)

  # merge command line and file options with defaults
  #   overwrite defaults with command line and file options
  @options                        = @options.merge(cmd_line_and_file_options)

  if not @options[:help] and not @options[:beaker_version_print]
    hosts_options = parse_hosts_options

    # merge in host file vars
    #   overwrite options (default, file options, command line) with host file options
    @options      = @options.merge(hosts_options)
    @attribution = @attribution.merge(tag_sources(hosts_options, "host_file"))

    # re-merge the command line options
    #   overwrite options (default, file options, hosts file ) with command line arguments
    @options      = @options.merge(cmd_line_options)
    @attribution = @attribution.merge(tag_sources(cmd_line_options, "cmd"))

    # merge in env vars
    #   overwrite options (default, file options, command line, hosts file) with env
    env_vars      = @presets.env_vars

    @options = @options.merge(env_vars)
    @attribution = @attribution.merge(tag_sources(env_vars, "env"))

    normalize_args
  end

  @options
end