Method: Launchr::Mixlib::CLI#parse_options
- Defined in:
- lib/launchr/mixin/mixlib_cli.rb
#parse_options(argv = ARGV) ⇒ Object
Parses an array, by default ARGV, for command line options (as configured at the class level).
Parameters
- argv<Array>
-
The array of arguments to parse; defaults to ARGV
Returns
- argv<Array>
-
Returns any un-parsed elements.
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/launchr/mixin/mixlib_cli.rb', line 450 def (argv=ARGV) argv = argv.dup argv = guess_and_switchify_arguments(argv) @opt_parser = OptionParser.new do |opts| # Set the banner opts. = # Create new options .each do |opt_key, opt_val| opt_args = build_option_arguments(opt_val) opt_method = case opt_val[:on] when :on :on when :tail :on_tail when :head :on_head else raise ArgumentError, "You must pass :on, :tail, or :head to :on" end parse_block = \ Proc.new() do |*c| if c.empty? || c == [nil] c = true config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(c)) || c else c = c.first config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(c)) || c end puts (opts.to_s) if opt_val[:show_options] exit opt_val[:exit] if opt_val[:exit] end # opts.send(:on, *[opt_method,*opt_args, parse_block]) opt_args.unshift opt_method opt_args << parse_block opts.send(*opt_args) end end @opt_parser.summary_indent = @summary_indent if @summary_indent @opt_parser.summary_width = @summary_width if @summary_width @opt_parser.parse!(argv) @filtered_argv = argv # Deal with any required values fail = nil .each do |opt_key, opt_value| next unless config[opt_key] # next if config[opt_key] == opt_value[:default] reqargs = [] case opt_value[:requires] when nil when Proc begin result = opt_value[:requires].call(config) rescue reqargs << $!. end reqargs << result if result.class == String when Array,Symbol required_opts = [opt_value[:requires]].flatten required_opts.each do |required_opt| reqargs << required_opt.to_sym unless config[required_opt.to_sym] end reqargs.map! do |opt| arg = ([opt][:long_strip] || [opt][:short_strip]).dup arg.gsub!(/^-+/,"") if arguments.keys.include?(opt) arg end end unless reqargs.empty? fail = true opt = (opt_value[:long_strip] || opt_value[:short_strip]).dup opt.gsub!(/^-+/,"") if arguments.keys.include?(opt_key) puts "You must supply #{reqargs.join(", ")} with #{opt}!" end end if fail puts (@opt_parser.to_s) exit 2 end argv end |