From: Robert Klemme Date: 2004-11-12T20:28:27+09:00 Subject: Re: From getoptlong to optparse schrieb im Newsbeitrag news:200411120150.iAC1otlO017036@sharui.nakada.niregi.kanuma.tochigi.jp... > 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 Yes, that's exactly the scenario I was thinking of ("login" and "list" as sub commands). > Then loop subcommand parser until ARGV becomes empty using > #order! instead of #parse!. I got lost somewhere in the diff below. :-) What eaxtly is the difference between "order!" and "parse!"? Kind regards robert > --- 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 > >