Method: Params.init

Defined in:
lib/params.rb

.init(args) ⇒ Object

Initializes the project parameters. Precedence is: Defined params, file and command line is highest.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/params.rb', line 254

def Params.init(args)
  #define default configuration file
  Params['conf_file'] = "~/.bbfs/etc/config_#{File.basename($PROGRAM_NAME)}.yml"

  @init_info_messages = []
  @init_warning_messages = []

  #parse command line argument and set configuration file if provided by user
  results = parse_command_line_arguments(args)
  if results['conf_file']
    Params['conf_file'] = File.expand_path(results['conf_file'])
    if !File.exist?(Params['conf_file']) or File.directory?(Params['conf_file'])
      raise("Param:'conf_file' value:'#{Params['conf_file']}' is a file name which does not exist" +
                " or a directory name")
    end
  end

  Params['conf_file'] = File.expand_path(Params['conf_file'])

  #load yml params if path is provided and exists
  if File.exist?(Params['conf_file'])
    @init_info_messages << "Loading parameters from configuration file:'#{Params['conf_file']}'"
    if not read_yml_params(File.open(Params['conf_file'], 'r'))
      raise("Bad configuration file #{Params['conf_file']}.")
    end
  else
    @init_warning_messages << "Configuration file path:'#{Params['conf_file']}' does not exist. " + \
                              "Skipping loading file parameters."
  end

  #override command line argument
  results.keys.each do |result_name|
    override_param(result_name, results[result_name])
  end

  # Prints help and parameters if needed.
  if @show_help_and_exit
    # Print parameters + description and exit.
    puts "Full list of parameters:"
    Params.each do |name, param|
      puts "--#{name}, #{param.type}, default:#{param.value}\n\t#{param.desc}"
    end
    exit
  end

  # Add parameters to log init messages (used by Log.init if param:print_params_to_stdout is true)
  @init_info_messages << 'Initialized executable parameters:'
  @init_info_messages << '---------------------------------'
  counter=0
  @params_data_base.values.each do |param|
    counter += 1
    @init_info_messages << "Param ##{counter}: #{param.name}=#{param.value}"
  end
  @init_info_messages << '---------------------------------'
end