# About GetoptLong and exceptions

**URL:** <https://www.ruby-forum.com/t/about-getoptlong-and-exceptions/64841>\
**Category:** Ruby\
**Created:** [February 9, 2006, 12:09am UTC](https://www.ruby-forum.com/t/about-getoptlong-and-exceptions/64841 "2006-02-09T00:09:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cowboy\_x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d78d45/32.png) [@cowboy\_x](https://www.ruby-forum.com/u/cowboy_x)\
**Post date:** [February 9, 2006, 12:09am UTC](https://www.ruby-forum.com/t/about-getoptlong-and-exceptions/64841/1 "2006-02-09T00:09:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cowboy\_x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d78d45/32.png) [@cowboy\_x](https://www.ruby-forum.com/u/cowboy_x)\
**Post date:** [February 9, 2006, 1:18am UTC](https://www.ruby-forum.com/t/about-getoptlong-and-exceptions/64841/2 "2006-02-09T01:18:30Z")

</div>

[http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/getoptlong.doc](http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/getoptlong.doc)

## 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

```

---

<div class="post-metadata">

**Author:** ![cowboy\_x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d78d45/32.png) [@cowboy\_x](https://www.ruby-forum.com/u/cowboy_x)\
**Post date:** [February 9, 2006, 11:18am UTC](https://www.ruby-forum.com/t/about-getoptlong-and-exceptions/64841/3 "2006-02-09T11:18:34Z")

</div>

“Dr.Evil” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> [http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/getoptlong.doc](http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/getoptlong.doc)
> 
> ## Quiet flag

Thanks!
