# Needing Help with OptionParser

**URL:** <https://www.ruby-forum.com/t/needing-help-with-optionparser/125400>\
**Category:** Ruby\
**Created:** [January 8, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/needing-help-with-optionparser/125400 "2008-01-08T21:10:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 8, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/needing-help-with-optionparser/125400/1 "2008-01-08T21:10:37Z")

</div>

Hello All,

I’m hoping someone can help me get around a problem I’m having. I have  
a  
Ruby command-line program where I use OptionParser to parse the  
program’s  
options. This program is a wrapper for a bunch of other programs, each  
of  
which also accept command-line options. A typical run command would  
look  
like this:

#\> wrapper\_app --script-to-run MyScript --script-options “–input  
foo.txt–output  
bar.txt”

Where the information passed with --script-options is the options to use  
for  
the script to be ran. However, OptionParser in my main wrapper program  
attempts to parse the options given by --script-options and throws an  
error  
because they aren’t valid options for the main wrapper program. I was  
hoping that surrounding them in quotes would cause it to be seen as a  
single  
input string to --script-options but that doesn’t seem to be the case.  
Does  
anyone know how I can do what I’m trying to do?

Thanks in advance!!! – BTR

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 8, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/needing-help-with-optionparser/125400/2 "2008-01-08T22:35:36Z")

</div>

Alle martedì 8 gennaio 2008, Bryan R. ha scritto:

> bar.txt"
> 
> Where the information passed with --script-options is the options to use  
> for the script to be ran. However, OptionParser in my main wrapper program  
> attempts to parse the options given by --script-options and throws an error  
> because they aren’t valid options for the main wrapper program. I was  
> hoping that surrounding them in quotes would cause it to be seen as a  
> single input string to --script-options but that doesn’t seem to be the  
> case. Does anyone know how I can do what I’m trying to do?
> 
> Thanks in advance!!! – BTR

I had exactly the same problem once. With the help of some people on  
this  
list, I found the following solution:

require ‘optparse’

option\_parser = OptionParser.new do |o|  
o.on(’-a’, ‘–a-option’, ‘something’){}  
end

unknown = []

begin  
option\_parser.parse! ARGV  
rescue OptionParser::InvalidOption =\> e  
e.recover ARGV  
#recover just put the unknown option back into ARGV, so I extract it  
#again and put it into unknown  
unknown \<\< ARGV.shift

#if ARGV still contains some elements, and the first one doesn’t start  
#with a -, i.e is the argument for the unknown option, I remove it as  
#well  
unknown \<\< ARGV.shift if ARGV.size\>0 and ARGV.first[0…0]!=’-’

# go on with parsing

retry  
end

p unknown

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 8, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/needing-help-with-optionparser/125400/3 "2008-01-08T22:44:58Z")

</div>

Stefano,

Great! Thanks for the help. I’ll try this out as soon as I can!!!

Bryan
