optparse problem

  • Thread starter scott_mccaskill
  • Start date
S

scott_mccaskill

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
 
A

Assaph Mehr

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
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top