Simple optparser problem

Hello,

I’m writing a small script to be handled with optparser. I’ve read the
documentation everything worked fine at first, then suddenly it stopped
accepting my flag parameters. The relevant part of the code is here:

class Morula
attr_accessor :database, :tableid
def initialize(database, tableid)
@database = database
@tableid = tableid
[…]
end
end

options = {}

optparse = OptionParser.new do |opts|
[…]
opts.on(’-d’, ‘–database’, ‘comments go here’) do |x|
options[:database] = x
end
[…]
end

[…]
if options[:database] == nil
db = db_default
else
db = options[:database].to_s
end
[…]
puts options[:database] # gives true instead of ‘x’

Is there something obvious I’m missing? I tried also adding ‘String’:
[…]
opts.on(’-d’, ‘–database’, String, ‘comments go here’) do |x|
options[:database] = x
end
[…]

Regards,

Panagiotis A.

When you write:

opts.on(’-d’, ‘–database’, ‘comments go here’) do |x|
options[:database] = x
end

‘x’ will be set to ‘true’, not the input value you expect. If you want
flags with values, you need to include a “VAL” placeholder:

opts.on(’-d VAL’, ‘–database VAL’, ‘comments go here’) do |x|
options[:database] = x
end

Then ‘x’ will hold the value after the -d flag.

Hello,

On 30 Νοε 2011, at 24:37 , Peter Lane wrote:

options[:database] = x
end

Then ‘x’ will hold the value after the -d flag.


Posted via http://www.ruby-forum.com/.

Thanks for the reply, worked fine!

Regards!

Panagiotis A.