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 Atmatzidis

···

-----------------------------
Pharmacy Student at VFU, Brno
mailing lists: ml@convalesco.org

personal info: http://about.me/atmosx

The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."

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.

···

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

Hello,

···

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

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.

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

Thanks for the reply, worked fine!

Regards!

Panagiotis Atmatzidis
-----------------------------
Pharmacy Student at VFU, Brno
mailing lists: ml@convalesco.org

personal info: http://about.me/atmosx

The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."