# Optparse: Arguments as Arguments Unexpectedness

**URL:** <https://www.ruby-forum.com/t/optparse-arguments-as-arguments-unexpectedness/168404>\
**Category:** Ruby\
**Created:** [May 20, 2009, 1:35pm UTC](https://www.ruby-forum.com/t/optparse-arguments-as-arguments-unexpectedness/168404 "2009-05-20T13:35:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [May 20, 2009, 1:35pm UTC](https://www.ruby-forum.com/t/optparse-arguments-as-arguments-unexpectedness/168404/1 "2009-05-20T13:35:08Z")

</div>

Hi,

I am trying to use the standard optionparser to read in some arguments,  
but one of those arguments is an argument itself. Is this a bug?

opt.rb:

require ‘optparse’  
OptionParser.new do |opts|  
opts.on(’-a’,’–args [ARGUMENTS]’,String,“arguments”) {|v| puts “Found:  
#{v}”}  
end.parse!

EXPECTED:  
$ ruby opt.rb -a “-d me”  
Found: -d me

ACTUAL:  
$ ruby opt.rb -a “-d me”  
Found:  
/usr/lib/ruby/1.8/optparse.rb:1445:in `complete': invalid option: -d me (OptionParser::InvalidOption) from /usr/lib/ruby/1.8/optparse.rb:1443:in`catch’  
from /usr/lib/ruby/1.8/optparse.rb:1443:in `complete' from /usr/lib/ruby/1.8/optparse.rb:1282:in`parse\_in\_order’  
from /usr/lib/ruby/1.8/optparse.rb:1249:in `catch' from /usr/lib/ruby/1.8/optparse.rb:1249:in`parse\_in\_order’  
from /usr/lib/ruby/1.8/optparse.rb:1243:in `order!' from /usr/lib/ruby/1.8/optparse.rb:1334:in`permute!’  
from /usr/lib/ruby/1.8/optparse.rb:1355:in `parse!’  
from opt.rb:3

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [May 20, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/optparse-arguments-as-arguments-unexpectedness/168404/2 "2009-05-20T14:28:12Z")

</div>

Ben W. wrote:

> I am trying to use the standard optionparser to read in some arguments,  
> but one of those arguments is an argument itself. Is this a bug?

I don’t think so. I think it’s an ambiguity because you have declared  
your -a flag to take an _optional_ argument.

Suppose your OptionParser also honoured the -d option. It would need to  
choose between “-a” with argument “-d me”, and “-a” with no argument  
followed by option “-d” with argument “me”. OptionParser is taking the  
view that anything which starts with a dash in that case is intended to  
be the next option on the line. Otherwise, the argument to the -a flag  
would hardly be optional if it always consumed the next string.

If your -a option _always_ takes an argument, then declare it as such,  
without the square brackets:

require ‘optparse’  
OptionParser.new do |opts|  
opts.on(’-a’,’–args ARGUMENTS’,String,“arguments”) {|v| puts “Found:  
#{v}”}  
end.parse!

$ ruby opt.rb -a “-d me”  
Found:  
-d me

Otherwise, your original code works as long as you include the flag and  
its argument in the same position.

$ ruby opt.rb “-a-d me”  
Found:  
-d me

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [May 20, 2009, 2:49pm UTC](https://www.ruby-forum.com/t/optparse-arguments-as-arguments-unexpectedness/168404/3 "2009-05-20T14:49:52Z")

</div>

Brian C. wrote:

> If your -a option _always_ takes an argument, then declare it as such,  
> without the square brackets:

Thanks for the quick, informative answer and sorry for the stupidity.  
ben
