# OptionParser can't tell me about non-options on command line

**URL:** <https://www.ruby-forum.com/t/optionparser-cant-tell-me-about-non-options-on-command-line/156281>\
**Category:** Ruby\
**Created:** [December 23, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/optionparser-cant-tell-me-about-non-options-on-command-line/156281 "2008-12-23T21:18:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alanp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@alanp](https://www.ruby-forum.com/u/alanp)\
**Post date:** [December 23, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/optionparser-cant-tell-me-about-non-options-on-command-line/156281/1 "2008-12-23T21:18:22Z")

</div>

I’d like to have a command line invocation of my ruby script that looks  
like the following:

Usage: vmci.rb [options] command args …

options:  
-h, -?, --help generate this usage message  
-l, --log-file FILE Identifies a log file to use.  
-e, --log-level LOGLEVEL desired level of logging.  
-x Bypass display of configuration

command  
one of the command recognized by vmci.rb

args  
any necessary arguments associated with the given command

I can’t figure out how to get at the “command” and “args” portions of  
the command line using OptionParser. Since a variable number of options  
may exist on the command line before the command, it’s not intuitive to  
me how to know which element of the ARGV array to use, or how to extract  
these values via OptionParser.

Can someone give me a little direction?

Thanks much.

* * *

alan partis  
thundernet development group

---

<div class="post-metadata">

**Author:** ![alanp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@alanp](https://www.ruby-forum.com/u/alanp)\
**Post date:** [December 23, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/optionparser-cant-tell-me-about-non-options-on-command-line/156281/2 "2008-12-23T21:48:42Z")

</div>

Alan Partis wrote:

> Can someone give me a little direction?

Nevermind … RTFM … I just went back to pickaxe 3rd edition and found  
that the call to OptionParser.parse returns an array containing the  
remaining elements from ARGV that weren’t processed as options.

* * *

alan partis  
thundernet development group

---

<div class="post-metadata">

**Author:** ![alanp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@alanp](https://www.ruby-forum.com/u/alanp)\
**Post date:** [December 24, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/optionparser-cant-tell-me-about-non-options-on-command-line/156281/3 "2008-12-24T12:55:26Z")

</div>

On 23.12.2008 21:40, Alan Partis wrote:

> Alan Partis wrote:
> 
> > Can someone give me a little direction?
> 
> Nevermind … RTFM … I just went back to pickaxe 3rd edition and found  
> that the call to OptionParser.parse returns an array containing the  
> remaining elements from ARGV that weren’t processed as options.

And if you use parse! (which is what I generally do) the original array  
is adjusted appropriately. This is important if you want to use ARGF  
for example. My typical pattern is

OptionParser.new do |opts|  
opts.on…  
end.parse! ARGV

# now ARGV has only non args left

Kind regards

robert
