OptionParser order of arguments

all-

i’ve had a lot of success working with OptionParser, but there is one
burr in the saddle- order of arguments parsed. sample code:

require ‘optparse’
class MyClass
def initialize(arguments)
@options = {}
opts = OptionParser.new
opts.on(’–some-param [PARAM]’) {|param| @options[:param] = param}
opts.on(’–print-param’) { puts @options[:param]}
opts.parse!
end
end
MyClass.new(ARGV)

put that in a file (opt_test.rb) and run the following:

ruby opt_test.rb --print-param --some-param tom
$ nil
ruby opt_test.rb --some-param tom --print-param
$ tom

obviously, it works as intended, but it does require that one pass
arguments in a particular order. anyone know of a way around this?

thanks all,
tom

Hi,

At Tue, 15 Jan 2008 15:09:58 +0900,
Tom M. wrote in [ruby-talk:287471]:

require ‘optparse’
class MyClass
def initialize(arguments)
@options = {}
print_param = false
opts = OptionParser.new
opts.on(’–some-param [PARAM]’) {|param| @options[:param] = param}
opts.on(’–print-param’) { print_param = true }
opts.parse!
puts @options[:param] if print_param

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs