[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

OptionParser order of arguments

Tom

1/15/2008 6:10:00 AM

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

1 Answer

Nobuyoshi Nakada

1/15/2008 6:25:00 AM

0

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
> end
> end
> MyClass.new(ARGV)

--
Nobu Nakada