Forum: Ruby About GetoptLong and exceptions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric J. (Guest)
on 2006-02-09 01:09
(Received via mailing list)
Hi,

I try to play with GetoptLong but i don't know how to handle
exceptions for this class (i've no found any documentation on
this). Here my toy snippet :


=-=-=-=-=-=-=-=-=-=-=-=-
require 'getoptlong'

def print_usage
  STDERR.puts <<-FIN
  Usage:

    #{$0} [--bla | -b ] [--nbre=val | -n val | -c val]
          [--longueur[=val] | -l [val] ] fichier1 [fichier2]
  FIN
  exit(1)
end

options = GetoptLong.new(
                 ["--bla", "-b",  GetoptLong::NO_ARGUMENT],
                 ["--nbre", "-n", "-c", GetoptLong::REQUIRED_ARGUMENT],
                 ["--longueur", "-l", GetoptLong::OPTIONAL_ARGUMENT])


begin
   options.each do |opt, arg|
      case opt
         when "-b"
           bla = true
         when "-n"
           cpteur = arg
         when "-l"
           longueur = (arg ? arg : 10)
       end  # case
   end # each
rescue Exception
   print_usage
end

# Now, parsing ARGV
(...)
=-=-=-=-=-=-=-=-=-=-=-=-

So, i expect a command line as "ruby toy.rb -z" would only print the
"Usage" message. But here's what i get:

toy.rb: invalid option -- z
Usage:

toy.rb [--bla | -b ] [--nbre=val | -n val | -c val]
      [--longueur[=val]?| -l [val] ] fichier1 [fichier2]


Where the "toy.rb: invalid option -- z" comes from? Unless i have not
fully understood the Ruby exception mechanism, it seems that my rescue
clause would handle all exceptions, isn't it ?

Any clue?
Dr.Evil (Guest)
on 2006-02-09 02:18
(Received via mailing list)
http://www.sra.co.jp/people/m-kasahr/ruby/getoptlo...

Quiet flag
----------
When an error is caused, an error message is output to standard error
by default.  Seting the `qiuet' flag, all error messages are
suppressed.

    parser.quiet = TRUE
Eric J. (Guest)
on 2006-02-09 12:18
(Received via mailing list)
"Dr.Evil" <removed_email_address@domain.invalid> writes:

> http://www.sra.co.jp/people/m-kasahr/ruby/getoptlo...
>
> Quiet flag
> ----------

Thanks!
This topic is locked and can not be replied to.