# Getoptlong question

**URL:** <https://www.ruby-forum.com/t/getoptlong-question/77843>\
**Category:** Ruby\
**Created:** [March 7, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/getoptlong-question/77843 "2006-03-07T15:40:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 7, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/getoptlong-question/77843/1 "2006-03-07T15:40:48Z")

</div>

This has been bugging me for weeks now, intermittently.

By all accounts I’ve read, this getoptlong code should work

require ‘getoptlong’

opts = GetoptLong.new(  
[“–devel”,“-d”,GetoptLong::NO\_ARGUMENT],  
[“–getpass”,“-g”,GetoptLong::NO\_ARGUMENT],  
[“–debug”,“-D”,GetoptLong::NO\_ARGUMENT],  
[“–verbose”,“-V”,GetoptLong::NO\_ARGUMENT]  
)

server = ‘[http://192.168.10.16/](http://192.168.10.16/)’  
verbose = false  
opts.each { |o,a| p o }  
opts.each { |o,a|  
case o  
when ‘–devel’  
server = ‘[http://192.168.20.56/](http://192.168.20.56/)’  
when ‘–getpass’  
puts “this option does nothing”  
when ‘–debug’: $DEBUG = true  
when ‘–verbose’: verbose = true  
end  
}  
p server  
p $DEBUG  
p verbose

This is the output I expect when I run it:

“–devel”  
“–debug”  
“–verbose”  
“[http://192.168.20.56/](http://192.168.20.56/)”  
true  
true

This is the output I get:

“–devel”  
“–debug”  
“–verbose”  
“[http://192.168.10.16/](http://192.168.10.16/)”  
false  
false

What am I missing?

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 7, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/getoptlong-question/77843/2 "2006-03-07T16:05:39Z")

</div>

> 

2006/3/7, Matt R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> ["--debug","-D",GetoptLong::NO_ARGUMENT],
> when '--getpass'
> 
> ```
> 
> “–debug”  
> “–verbose”  
> “[http://192.168.10.16/](http://192.168.10.16/)”  
> false  
> false
> 
> What am I missing?

Insert a “p a” in the second opts.each block and see what happens. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 7, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/getoptlong-question/77843/3 "2006-03-07T16:05:45Z")

</div>

On Tue, 7 Mar 2006, Matt R. wrote:

> ```
> ["--debug","-D",GetoptLong::NO_ARGUMENT],
> when '--getpass'
> 
> ```
> 
> “–debug”  
> “–verbose”  
> “[http://192.168.10.16/](http://192.168.10.16/)”  
> false  
> false
> 
> What am I missing?

harp:~ \> diff -ubB [a.rb.org](http://a.rb.org) a.rb  
— [a.rb.org](http://a.rb.org) 2006-03-07 07:56:07.000000000 -0700  
+++ a.rb 2006-03-07 07:58:12.000000000 -0700  
@@ -9,7 +9,7 @@

server = ‘[http://192.168.10.16/](http://192.168.10.16/)’  
verbose = false  
-opts.each { |o,a| p o }  
+#opts.each { |o,a| p o }  
opts.each { |o,a|  
case o  
when ‘–devel’

harp:~ \> ruby – a.rb --debug --devel --verbose  
“[http://192.168.20.56/](http://192.168.20.56/)”  
true  
true

each is destructive - as most argv parsers are.

hth.

-a

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 7, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/getoptlong-question/77843/4 "2006-03-07T16:24:06Z")

</div>

On Wed, 8 Mar 2006, removed\_email\_address@domain.invalid wrote:

> server = ‘[http://192.168.10.16/](http://192.168.10.16/)’  
> true  
> true
> 
> each is destructive - as most argv parsers are.

Oh, fer cryin’ out loud I’m an idiot.

> hth.

Certainly did. Thanks.

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 7, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/getoptlong-question/77843/5 "2006-03-07T16:33:15Z")

</div>

On Wed, 8 Mar 2006, Matt R. wrote:

> > each is destructive - as most argv parsers are.
> 
> Oh, fer cryin’ out loud I’m an idiot.

welcome to the club! 😉

-a

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 7, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/getoptlong-question/77843/6 "2006-03-07T16:20:58Z")

</div>

Matt R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> What am I missing?

GetoptLong::each as well as GetoptLong::get consume the options. After  
the first opts.each{…} your opts are emtpy.

The docs don’t mention that, I think. I also had those troubles when I  
used opts.get == nil to check if no options were given.

Perhaps you should have a look at the optparse module in the stdlib.

Regards,  
Tassilo
