Forum: Ruby getoptlong question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
44d5033f854ba9246875c840f88ef0d3?d=identicon&s=25 Matt Rose (Guest)
on 2006-03-07 15:40
(Received via mailing list)
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/'
verbose = false
opts.each { |o,a| p o }
opts.each { |o,a|
         case o
                 when '--devel'
 			server = '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/"
true
true

This is the output I get:

"--devel"
"--debug"
"--verbose"
"http://192.168.10.16/"
false
false

What am I missing?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-07 16:05
(Received via mailing list)
>
2006/3/7, Matt Rose <mattrose@folkwolf.net>:
>                          ["--debug","-D",GetoptLong::NO_ARGUMENT],
>                  when '--getpass'
>
> "--debug"
> "--verbose"
> "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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-07 16:05
(Received via mailing list)
On Tue, 7 Mar 2006, Matt Rose wrote:

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

harp:~ > diff -ubB a.rb.org a.rb
--- 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/'
  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/"
true
true


each is destructive - as most argv parsers are.

hth.

-a
Ea9c961a40599c37b750dc36df620623?d=identicon&s=25 Tassilo Horn (Guest)
on 2006-03-07 16:20
(Received via mailing list)
Matt Rose <mattrose@folkwolf.net> 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
44d5033f854ba9246875c840f88ef0d3?d=identicon&s=25 Matt Rose (Guest)
on 2006-03-07 16:24
(Received via mailing list)
On Wed, 8 Mar 2006, ara.t.howard@noaa.gov wrote:

> server = '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.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-07 16:33
(Received via mailing list)
On Wed, 8 Mar 2006, Matt Rose wrote:

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

welcome to the club!  ;-)


-a
This topic is locked and can not be replied to.