From: Stefano Crocco Date: 2006-11-15T05:31:28+09:00 Subject: Re: parsing unknown options unknown wrote: > On Tue, 14 Nov 2006, Stefano Crocco wrote: > >> >> - load the plugin >> - use OptionParser to rescan the options >> >> I'm still working on how to communicate the options recognized by the >> plugin to the main program. >> >> Stefano > > OptionParser already handles unknown options: > > begin > option_parser.parse! argv > rescue OptionParser::InvalidOption => e > # preverve unknown options > e.recover argv > end > > > -a Thanks for pointing this out. It's exactly what I was looking for, and I completely missed it. I've modified your code to store in a array the unrecognized options and their arguments, if any is provided: unknown=[] #this is the array where the unknown options will be put begin option_parser.parse! argv rescue OptionParser::InvalidOption => e e.recover argv #recover just put the unknown option back into argv, so I extract it again and put it into unknown unknown << argv.shift #if argv still contains some elements, and the first one doesn't start with a -, i.e is the argument for the unknown option, I remove it as well unknown << argv.shift if argv.size>0 and temp.first[0..0]!='-' # go on with parsing retry end puts unknown -- Posted via http://www.ruby-forum.com/.