Hi,

At Wed, 16 Mar 2005 05:46:04 +0900,
Jos Backus wrote in [ruby-talk:133774]:
> > opt.parse! returns the remaining non-option arguments (which is in fact
> > just ARGV at this stage, with options removed), or nil upon failure. This
> > is propagated to the return value of ARGV.options. A common idiom seems to
> > be:
> > 
> >   ARGV.options do |opt|
> >     ...
> >     ...
> >     opt.parse!
> >   end or exit 1
> >   #   ^^^^^^^^^
> 
> I might use this. The downside here is that one doesn't know which option
> failed so it's hard to give a more specific error message. Perhaps the author
> will address this issue in due time.

Actually, #parse! just raises a exception, subclass of
OptionParser::Error, and ARGV.options does rescue it in the
given block and converts to nil.

  require 'optparse'

  client = nil
  ARGV.options do |opt|
    opt.on("-c CLIENT", String) do |s|
      puts "client: #{s}"
      client = s
    end
    begin
      opt.parse!
    rescue OptionParser::MissingArgument
      p $!.args                   # => "-c"
      abort
    end
  end

  puts "client is #{client}" if client

  puts "done"

-- 
Nobu Nakada