About GetoptLong and exceptions

E

Eric Jacoboni

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top