views:

32

answers:

1

Is there any way to kick off optparse several times in one Ruby program, each with different sets of options?


Example:

$ myscript.rb --subsys1opt a --subsys2opt b

here, myscript.rb would use subsys1 and subsys2, delegating their options handling logic to them, possibly in a sequence where 'a' is processed first, followed by 'b' in separate OptParser object; each time picking options only relevant for that context.. A final phase could check that there is nothing unknown left after each part processed theirs.

Use cases:

  1. in a loosely coupled -frontend- program , where various components have different arguments, I don't want 'main' to know about everything, just to delegate sets of arguments/options to each part

  2. Embedding some larger system like RSpec into my application, and I'd to simply pass command line through their options without my wrapper knowing those.

I'd be OK with some delimiter option as well, like '--' or '--vmargs' in some Java apps.

There are lots of real world examples for similar things in the Unix world (startx/X, git plumbing and porcelain), where one layer handles some options but propagates the rest to the lower layer.

Out of the box, this doesn't seem to work: each OptionParse.parse! calls will do an exhaustive processing, failing on anything it doesn't know about. I guess i'd happy to skip unknown options. Any hints, perhaps alternative approaches are welcome.

+1  A: 

Assuming the order in which the parsers will run is well defined, you can just stored the extra options in a temporary global variable and run OptionParser#parse! each set of options. The easiest way to do this is to use a delimiter like you alluded to. Suppose the second set of arguments is separated from the first by the delimiter --. Then this will do what you want:

opts = OptionParser.new do |opts|
    # set up one OptionParser here
end

both_args = $*.join(" ").split(" -- ")
$extra_args = both_args[1].split(/\s+/)
opts.parse!(both_args[0].split(/\s+/))

Then, in the second code/context, you could do

other_opts = OptionParser.new do |opts|
    # set up the other OptionParser here
end

other_opts.parse!($extra_args)

Alternatively (and this is probably the "more proper" way to do this), you could simply use OptionParser#parse (no exclamation point), which doesn't remove the command-line switches from the $* array, and make sure that there aren't options defined the same in both sets. I would advise against modifying the $* array by hand, since it makes your code harder to understand if you are only looking at the second part, but you could do that. You would have to ignore invalid options in this case:

begin
    opts.parse
rescue OptionParser::InvalidOption
    puts "Warning: Invalid option"
end

Hope this helps.

EDIT:

The second method doesn't actually work, as was pointed out in a comment. However, if you have to modify the $* array anyway, you can do this instead:

tmp = Array.new

while($*.size > 0)
    begin
        opts.parse!
    rescue OptionParser::InvalidOption => e
        tmp.push(e.to_s.sub(/invalid option:\s+/,''))
    end
end

tmp.each { |a| $*.push(a) }

It's more than a little bit hack-y, but it should do what you want.

David Hollman
Yes, the delimiter approach seems a viable way, I just hoped it to be nicer than tweaking arrays, with join and split; perhaps directly supported by OptionParser.The problem with your alternative solution is, when that exception is raised, the whole processing is aborted, so subsequent good arguments would be skipped too. Check this: `ruby -roptparse -e 'begin OptionParser.new {|o|o.on("--ok"){puts "OK"}}.parse *ARGV;rescue OptionParser::InvalidOption;warn "BAD";end' -- --bad --ok` # this says BAD although should say OK as well.
inger
Also, one of my usecases above is to wrap existing frameworks/systems like RSpec with minimal effort, something like calling Spec::Runner.run_examples, which does optparsing internally. So, unfortunately this means I do have rewrite ARGV (even though it's constant and agree with you to avoid it if possible)
inger