OptionParser order of arguments

T

Tom Metge

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
 
N

Nobuyoshi Nakada

Hi,

At Tue, 15 Jan 2008 15:09:58 +0900,
Tom Metge 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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top