On 20.06.2010 12:47, R.. Kumar wrote:
> Thomas Sawyer wrote:
>
>> OptionParser generally works fine --even for the items you mention. To
>> handle subcommands just pop off the top of ARGV before parsing (or for
>> more advanced use, find the first non-option item).
>
> I just found a great example of how you can use subcommands with
> OptionParser.
>
> http://stackoverflow.com/questions/2732894/using-rubys-optionparser-to-parse-sub-commands

I would modify that approach because it has the drawback of wasting CPU 
cycles creating all the unnecessary OptionParsers for subcommands that 
are not found in ARGV.  Rather, I'd do something more lazy, e.g.

global = OptionParser.new do |opts|
   # ...
end

subcommands = {
   'foo' => lambda {|argv| OptionParser.new do |opts|
      # ...
    end.parse! argv},
    # ...
    'baz' => lambda {|argv| OptionParser.new do |opts|
      # ...
    end.parse! argv},
  }

  global.order!
  subcommands[ARGV.shift][ARGV]

Of course you can also make "subcommands" a method which figures what to 
return based on the argument (command name).

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/