[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

I need some help with OptionsParser

Brian Corrigan

3/20/2006 6:12:00 PM

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...

9 Answers

Keith Fahlgren

3/20/2006 6:21:00 PM

0

On Monday 20 March 2006 1:13 pm, bcorrigan78@gmail.com wrote:
> 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


Brian Corrigan

3/20/2006 6:32:00 PM

0

Not working on my end.. Still PUTS nothing?

Brian Corrigan

3/20/2006 6:33:00 PM

0

Am I calling it wrong?

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

still put nothing :(

Keith Fahlgren

3/20/2006 6:42:00 PM

0

On Monday 20 March 2006 1:33 pm, bcorrigan78@gmail.com wrote:
> 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


Brian Corrigan

3/20/2006 6:48:00 PM

0

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>

Ara.T.Howard

3/20/2006 7:17:00 PM

0

Brian Corrigan

3/20/2006 7:24:00 PM

0

That seems right! But alas :(

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


C:\temp>

Ara.T.Howard

3/20/2006 7:32:00 PM

0

Brian Corrigan

3/20/2006 7:57:00 PM

0

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?)
ara.t.howard@noaa.gov wrote:
> On Tue, 21 Mar 2006, bcorrigan78@gmail.com wrote:
>
> > That seems right! But alas :(
> >
> > C:\temp>ruby -- test.rb -s "Very Big"
>
> try
>
> ruby test.rb -- -s "Very Big"
>
> -a
> --
> share your knowledge. it's a way to achieve immortality.
> - h.h. the 14th dali lama