# OptionParser order of arguments

**URL:** <https://www.ruby-forum.com/t/optionparser-order-of-arguments/126074>\
**Category:** Ruby\
**Created:** [January 15, 2008, 7:10am UTC](https://www.ruby-forum.com/t/optionparser-order-of-arguments/126074 "2008-01-15T07:10:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 15, 2008, 7:10am UTC](https://www.ruby-forum.com/t/optionparser-order-of-arguments/126074/1 "2008-01-15T07:10:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 15, 2008, 7:26am UTC](https://www.ruby-forum.com/t/optionparser-order-of-arguments/126074/2 "2008-01-15T07:26:10Z")

</div>

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
