[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

About GetoptLong and exceptions

Eric Jacoboni

2/8/2006 11:05:00 PM

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?
--
Eric Jacoboni, ne il y a 1442879654 secondes
2 Answers

Dr.Evil

2/9/2006 12:13:00 AM

0

http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/geto...

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 Jacoboni

2/9/2006 10:14:00 AM

0

"Dr.Evil" <akovbovich@gmail.com> writes:

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

Thanks!

--
Eric Jacoboni, ne il y a 1442920407 secondes