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
adds keys :env :args with resulting values after processing warns if some parameters were not used.
-
#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.
59 60 61 62 63 64 65 |
# File 'lib/aspera/command_line_builder.rb', line 59 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 = {} @result_args = [] @used_param_names = [] end |
Instance Attribute Details
#params_definition ⇒ Object (readonly)
Returns the value of attribute params_definition.
56 57 58 |
# File 'lib/aspera/command_line_builder.rb', line 56 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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aspera/command_line_builder.rb', line 28 def normalize_description(full_description) full_description.each do |name, | raise "Expecting Hash, but have #{options.class} in #{name}" unless .is_a?(Hash) unsupported_keys = .keys - OPTIONS_KEYS raise "Unsupported definition keys: #{unsupported_keys}" unless unsupported_keys.empty? raise "Missing key: cli for #{name}" unless .key?(:cli) raise 'Key: cli must be Hash' unless [:cli].is_a?(Hash) raise 'Missing key: cli.type' unless [:cli].key?(:type) raise "Unsupported processing type for #{name}: #{options[:cli][:type]}" unless CLI_OPTION_TYPES.include?([:cli][:type]) # by default : optional [:mandatory] ||= false [:desc] ||= '' [:desc] = "DEPRECATED: #{options[:deprecation]}\n#{options[:desc]}" if .key?(:deprecation) cli = [:cli] unsupported_cli_keys = cli.keys - CLI_KEYS raise "Unsupported cli keys: #{unsupported_cli_keys}" unless unsupported_cli_keys.empty? # 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
19 20 21 22 23 24 25 |
# File 'lib/aspera/command_line_builder.rb', line 19 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
80 81 82 83 |
# File 'lib/aspera/command_line_builder.rb', line 80 def () return if .nil? .each{|o|@result_args.push(o.to_s)} end |
#add_env_args(env, args) ⇒ Object
adds keys :env :args with resulting values after processing warns if some parameters were not used
69 70 71 72 73 74 75 76 77 |
# File 'lib/aspera/command_line_builder.rb', line 69 def add_env_args(env, args) Log.log.debug{"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.merge!(@result_env) args.push(*@result_args) return nil end |
#process_params ⇒ Object
85 86 87 88 89 |
# File 'lib/aspera/command_line_builder.rb', line 85 def process_params @params_definition.each_key do |k| process_param(k) end end |
#read_param(name) ⇒ Object
91 92 93 |
# File 'lib/aspera/command_line_builder.rb', line 91 def read_param(name) return process_param(name, read: true) end |