Class: Aspera::CommandLineBuilder
- Inherits:
-
Object
- Object
- Aspera::CommandLineBuilder
- 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
-
#params_definition ⇒ Object
readonly
Returns the value of attribute params_definition.
Class Method Summary collapse
-
.normalize_description(full_description) ⇒ Object
Called by provider of definition before constructor of this class so that params_definition has all mandatory fields.
-
.yes_to_true(value) ⇒ Object
transform yes/no to true/false.
Instance Method Summary collapse
-
#add_command_line_options(options) ⇒ Object
add options directly to command line.
-
#add_env_args(env_args) ⇒ Object
add processed parameters to env and args, warns about unused parameters.
-
#initialize(param_hash, params_definition) ⇒ CommandLineBuilder
constructor
A new instance of CommandLineBuilder.
- #process_params ⇒ Object
- #read_param(name) ⇒ Object
Constructor Details
#initialize(param_hash, params_definition) ⇒ CommandLineBuilder
Returns a new instance of CommandLineBuilder.
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_definition ⇒ Object (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, | Aspera.assert_type(, Hash){name} unsupported_keys = .keys - OPTIONS_KEYS Aspera.assert(unsupported_keys.empty?){"Unsupported definition keys: #{unsupported_keys}"} Aspera.assert(.key?(:cli)){"Missing key: cli for #{name}"} Aspera.assert_type([:cli], Hash){'Key: cli'} Aspera.assert([:cli].key?(:type)){'Missing key: cli.type'} Aspera.assert_values([:cli][:type], CLI_OPTION_TYPES){"Unsupported processing type for #{name}"} # by default : optional [:mandatory] ||= false [:desc] ||= '' [:desc] = "DEPRECATED: #{[:deprecation]}\n#{[:desc]}" if .key?(:deprecation) cli = [: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 [:accepted_types] ||= [:cli][:type].eql?(:opt_without_arg) ? :bool : :string # single type is placed in array [:accepted_types] = [[:accepted_types]] unless [: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 () return if .nil? .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
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_params ⇒ Object
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 |