Forum: Ruby About GetoptLong and exceptions

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

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

    #{$0} [--bla | -b ] [--nbre=val | -n val | -c val]
          [--longueur[=val] | -l [val] ] fichier1 [fichier2]

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

   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

# 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

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)

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

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

> Quiet flag
> ----------

This topic is locked and can not be replied to.