From: nobu.nokada@... Date: 2004-11-12T10:51:02+09:00 Subject: Re: From getoptlong to optparse Hi, At Wed, 10 Nov 2004 23:23:31 +0900, Robert Klemme wrote in [ruby-talk:119714]: > Somehow I though Massimiliano will have multiple sub commands - apparently > that was wrong. Out of curiosity: What does the code above do in the > light of multiple sub commands? As far as I understand it, once a > subcommand is encountered options for this subcommand are defined and > would remain valid once another subcommand is seen. Is that right? Or is > this scheme incapable of dealing with multiple sub commands. Do you mean this? prog.rb --host example.com --port 1024 login --pass secret list --open Then loop subcommand parser until ARGV becomes empty using #order! instead of #parse!. --- prog.rb 2004-11-12 10:33:03.000000000 +0900 +++ mprog.rb 2004-11-12 10:47:16.000000000 +0900 @@ -6,5 +6,5 @@ require 'pp' # general options -opt = OptionParser.new do |opt| +general = OptionParser.new do |opt| opt.banner = "Usage: #{$0} [general options] subcommand [specific options]" opt.define(" general options:") @@ -30,6 +30,7 @@ missing = proc {|opt| begin cmd = nil - opt.order!(ARGV) {|cmd| opt.terminate} - opt = OptionParser.new(opt) do |opt| + (opt = general).order!(ARGV) {|cmd| opt.terminate} + begin + OptionParser.new(general) do |opt| unless cmd abort "#{$0}: command missing\n#{missing[opt]}" @@ -40,6 +41,6 @@ begin opt.define(" specific options for #{cmd}:") subopt[opt] - end - opt.parse!(ARGV) + end.order!(ARGV) {|cmd| opt.terminate} + end until ARGV.empty? rescue OptionParser::ParseError => e abort "#{e}\n#{opt}" -- Nobu Nakada