Method: CommandLine::OptionParser#parse_argv
- Defined in:
- lib/commandline/optionparser/optionparser.rb
#parse_argv(argv, &block) ⇒ Object
Seperates options from arguments Does not look for valid options ( or should it? )
%w(-fred file1 file2) => ["-fred", ["file1", "file2"]]
%w(--fred -t -h xyz) => ["--fred", []] ["-t", []] ["-h", ["xyz"]]
%w(-f=file) => ["-f", ["file"]]
%w(--file=fred) => ["--file", ["fred"]]
%w(-file=fred) => ["-file", ["fred"]]
['-file="fred1 fred2"'] => ["-file", ["fred1", "fred2"]]
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 442 443 444 445 446 447 448 |
# File 'lib/commandline/optionparser/optionparser.rb', line 391 def parse_argv(argv, &block) return parse_posix_argv(argv, &block) if @posix @not_parsed = [] tagged = [] argv.each_with_index { |e,i| if "--" == e @not_parsed = argv[(i+1)..(argv.size+1)] break elsif "-" == e tagged << [:arg, e] elsif ?- == e[0] m = Option::GENERAL_OPT_EQ_ARG_RE.match(e) if m.nil? tagged << [:opt, e] else tagged << [:opt, m[1]] tagged << [:arg, m[2]] end else tagged << [:arg, e] end } # # The tagged array has the form: # [ # [:opt, "-a"], [:arg, "filea"], # [:opt, "-b"], [:arg, "fileb"], # #[:not_parsed, ["-z", "-y", "file", "file2", "-a", "-b"]] # ] # # Now, combine any adjacent args such that # [[:arg, "arg1"], [:arg, "arg2"]] # becomes # [[:args, ["arg1", "arg2"]]] # and the final result should be # [ "--file", ["arg1", "arg2"]] # parsed = [] @args = [] tagged.each { |e| if :opt == e[0] parsed << [e[1], []] elsif :arg == e[0] if Array === parsed[-1] parsed[-1][-1] += [e[1]] else @args << e[1] end else raise "How did we get here?" end } parsed.each { |e| block.call(e) } end |