Forum: Ruby I need some help with OptionsParser

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.
B1c2bbb71f4e3d8cca7723f028048663?d=identicon&s=25 bcorrigan78@gmail.com (Guest)
on 2006-03-20 19:13
(Received via mailing list)
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...
01d68aff859065b5cbc1cfc67cb16871?d=identicon&s=25 Keith Fahlgren (Guest)
on 2006-03-20 19:21
(Received via mailing list)
On Monday 20 March 2006 1:13 pm, bcorrigan78@gmail.com wrote:
> 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
B1c2bbb71f4e3d8cca7723f028048663?d=identicon&s=25 bcorrigan78@gmail.com (Guest)
on 2006-03-20 19:34
(Received via mailing list)
Not working on my end.. Still PUTS nothing?
B1c2bbb71f4e3d8cca7723f028048663?d=identicon&s=25 bcorrigan78@gmail.com (Guest)
on 2006-03-20 19:35
(Received via mailing list)
Am I calling it wrong?

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

still put nothing :(
01d68aff859065b5cbc1cfc67cb16871?d=identicon&s=25 Keith Fahlgren (Guest)
on 2006-03-20 19:41
(Received via mailing list)
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
B1c2bbb71f4e3d8cca7723f028048663?d=identicon&s=25 bcorrigan78@gmail.com (Guest)
on 2006-03-20 19:49
(Received via mailing list)
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>
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-20 20:17
(Received via mailing list)
On Tue, 21 Mar 2006, bcorrigan78@gmail.com wrote:

> 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
B1c2bbb71f4e3d8cca7723f028048663?d=identicon&s=25 bcorrigan78@gmail.com (Guest)
on 2006-03-20 20:28
(Received via mailing list)
That seems right!  But alas :(

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


C:\temp>
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-20 20:31
(Received via mailing list)
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
B1c2bbb71f4e3d8cca7723f028048663?d=identicon&s=25 bcorrigan78@gmail.com (Guest)
on 2006-03-20 21:00
(Received via mailing list)
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?)
This topic is locked and can not be replied to.