Is there any way to kick off OptionParser several times in one Ruby program, each with different sets of options?
For 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 OptionParser 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.
The use cases are:
In a loosely coupled front-end 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.
Embedding some larger system like RSpec into my application, and I'd to simply pass a 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!
call will do 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.
I also needed the same... it took me a while but a relatively simple way has worked fine in the end.
"args" will contain all command line arguments without the ones already parsed into the "options" hash. I'm passing this "args" to an external program to be called from this ruby script.
Assuming the order in which the parsers will run is well defined, you can just store the extra options in a temporary global variable and run
OptionParser#parse!
on 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:Then, in the second code/context, you could do:
Alternatively, and this is probably the "more proper" way to do this, you could simply use
OptionParser#parse
, without the exclamation point, which won'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: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:It's more than a little bit hack-y, but it should do what you want.
For posterity, you can do this with the
order!
method:At this point,
args
has been modified - all known options were consumed and handled byoption_parser
- and can be passed to a different option parser:Obviously, this solution is order-dependent, but what you are trying to do is somewhat complex and unusual.
One thing that might be a good compromise is to just use
--
on the command line to stop processing. Doing that would leaveargs
with whatever followed--
, be that more options or just regular arguments.Parse options up until the first unknown option ... the block might be called multiple times, so make sure that is safe ...
I ran into a similar problem when I was writing a script that wrapped a ruby gem, which needed its own options with arguments passed to it.
I came up with the following solution in which it supports options with arguments for the wrapped tool. It works by parsing it through the first optparser, and separates what it can't use into a seperate array (which can be re-parsed again with another optparse).
Probably not the greatest or most efficient way of doing it, but it worked for me.
Another solution which relies on
parse!
having a side effect on the argument list even if an error is thrown.Let's define a method which tries to scan some argument list using a user defined parser and calls itself recursively when an InvalidOption error is thrown, saving the invalid option for later with eventual parameters: