getoptlong question

M

Matt Rose

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?
 
T

Tassilo Horn

Matt Rose said:
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
 
R

Robert Klemme

2006/3/7 said:
This has been bugging me for weeks now, intermittently.

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

require 'getoptlong'

opts =3D GetoptLong.new(
["--devel","-d",GetoptLong::NO_ARGUMENT],
["--getpass","-g",GetoptLong::NO_ARGUMENT],
["--debug","-D",GetoptLong::NO_ARGUMENT],
["--verbose","-V",GetoptLong::NO_ARGUMENT]
)

server =3D 'http://192.168.10.16/'
verbose =3D false
opts.each { |o,a| p o }
opts.each { |o,a|
case o
when '--devel'
server =3D 'http://192.168.20.56/'
when '--getpass'
puts "this option does nothing"
when '--debug': $DEBUG =3D true
when '--verbose': verbose =3D 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. :)

Kind regards

robert
 
A

ara.t.howard

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?

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
 
M

Matt Rose

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.

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

Certainly did. Thanks.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

GetoptLong usage 2
Command Line Arguments 0
GetoptLong#.quiet not doing what expected 2
GetoptLong order of options 3
About GetoptLong and exceptions 2
hash 6
printing from a file - beginner 0
hash 13

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top