Getoptlong question

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

This is the output I get:

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

What am I missing?

···

--
--------------------------------------------------------------------------
Matt Rose mattrose@folkwolf.net Visit my blog! http://folkwolf.net
He's a short-sighted vegetarian cop with acid for blood. She's a
green-fingered punk lawyer who can talk to animals. They fight crime!

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?

Insert a "p a" in the second opts.each block and see what happens. :slight_smile:

Kind regards

robert

···

2006/3/7, Matt Rose <mattrose@folkwolf.net>:

--
Have a look: Robert K. | Flickr

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/&#39;
  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/&quot;
true

each is destructive - as most argv parsers are.

hth.

-a

···

On Tue, 7 Mar 2006, Matt Rose wrote:

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/&#39;
verbose = false
opts.each { |o,a| p o }
opts.each { |o,a|
       case o
               when '--devel'
      server = 'http://192.168.20.56/&#39;
               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/&quot;
true

This is the output I get:

"--devel"
"--debug"
"--verbose"
"http://192.168.10.16/&quot;
false

What am I missing?

--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama
cat a.rb

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

This has been bugging me for weeks now, intermittently.

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/&#39;
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/&quot;
true

each is destructive - as most argv parsers are.

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

hth.

Certainly did. Thanks.

···

On Wed, 8 Mar 2006, ara.t.howard@noaa.gov wrote:

On Tue, 7 Mar 2006, Matt Rose wrote:

-a

--
--------------------------------------------------------------------------
Matt Rose mattrose@folkwolf.net Visit my blog! http://folkwolf.net
He's a time-tossed coffee-fuelled vampire hunter on a search for his
missing sister. She's a hard-bitten goth traffic cop looking for love
in all the wrong places. They fight crime!

welcome to the club! :wink:

-a

···

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.

--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama