[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

optparse problem

scott_mccaskill

2/1/2005 10:05:00 PM

>From looking at the optparse example in optparse.rb, it seems like the
following should work, but it doesn't:

-----------------------------

require 'optparse'
require 'ostruct'

def parseArgs(args)
options = OpenStruct.new
options.number = 1

opts = OptionParser.new do |opts|
opts.on("--number", Integer, "an integer") do |n|
options.number = n
end
end

opts.parse!(args)[0]
options
end

options = parseArgs(ARGV)
p options

-----------------------------

> ruby opts.rb --number 42
<OpenStruct number=nil>

It works for Float (as in the example in optparse.rb), but not for
Integer; is this intentional? Using ruby 1.8.2 (2004-12-25)
[i386-mswin32]

Thanks,
Scott

1 Answer

Assaph Mehr

2/1/2005 11:02:00 PM

0


scott_mccask...@yahoo.com wrote:
> >From looking at the optparse example in optparse.rb, it seems like
the
> following should work, but it doesn't:
>
> -----------------------------
>
> require 'optparse'
> require 'ostruct'
>
> def parseArgs(args)
> options = OpenStruct.new
> options.number = 1
>
> opts = OptionParser.new do |opts|
> opts.on("--number", Integer, "an integer") do |n|

opts.on("--number INTEGER", Integer, "an integer") do |n|

you need to tell optparse that there is a mandatoty parameter for the
option.

> options.number = n
> end
> end
>
> opts.parse!(args)[0]
> options
> end
>
> options = parseArgs(ARGV)
> p options
>
> -----------------------------
>
> > ruby opts.rb --number 42
> <OpenStruct number=nil>
>
> It works for Float (as in the example in optparse.rb), but not for
> Integer; is this intentional? Using ruby 1.8.2 (2004-12-25)
> [i386-mswin32]
>
> Thanks,
> Scott