Method: Launchr::Mixlib::CLI#guess_and_switchify_arguments

Defined in:
lib/launchr/mixin/mixlib_cli.rb

#guess_and_switchify_arguments(argv) ⇒ Object



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
441
# File 'lib/launchr/mixin/mixlib_cli.rb', line 389

def guess_and_switchify_arguments argv
  # collect argument declarations
  short_args = @arguments.values.map { |args| args[:short_strip] }
  long_args  = @arguments.values.map { |args| args[:long_strip]  }

  short_opts_args = @options_arguments.values.map { |args| args[:short_strip] }
  long_opts_args  = @options_arguments.values.map { |args| args[:long_strip]  }

  short_opts_args_unfiltered = @options_arguments.values.map { |args| args[:short] }
  long_opts_args_unfiltered  = @options_arguments.values.map { |args| args[:long]  }

  i = 0
  while i < argv.size

    # switchify the argv argument if it looks like a recognised argument
    if short_args.include?("-"+argv[i].sub(/^no-/,"").sub(/(=|\s).*/,""))
      argv[i] = "-" + argv[i]
    end

    if long_args.include?("--"+argv[i].sub(/^no-/,"").sub(/(=|\s).*/,""))
      argv[i] = "--" + argv[i]
    end

    # when the argv argument matches a recognised option or argument
    # without the style =value, the following argument might have to be skipped...

    # so find the index of the switch declaration
    j = nil
    if short_opts_args.include?(argv[i])
      j = short_opts_args.index(argv[i])
    end
    if long_opts_args.include?(argv[i])
      j = long_opts_args.index(argv[i])
    end

    if j
      # when the switch declaration has a required argument
      if short_opts_args_unfiltered[j] =~ /( .+|\<|\=|[A-Z])/
        # skip forward one
        i += 1
      end
      # when the switch declaration has a required argument
      if long_opts_args_unfiltered[j] =~ /( .+|\<|\=|[A-Z])/
        # skip forward one
        i += 1
      end
    end
    # next argument
    i += 1
  end

  argv
end