Class: Aspera::CommandLineBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/command_line_builder.rb

Overview

helper class to build command line from a parameter list (key-value hash) constructor takes hash: { ‘param1’:‘value1’, …} process_param is called repeatedly with all known parameters add_env_args is called to get resulting param list and env var (also checks that all params were used)

Constant Summary collapse

CLI_OPTION_TYPES =
%i[special ignore envvar].concat(CLI_OPTION_TYPE_SWITCH).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param_hash, params_definition) ⇒ CommandLineBuilder

Returns a new instance of CommandLineBuilder.

Parameters:

  • param_hash (Hash)

    with parameters

  • params_definition (Hash)

    with definition of parameters



62
63
64
65
66
67
68
69
70
# File 'lib/aspera/command_line_builder.rb', line 62

def initialize(param_hash, params_definition)
  @param_hash = param_hash # keep reference so that it can be modified by caller before calling `process_params`
  @params_definition = params_definition
  @result = {
    env:  {},
    args: []
  }
  @used_param_names = []
end

Instance Attribute Details

#params_definitionObject (readonly)

Returns the value of attribute params_definition.



58
59
60
# File 'lib/aspera/command_line_builder.rb', line 58

def params_definition
  @params_definition
end

Class Method Details

.normalize_description(full_description) ⇒ Object

Called by provider of definition before constructor of this class so that params_definition has all mandatory fields



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aspera/command_line_builder.rb', line 30

def normalize_description(full_description)
  full_description.each do |name, options|
    Aspera.assert_type(options, Hash){name}
    unsupported_keys = options.keys - OPTIONS_KEYS
    Aspera.assert(unsupported_keys.empty?){"Unsupported definition keys: #{unsupported_keys}"}
    Aspera.assert(options.key?(:cli)){"Missing key: cli for #{name}"}
    Aspera.assert_type(options[:cli], Hash){'Key: cli'}
    Aspera.assert(options[:cli].key?(:type)){'Missing key: cli.type'}
    Aspera.assert_values(options[:cli][:type], CLI_OPTION_TYPES){"Unsupported processing type for #{name}"}
    # by default : optional
    options[:mandatory] ||= false
    options[:desc] ||= ''
    options[:desc] = "DEPRECATED: #{options[:deprecation]}\n#{options[:desc]}" if options.key?(:deprecation)
    cli = options[:cli]
    unsupported_cli_keys = cli.keys - CLI_KEYS
    Aspera.assert(unsupported_cli_keys.empty?){"Unsupported cli keys: #{unsupported_cli_keys}"}
    # by default : string, unless it's without arg
    options[:accepted_types] ||= options[:cli][:type].eql?(:opt_without_arg) ? :bool : :string
    # single type is placed in array
    options[:accepted_types] = [options[:accepted_types]] unless options[:accepted_types].is_a?(Array)
    # add default switch name if not present
    if !cli.key?(:switch) && cli.key?(:type) && CLI_OPTION_TYPE_SWITCH.include?(cli[:type])
      cli[:switch] = '--' + name.to_s.tr('_', '-')
    end
  end
end

.yes_to_true(value) ⇒ Object

transform yes/no to true/false



21
22
23
24
25
26
27
# File 'lib/aspera/command_line_builder.rb', line 21

def yes_to_true(value)
  case value
  when 'yes' then return true
  when 'no' then return false
  end
  raise "unsupported value: #{value}"
end

Instance Method Details

#add_command_line_options(options) ⇒ Object

add options directly to command line



85
86
87
88
# File 'lib/aspera/command_line_builder.rb', line 85

def add_command_line_options(options)
  return if options.nil?
  options.each{|o|@result[:args].push(o.to_s)}
end

#add_env_args(env_args) ⇒ Object

add processed parameters to env and args, warns about unused parameters

Parameters:

  • env_args (Hash)

    with :env and :args



74
75
76
77
78
79
80
81
82
# File 'lib/aspera/command_line_builder.rb', line 74

def add_env_args(env_args)
  Log.log.debug{"add_env_args: ENV=#{@result[:env]}, ARGS=#{@result[:args]}"}
  # warn about non translated arguments
  @param_hash.each_pair{|key, val|Log.log.warn{"unrecognized parameter: #{key} = \"#{val}\""} if !@used_param_names.include?(key)}
  # set result
  env_args[:env].merge!(@result[:env])
  env_args[:args].push(*@result[:args])
  return nil
end

#process_paramsObject



90
91
92
93
94
# File 'lib/aspera/command_line_builder.rb', line 90

def process_params
  @params_definition.each_key do |k|
    process_param(k)
  end
end

#read_param(name) ⇒ Object



96
97
98
# File 'lib/aspera/command_line_builder.rb', line 96

def read_param(name)
  return process_param(name, read: true)
end