I need some help with OptionsParser

B

bcorrigan78

Hey all,

I must be doing something wrong here. Im calling the following file
this way:
ruby script.rb -s 1234
or
ruby script.rb -s1111

#Code snip
require 'optparse'
opts = OptionParser.new
opts.on("-s", "--size VAL", Integer) do |val|
puts "-s #{val}"
puts val
end

But..no matter what I do, I don't get any values out. I must be doing
something wrong??

Thanks!
Brian
PS: The documentation really isn't too clear on this...
 
K

Keith Fahlgren

Hey all,

I must be doing something wrong here. Im calling the following file
this way:
ruby script.rb -s 1234
or
ruby script.rb -s1111

#Code snip
require 'optparse'
opts = OptionParser.new
opts.on("-s", "--size VAL", Integer) do |val|
puts "-s #{val}"
puts val
end

I think the "Integer" is unnecessary. Here's how I usually use it:

opts.on("-p", "--prefix NAME", "REQUIRED: Set the prefix NAME (like
'ldpg').") {|p| $prefix = p}


HTH,
Keith
 
B

bcorrigan78

Am I calling it wrong?

ruby test.rb -p test
or
ruby test.rb -ptest

still put nothing :(
 
K

Keith Fahlgren

Not working on my end.. Still PUTS nothing?

Are you parsing them?

keith@devel /work/tools/keith/bin/oneoff $ cat ./rubytalk.rb
#!/usr/bin/env ruby
require 'optparse'
opts = OptionParser.new
opts.on("-s", "--size VAL", "Something to describe size") {|s| puts
"Size is: #{s}"}
opts.parse! # the important part...

keith@devel /work/tools/keith/bin/oneoff $ ./rubytalk.rb -s "Very Big"
Size is: Very Big


HTH,
Keith
 
B

bcorrigan78

Hey.. Well, I was.. There really must be something up here.

Code: (test.rb)
require 'optparse'
opts = OptionParser.new
opts.on("-s", "--size VAL", "Something to describe size") {|s| puts
"Size is: #{s}"}
opts.parse! # the important part...

Result:
C:\temp>ruby test.rb -s "Very Big"


C:\temp>
 
A

ara.t.howard

Hey.. Well, I was.. There really must be something up here.

Code: (test.rb)
require 'optparse'
opts = OptionParser.new
opts.on("-s", "--size VAL", "Something to describe size") {|s| puts
"Size is: #{s}"}
opts.parse! # the important part...

Result:
C:\temp>ruby -- test.rb -s "Very Big"
^^
^^
^^

you are passing '-s' to ruby, not test.rb. the '--' tells ruby to stop
processing command line options.

-a
 
B

bcorrigan78

Nada.. This isn't working either:

c:\temp>test.rb -s "Very Big"

c:\temp>

(But it kind of rules out the problem being the -- though right?)
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top