Class: Aspera::Cli::Manager
- Inherits:
-
Object
- Object
- Aspera::Cli::Manager
- Defined in:
- lib/aspera/cli/manager.rb
Overview
parse command line options arguments options start with '-', others are commands resolves on extended value syntax
Instance Attribute Summary collapse
-
#ask_missing_mandatory ⇒ Object
Returns the value of attribute ask_missing_mandatory.
-
#ask_missing_optional ⇒ Object
Returns the value of attribute ask_missing_optional.
-
#fail_on_missing_mandatory ⇒ Object
writeonly
Sets the attribute fail_on_missing_mandatory.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
-
.get_from_list(short_value, descr, allowed_values) ⇒ Object
Find shortened string value in allowed symbol list.
-
.multi_choice_assert_msg(error_msg, accept_list) ⇒ Object
Generates error message with list of allowed values.
-
.option_line_to_name(name) ⇒ String
Change option name with dash to name with underscore.
- .option_name_to_line(name) ⇒ Object
-
.percent_selector(identifier) ⇒ Hash{Symbol => String}?
{field:,value:}if identifier is a percent selector, elsenil.
Instance Method Summary collapse
-
#add_option_preset(preset_hash, where, override: true) ⇒ Object
Adds each of the keys of specified hash as an option.
-
#add_types_info(types) ⇒ String
Add a type to the message if not special types.
-
#args_as_extended(end_marker) ⇒ Hash, Array
Read remaining args and build an
ArrayorHash. -
#clear_option(option_symbol) ⇒ Object
Set option to
nil. -
#command_or_arg_empty? ⇒ Boolean
Check if there were unprocessed values to generate error.
-
#declare(option_symbol, description, short: nil, allowed: nil, default: nil, handler: nil, deprecation: nil, schema: nil, &block) ⇒ Object
Declare an option.
-
#final_errors ⇒ Object
Unprocessed options or arguments ?.
-
#get_interactive(descr, check_option: false, multiple: false, accept_list: nil, schema: nil) ⇒ String
Prompt user for input in a list of symbols.
-
#get_next_argument(descr, mandatory: true, multiple: false, accept_list: nil, validation: Allowed::TYPES_STRING, aliases: nil, default: nil, schema: nil) ⇒ Object
One value, list or nil (if optional and no default).
- #get_next_command(command_list, aliases: nil) ⇒ Object
-
#get_option(option_symbol, mandatory: false) ⇒ Object
Get an option value by name either return value or calls handler, can return nil ask interactively if requested/required.
-
#initialize(program_name, argv = nil) ⇒ Manager
constructor
A new instance of Manager.
-
#instance_identifier(description: 'identifier', &block) {|field, value| ... } ⇒ String+
Resource identifier as positional parameter.
-
#known_options(only_defined: false) ⇒ Hash
Options as taken from config file and command line just before command execution.
-
#option_def(option_symbol) ⇒ OptionValue
Get an option definition by name.
-
#parse_options! ⇒ Object
Removes already known options from the list.
- #prompt_user_input(prompt, sensitive: false) ⇒ Object
-
#prompt_user_input_in_list(prompt, sym_list) ⇒ Symbol
prompt user for input in a list of symbols.
-
#set_option(option_symbol, value, where: 'code override') ⇒ Object
Set an option value by name, either store value or call handler String is given to extended value.
-
#unprocessed_options_with_value ⇒ Hash
Get all original options on command line used to generate a config in config file.
-
#unshift_next_argument(argument) ⇒ Object
Allows a plugin to add an argument as next argument to process.
Constructor Details
#initialize(program_name, argv = nil) ⇒ Manager
Returns a new instance of Manager.
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 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 |
# File 'lib/aspera/cli/manager.rb', line 246 def initialize(program_name, argv = nil) # command line values *not* starting with '-' @unprocessed_cmd_line_arguments = [] # command line values starting with at least one '-' @unprocessed_cmd_line_options = [] # a copy of all initial options @initial_cli_options = [] # Option descriptions: maps option symbol to its OptionValue descriptor # @type [Hash{Symbol => OptionValue}] @declared_options = {} # do we ask missing options and arguments to user ? @ask_missing_mandatory = false # STDIN.isatty # ask optional options if not provided and in interactive @ask_missing_optional = false # get_option fails if a mandatory parameter is asked @fail_on_missing_mandatory = true # Array of [key(sym), value] # those must be set before parse # parse consumes those defined only @option_pairs_batch = {} @option_pairs_env = {} # NOTE: was initially inherited but it is preferred to have specific methods @parser = OptionParser.new @parser.program_name = program_name # options can also be provided by env vars : --param-name -> ASCLI_PARAM_NAME env_prefix = program_name.upcase + OPTION_SEP_SYMBOL ENV.each do |k, v| @option_pairs_env[k.delete_prefix(env_prefix).downcase.to_sym] = v if k.start_with?(env_prefix) end Log.log.debug{"env=#{@option_pairs_env}".red} @unprocessed_cmd_line_options = [] @unprocessed_cmd_line_arguments = [] return if argv.nil? # true until `--` is found (stop options) = true until argv.empty? value = argv.shift if && value.start_with?('-') Log.log.trace1{"opt: #{value}"} if value.eql?(OPTIONS_STOP) = false else @unprocessed_cmd_line_options.push(value) end else Log.log.trace1{"arg: #{value}"} @unprocessed_cmd_line_arguments.push(value) end end @initial_cli_options = @unprocessed_cmd_line_options.dup.freeze Log.log.trace1{"add_cmd_line_options:commands/arguments=#{@unprocessed_cmd_line_arguments},options=#{@unprocessed_cmd_line_options}".red} @parser.separator('') @parser.separator('OPTIONS: global') declare(:interactive, 'Use interactive input of missing params', allowed: Allowed::TYPES_BOOLEAN, handler: {o: self, m: :ask_missing_mandatory}) declare(:ask_options, 'Ask even optional options', allowed: Allowed::TYPES_BOOLEAN, handler: {o: self, m: :ask_missing_optional}) # do not parse options yet, let's wait for option `-h` to be overridden end |
Instance Attribute Details
#ask_missing_mandatory ⇒ Object
Returns the value of attribute ask_missing_mandatory.
241 242 243 |
# File 'lib/aspera/cli/manager.rb', line 241 def ask_missing_mandatory @ask_missing_mandatory end |
#ask_missing_optional ⇒ Object
Returns the value of attribute ask_missing_optional.
241 242 243 |
# File 'lib/aspera/cli/manager.rb', line 241 def ask_missing_optional @ask_missing_optional end |
#fail_on_missing_mandatory=(value) ⇒ Object (writeonly)
Sets the attribute fail_on_missing_mandatory
242 243 244 |
# File 'lib/aspera/cli/manager.rb', line 242 def fail_on_missing_mandatory=(value) @fail_on_missing_mandatory = value end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
240 241 242 |
# File 'lib/aspera/cli/manager.rb', line 240 def parser @parser end |
Class Method Details
.get_from_list(short_value, descr, allowed_values) ⇒ Object
Find shortened string value in allowed symbol list
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/aspera/cli/manager.rb', line 200 def get_from_list(short_value, descr, allowed_values) Aspera.assert_type(short_value, String) # we accept shortcuts matching_exact = allowed_values.select{ |i| i.to_s.eql?(short_value)} return matching_exact.first if matching_exact.length == 1 matching = allowed_values.select{ |i| i.to_s.start_with?(short_value)} Aspera.assert(!matching.empty?, multi_choice_assert_msg("unknown value for #{descr}: #{short_value}", allowed_values), type: BadArgument) Aspera.assert(matching.length.eql?(1), multi_choice_assert_msg("ambiguous shortcut for #{descr}: #{short_value}", matching), type: BadArgument) return BoolValue.true?(matching.first) if allowed_values.eql?(BoolValue::ALL) matching.first end |
.multi_choice_assert_msg(error_msg, accept_list) ⇒ Object
Generates error message with list of allowed values
215 216 217 |
# File 'lib/aspera/cli/manager.rb', line 215 def multi_choice_assert_msg(error_msg, accept_list) [error_msg, 'Use:', *accept_list.map{ |choice| "- #{choice}"}.sort].join("\n") end |
.option_line_to_name(name) ⇒ String
Change option name with dash to name with underscore
222 223 224 |
# File 'lib/aspera/cli/manager.rb', line 222 def option_line_to_name(name) name.gsub(OPTION_SEP_LINE, OPTION_SEP_SYMBOL) end |
.option_name_to_line(name) ⇒ Object
226 227 228 |
# File 'lib/aspera/cli/manager.rb', line 226 def option_name_to_line(name) "#{OPTION_PREFIX}#{name.to_s.gsub(OPTION_SEP_SYMBOL, OPTION_SEP_LINE)}" end |
.percent_selector(identifier) ⇒ Hash{Symbol => String}?
Returns {field:,value:} if identifier is a percent selector, else nil.
231 232 233 234 235 236 237 |
# File 'lib/aspera/cli/manager.rb', line 231 def percent_selector(identifier) Aspera.assert_type(identifier, String) if (m = identifier.match(REGEX_LOOKUP_ID_BY_FIELD)) return {field: m[1], value: ExtendedValue.instance.evaluate(m[2], context: "percent selector: #{m[1]}")} end nil end |
Instance Method Details
#add_option_preset(preset_hash, where, override: true) ⇒ Object
Adds each of the keys of specified hash as an option
516 517 518 519 520 521 522 523 |
# File 'lib/aspera/cli/manager.rb', line 516 def add_option_preset(preset_hash, where, override: true) Aspera.assert_type(preset_hash, Hash) Log.log.debug{"add_option_preset: #{preset_hash}, #{where}, #{override}"} preset_hash.each do |k, v| option_symbol = k.to_sym @option_pairs_batch[option_symbol] = v if override || !@option_pairs_batch.key?(option_symbol) end end |
#add_types_info(types) ⇒ String
Add a type to the message if not special types
307 308 309 310 |
# File 'lib/aspera/cli/manager.rb', line 307 def add_types_info(types) return '' if !types || types.empty? || types.eql?(Allowed::TYPES_ENUM) || types.eql?(Allowed::TYPES_BOOLEAN) || types.eql?(Allowed::TYPES_STRING) " (#{types.map(&:name).join(', ')})" end |
#args_as_extended(end_marker) ⇒ Hash, Array
Read remaining args and build an Array or Hash
673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'lib/aspera/cli/manager.rb', line 673 def args_as_extended(end_marker) # This extended value does not take args (`@:`) # ExtendedValue.assert_no_value(end_marker, :p) end_marker = SpecialValues::EOA if end_marker.empty? result = nil get_next_argument('args', multiple: end_marker).each do |argument| Aspera.assert(argument.include?(OPTION_VALUE_SEPARATOR)){"Positional argument: #{argument} does not include #{OPTION_VALUE_SEPARATOR}"} path, value = argument.split(OPTION_VALUE_SEPARATOR, 2) result = DotContainer.dotted_to_container(path.split(DotContainer::SEPARATOR), smart_convert(value), result) end result end |
#clear_option(option_symbol) ⇒ Object
Set option to nil
507 508 509 510 |
# File 'lib/aspera/cli/manager.rb', line 507 def clear_option(option_symbol) Aspera.assert_type(option_symbol, Symbol) option_def(option_symbol).clear end |
#command_or_arg_empty? ⇒ Boolean
Check if there were unprocessed values to generate error
531 532 533 |
# File 'lib/aspera/cli/manager.rb', line 531 def command_or_arg_empty? @unprocessed_cmd_line_arguments.empty? end |
#declare(option_symbol, description, short: nil, allowed: nil, default: nil, handler: nil, deprecation: nil, schema: nil, &block) ⇒ Object
Declare an option
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/aspera/cli/manager.rb', line 322 def declare(option_symbol, description, short: nil, allowed: nil, default: nil, handler: nil, deprecation: nil, schema: nil, &block) Aspera.assert_type(option_symbol, Symbol) Aspera.assert(!@declared_options.key?(option_symbol)){"#{option_symbol} already declared"} Aspera.assert(description[-1] != '.'){"#{option_symbol} ends with dot"} Aspera.assert(description[0] == description[0].upcase){"#{option_symbol} description does not start with an uppercase"} Aspera.assert(!['hash', 'extended value'].any?{ |s| description.downcase.include?(s)}){"#{option_symbol} shall use :allowed instead of hash/extended value in option description"} Aspera.assert_type(handler, Hash) if handler Aspera.assert(handler.keys.sort.eql?(%i[m o]), 'handler must have keys :m and :o') if handler option_attrs = @declared_options[option_symbol] = OptionValue.new( option: option_symbol, description: description, allowed: allowed, handler: handler, deprecation: deprecation, schema: schema ) real_types = option_attrs.types&.reject{ |i| [NilClass, String, Symbol].include?(i)} description += add_types_info(real_types) description = "#{description} (#{'deprecated'.blue}: #{deprecation})" if deprecation set_option(option_symbol, default, where: 'default') unless default.nil? on_args = [description] case option_attrs.types when Allowed::TYPES_ENUM, Allowed::TYPES_BOOLEAN # This option value must be a symbol (or array of symbols) set_option(option_symbol, BoolValue.true?(default), where: 'default') if option_attrs.values.eql?(BoolValue::ALL) && !default.nil? value = get_option(option_symbol) help_values = if option_attrs.types.eql?(Allowed::TYPES_BOOLEAN) highlight_current_in_list(BoolValue::SYMBOLS, BoolValue.to_sym(value)) else highlight_current_in_list(option_attrs.values, value) end on_args[0] = "#{description}: #{help_values}" on_args.push(symbol_to_option(option_symbol, 'ENUM')) # on_args.push(option_attrs.values) @parser.on(*on_args) do |v| set_option(option_symbol, self.class.get_from_list(v.to_s, description, option_attrs.values), where: SOURCE_USER) end when Allowed::TYPES_NONE Aspera.assert_type(block, Proc){"missing execution block for #{option_symbol}"} on_args.push(symbol_to_option(option_symbol)) on_args.push("-#{short}") if short.is_a?(String) @parser.on(*on_args, &block) else on_args.push(symbol_to_option(option_symbol, 'VALUE')) on_args.push("-#{short}VALUE") unless short.nil? # coerce integer on_args.push(Integer) if option_attrs.types.eql?(Allowed::TYPES_INTEGER) @parser.on(*on_args) do |v| set_option(option_symbol, v, where: SOURCE_USER) end end Log.log.trace1{"on_args=#{on_args}"} end |
#final_errors ⇒ Object
Unprocessed options or arguments ?
536 537 538 539 540 541 |
# File 'lib/aspera/cli/manager.rb', line 536 def final_errors result = [] result.push("unprocessed options: #{@unprocessed_cmd_line_options}") unless @unprocessed_cmd_line_options.empty? result.push("unprocessed values: #{@unprocessed_cmd_line_arguments}") unless @unprocessed_cmd_line_arguments.empty? result end |
#get_interactive(descr, check_option: false, multiple: false, accept_list: nil, schema: nil) ⇒ String
Prompt user for input in a list of symbols
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
# File 'lib/aspera/cli/manager.rb', line 644 def get_interactive(descr, check_option: false, multiple: false, accept_list: nil, schema: nil) option_attrs = @declared_options[descr.to_sym] what = option_attrs ? 'option' : 'argument' default_prompt = "#{what}: #{descr}" if !@ask_missing_mandatory = "Missing #{default_prompt}" = self.class.multi_choice_assert_msg(, accept_list) if accept_list += "\n#{TerminalFormatter::HINT}Give `#{HELP}` as argument to retrieve the schema of the missing argument." if schema raise Cli::MissingArgument, end # ask interactively result = [] puts(' (one per line, end with empty line)') if multiple loop do prompt = default_prompt prompt = "#{accept_list.join(' ')}\n#{default_prompt}" if accept_list entry = prompt_user_input(prompt, sensitive: option_attrs&.sensitive) break if entry.empty? && multiple entry = ExtendedValue.instance.evaluate(entry, context: 'interactive input') entry = self.class.get_from_list(entry, descr, accept_list) if accept_list return entry unless multiple result.push(entry) end result end |
#get_next_argument(descr, mandatory: true, multiple: false, accept_list: nil, validation: Allowed::TYPES_STRING, aliases: nil, default: nil, schema: nil) ⇒ Object
Returns one value, list or nil (if optional and no default).
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/aspera/cli/manager.rb', line 385 def get_next_argument(descr, mandatory: true, multiple: false, accept_list: nil, validation: Allowed::TYPES_STRING, aliases: nil, default: nil, schema: nil) Aspera.assert_array_all(accept_list, Symbol) unless accept_list.nil? Aspera.assert_hash_all(aliases, Symbol, Symbol) unless aliases.nil? validation = Symbol unless accept_list.nil? validation = [validation] unless validation.is_a?(Array) || validation.nil? Aspera.assert_array_all(validation, Class){'validation'} unless validation.nil? descr = "#{descr}#{add_types_info(validation)}" result = if !@unprocessed_cmd_line_arguments.empty? case multiple when true values = @unprocessed_cmd_line_arguments.shift(@unprocessed_cmd_line_arguments.length) when false values = [@unprocessed_cmd_line_arguments.shift] when String index = @unprocessed_cmd_line_arguments.index(multiple) if index values = @unprocessed_cmd_line_arguments.shift(index) @unprocessed_cmd_line_arguments.shift # remove end marker else values = @unprocessed_cmd_line_arguments.shift(@unprocessed_cmd_line_arguments.length) end else Aspera.error_unexpected_value(multiple){'multiple'} end values = values.map{ |v| ExtendedValue.instance.evaluate(v, context: "argument: #{descr}", allowed: validation)} # If expecting list and only one arg of type array : it is the list values = values.first if multiple && values.length.eql?(1) && values.first.is_a?(Array) if accept_list allowed_values = [].concat(accept_list) allowed_values.concat(aliases.keys) unless aliases.nil? values = values.map{ |v| self.class.get_from_list(v, descr, allowed_values)} end multiple ? values : values.first elsif !default.nil? then default # no value provided, either get value interactively, or exception elsif mandatory then get_interactive(descr, multiple: multiple, accept_list: accept_list, schema: schema) end if result.is_a?(String) && validation&.eql?(Allowed::TYPES_INTEGER) int_result = Integer(result, exception: false) raise Cli::BadArgument, "Invalid integer: #{result}" if int_result.nil? result = int_result end Log.log.trace1{"#{descr}=#{result}"} result = aliases[result] if aliases&.key?(result) # if value comes from JSON/YAML, it may come as Integer result = result.to_s if result.is_a?(Integer) && validation&.eql?(Allowed::TYPES_STRING) if validation && (mandatory || !result.nil?) value_list = multiple ? result : [result] value_list.each do |value| raise SchemaRequest.new(:argument, descr, schema) if validation.include?(Hash) && value.eql?(HELP) raise Cli::BadArgument, "Argument #{descr} is a #{value.class} but must be #{'one of: ' if validation.length > 1}#{validation.map(&:name).join(', ')}" unless validation.any?{ |t| value.is_a?(t)} end end result end |
#get_next_command(command_list, aliases: nil) ⇒ Object
460 |
# File 'lib/aspera/cli/manager.rb', line 460 def get_next_command(command_list, aliases: nil); get_next_argument('command', accept_list: command_list, aliases: aliases); end |
#get_option(option_symbol, mandatory: false) ⇒ Object
Get an option value by name either return value or calls handler, can return nil ask interactively if requested/required
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/aspera/cli/manager.rb', line 476 def get_option(option_symbol, mandatory: false) Aspera.assert_type(option_symbol, Symbol) option_attrs = option_def(option_symbol) result = option_attrs.value # Do not fail for manual generation if option mandatory but not set return :skip_missing_mandatory if result.nil? && mandatory && !@fail_on_missing_mandatory if result.nil? if !@ask_missing_mandatory Aspera.assert(!mandatory, type: Cli::BadArgument){"Missing mandatory option: #{option_symbol}"} elsif @ask_missing_optional || mandatory # ask_missing_mandatory result = get_interactive(option_symbol.to_s, check_option: true, accept_list: option_attrs.values, schema: option_attrs.schema) set_option(option_symbol, result, where: 'interactive') end end result end |
#instance_identifier(description: 'identifier', &block) {|field, value| ... } ⇒ String+
Resource identifier as positional parameter
450 451 452 453 454 455 456 457 458 |
# File 'lib/aspera/cli/manager.rb', line 450 def instance_identifier(description: 'identifier', &block) res_id = get_next_argument(description, multiple: get_option(:bulk)) if res_id.nil? # Can be an Array if res_id.is_a?(String) && (m = Manager.percent_selector(res_id)) Aspera.assert(block_given?, type: Cli::BadArgument){"Percent syntax for #{description} not supported in this context"} res_id = yield(m[:field], m[:value]) end res_id end |
#known_options(only_defined: false) ⇒ Hash
Returns options as taken from config file and command line just before command execution.
564 565 566 567 568 569 570 571 572 573 |
# File 'lib/aspera/cli/manager.rb', line 564 def (only_defined: false) result = {} @declared_options.each_key do |option_symbol| v = get_option(option_symbol) result[option_symbol] = v unless only_defined && v.nil? rescue => e result[option_symbol] = e.to_s end result end |
#option_def(option_symbol) ⇒ OptionValue
Get an option definition by name
466 467 468 469 |
# File 'lib/aspera/cli/manager.rb', line 466 def option_def(option_symbol) Aspera.assert(@declared_options.key?(option_symbol), type: Cli::BadArgument){"Unknown option: #{option_symbol}"} @declared_options[option_symbol] end |
#parse_options! ⇒ Object
Removes already known options from the list
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
# File 'lib/aspera/cli/manager.rb', line 576 def Log.log.trace1('parse_options!'.red) # First options from conf file consume_option_pairs(@option_pairs_batch, 'set') # Then, env var (to override) consume_option_pairs(@option_pairs_env, 'env') # Then, command line override = [] begin # remove known options one by one, exception if unknown Log.log.trace1('Before parse') Log.dump(:unprocessed_cmd_line_options, @unprocessed_cmd_line_options, level: :trace1) @parser.parse!(@unprocessed_cmd_line_options) Log.log.trace1('After parse') rescue OptionParser::InvalidOption => e Log.log.trace1{"InvalidOption #{e}".red} # An option like --a.b.c=d does: a={"b":{"c":ext_val(d)}} if e.args.first.start_with?(OPTION_PREFIX) name, value = e.args.first.delete_prefix(OPTION_PREFIX).split(OPTION_VALUE_SEPARATOR, 2) if !value.nil? path = name.split(DotContainer::SEPARATOR) option_sym = self.class.option_line_to_name(path.shift).to_sym if @declared_options.key?(option_sym) # it's a known option, so let's process it set_option(option_sym, DotContainer.dotted_to_container(path, smart_convert(value), get_option(option_sym)), where: 'dotted') # resume to next retry end end end # Save for later processing .push(e.args.first) retry end Log.log.trace1{"remains: #{}"} # Set unprocessed options for next time @unprocessed_cmd_line_options = end |
#prompt_user_input(prompt, sensitive: false) ⇒ Object
615 616 617 618 619 620 621 |
# File 'lib/aspera/cli/manager.rb', line 615 def prompt_user_input(prompt, sensitive: false) return $stdin.getpass("#{prompt}> ") if sensitive print("#{prompt}> ") line = $stdin.gets Aspera.assert_type(line, String){'Unexpected end of standard input'} line.chomp end |
#prompt_user_input_in_list(prompt, sym_list) ⇒ Symbol
prompt user for input in a list of symbols
627 628 629 630 631 632 633 634 635 636 |
# File 'lib/aspera/cli/manager.rb', line 627 def prompt_user_input_in_list(prompt, sym_list) loop do input = prompt_user_input(prompt).to_sym if sym_list.any?{ |a| a.eql?(input)} return input else $stderr.puts("No such #{prompt}: #{input}, select one of: #{sym_list.join(', ')}") # rubocop:disable Style/StderrPuts end end end |
#set_option(option_symbol, value, where: 'code override') ⇒ Object
Set an option value by name, either store value or call handler String is given to extended value
499 500 501 502 503 504 |
# File 'lib/aspera/cli/manager.rb', line 499 def set_option(option_symbol, value, where: 'code override') Aspera.assert_type(option_symbol, Symbol) option = option_def(option_symbol) raise SchemaRequest.new(:option, option.option, option.schema) if option.types&.include?(Hash) && value.eql?(HELP) option.assign_value(value, where: where) end |
#unprocessed_options_with_value ⇒ Hash
Get all original options on command line used to generate a config in config file
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/aspera/cli/manager.rb', line 545 def result = {} @initial_cli_options.each do |option_argument| # ignore short options next unless option_argument.start_with?(OPTION_PREFIX) name, value = option_argument.delete_prefix(OPTION_PREFIX).split(OPTION_VALUE_SEPARATOR, 2) # ignore options without value next if value.nil? Log.log.debug{"option #{name}=#{value}"} path = name.split(DotContainer::SEPARATOR) path[0] = self.class.option_line_to_name(path[0]) DotContainer.dotted_to_container(path, smart_convert(value), result) @unprocessed_cmd_line_options.delete(option_argument) end result end |
#unshift_next_argument(argument) ⇒ Object
Allows a plugin to add an argument as next argument to process
526 527 528 |
# File 'lib/aspera/cli/manager.rb', line 526 def unshift_next_argument(argument) @unprocessed_cmd_line_arguments.unshift(argument) end |