# Command-line option parsing

**URL:** https://rubytalk.org/t/command-line-option-parsing/23639
**Category:** ruby-talk
**Created:** [27 December 2005 23:37 UTC](https://rubytalk.org/t/command-line-option-parsing/23639 "2005-12-27T23:37:53Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![Eric\_J\_Roode](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@Eric\_J\_Roode](https://rubytalk.org/u/Eric_J_Roode)
#### Post date: [27 December 2005 23:37 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/1 "2005-12-27T23:37:53Z")

</div>

Greetings,

&nbsp;&nbsp;&nbsp;&nbsp;I'm new to ruby (from Perl). I notice there are several command-line  
options parsing modules. The two that stuck out were GetoptLong and  
OptionsParser. GetoptLong suffers from being "the same old Getopt".  
OptionsParser claims to be "more advanced and more ruby-like", but has  
extremely poor documentation.

&nbsp;&nbsp;&nbsp;&nbsp;Are these the best two modules out there? Is there an emerging  
"standard" module? (In Perl, Getopt::Long is by far the most commonly-used  
module).

(I apologize if this is a FAQ; I read the FAQ and did a google scan of past  
articles. Most of what I found was authors announcing their own options-  
parsing modules).

Thanks in advance,

> **···**
>
> --  
> Eric Roode

---

<div class="post-metadata">

### Author: ![Lou\_Vanek](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@Lou\_Vanek](https://rubytalk.org/u/Lou_Vanek)
#### Post date: [28 December 2005 00:09 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/2 "2005-12-28T00:09:04Z")

</div>

i like CommandLine::OptionParser  
([http://rubyforge.org/docman/view.php/632/170/index.html\](http://rubyforge.org/docman/view.php/632/170/index.html%5C))  
because it's documentation is better than average as well  
as its features.  
it doesn't appear to be the ruby standard, however, but for  
in-house projects there's a lot to like.

Eric J. Roode wrote:

> **···**
>
> > Greetings,
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;I'm new to ruby (from Perl). I notice there are several command-line options parsing modules. The two that stuck out were GetoptLong and OptionsParser. GetoptLong suffers from being "the same old Getopt". OptionsParser claims to be "more advanced and more ruby-like", but has extremely poor documentation.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Are these the best two modules out there? Is there an emerging "standard" module? (In Perl, Getopt::Long is by far the most commonly-used module).
> > 
> > (I apologize if this is a FAQ; I read the FAQ and did a google scan of past articles. Most of what I found was authors announcing their own options-  
> > parsing modules).
> > 
> > Thanks in advance,

---

<div class="post-metadata">

### Author: ![mathew](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@mathew](https://rubytalk.org/u/mathew)
#### Post date: [28 December 2005 04:17 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/3 "2005-12-28T04:17:54Z")

</div>

Eric J. Roode wrote:

> GetoptLong suffers from being "the same old Getopt".

Well, what do you expect from an options parser? It's pretty much a solved problem, and using the standard getopt algorithms has the advantage that users know what to expect.

mathew

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<URL:[http://www.pobox.com/~meta/&gt](http://www.pobox.com/~meta/&gt);  
> My parents went to the lost kingdom of Hyrule  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and all I got was this lousy triforce.

---

<div class="post-metadata">

### Author: ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)
#### Post date: [28 December 2005 13:37 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/4 "2005-12-28T13:37:54Z")

</div>

I used to use GetopLong in the beginning but switched to OptionParser. Agreee, the documentation could be better but if you look carefully at the example given in RDoc you get pretty much everything you need. I'm not sure about a standard parser but my impression was that OptionParser is more widely used. But I may be wrong here.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Eric J. Roode \<sdn.girths00869@zoemail.net\> wrote:
> 
> > Greetings,
> > 
> > &nbsp;&nbsp;&nbsp;I'm new to ruby (from Perl). I notice there are several  
> > command-line options parsing modules. The two that stuck out were  
> > GetoptLong and OptionsParser. GetoptLong suffers from being "the  
> > same old Getopt". OptionsParser claims to be "more advanced and more  
> > ruby-like", but has extremely poor documentation.
> > 
> > &nbsp;&nbsp;&nbsp;Are these the best two modules out there? Is there an emerging  
> > "standard" module? (In Perl, Getopt::Long is by far the most  
> > commonly-used module).

---

<div class="post-metadata">

### Author: ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)
#### Post date: [28 December 2005 14:23 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/5 "2005-12-28T14:23:48Z")

</div>

Yikes.  
That is old documenetation and the link on Rubyforge was to be replaced eons  
ago. The new docs are at:  
&nbsp;&nbsp;[http://rubyforge.org/docman/view.php/632/233/posted-docs.index.html](http://rubyforge.org/docman/view.php/632/233/posted-docs.index.html)

There you will find the option parser docs after Application section. For  
the most part, if you use the CommandLine::Application class, you don't need  
to get  
your hands that dirty with CommandLine::OptionParser.

If you have any questions, just post a question or send me  
a private email. I plan to post a video on the usage this week. Maybe a  
rails like video will generate more interest. 🙂

> **···**
>
> On 12/27/05, Lou Vanek \<vanek@acd.net\> wrote:
> 
> > i like CommandLine::OptionParser  
> > ([http://rubyforge.org/docman/view.php/632/170/index.html\](http://rubyforge.org/docman/view.php/632/170/index.html%5C))  
> > because it's documentation is better than average as well  
> > as its features.  
> > it doesn't appear to be the ruby standard, however, but for  
> > in-house projects there's a lot to like.
> 
> --  
> Jim Freeze

---

<div class="post-metadata">

### Author: ![Steve\_Litt](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Steve\_Litt](https://rubytalk.org/u/Steve_Litt)
#### Post date: [28 December 2005 15:20 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/6 "2005-12-28T15:20:13Z")

</div>

Does OptionParser come with standard Ruby?

SteveT

Steve Litt

> **[Troubleshooters.Com](http://www.troubleshooters.com)**
>
> The process and mindset of troubleshooting.

slitt@troubleshooters.com

> **···**
>
> On Wednesday 28 December 2005 08:37 am, Robert Klemme wrote:
> 
> > Eric J. Roode \<sdn.girths00869@zoemail.net\> wrote:  
> > \> Greetings,  
> > \>  
> > \> I'm new to ruby (from Perl). I notice there are several  
> > \> command-line options parsing modules. The two that stuck out were  
> > \> GetoptLong and OptionsParser. GetoptLong suffers from being "the  
> > \> same old Getopt". OptionsParser claims to be "more advanced and more  
> > \> ruby-like", but has extremely poor documentation.  
> > \>  
> > \> Are these the best two modules out there? Is there an emerging  
> > \> "standard" module? (In Perl, Getopt::Long is by far the most  
> > \> commonly-used module).
> > 
> > I used to use GetopLong in the beginning but switched to OptionParser.  
> > Agreee, the documentation could be better but if you look carefully at the  
> > example given in RDoc you get pretty much everything you need. I'm not  
> > sure about a standard parser but my impression was that OptionParser is  
> > more widely used. But I may be wrong here.
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

### Author: ![Eric\_J\_Roode](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@Eric\_J\_Roode](https://rubytalk.org/u/Eric_J_Roode)
#### Post date: [28 December 2005 22:52 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/7 "2005-12-28T22:52:54Z")

</div>

mathew \<meta@pobox.com\> wrote in news:wjosf.9901$9e.8623  
@tornado.texas.rr.com:

> Eric J. Roode wrote:
> 
> > GetoptLong suffers from being "the same old Getopt".
> 
> Well, what do you expect from an options parser? It's pretty much a  
> solved problem, and using the standard getopt algorithms has the  
> advantage that users know what to expect.

Well... that's not \*bad\*, but I guess I had expected something a bit more  
ruby-like. Whatever that means. 🙂

> **···**
>
> --  
> Eric  
> `$=`;$\_=\%!;($\_)=/(.)/;$==++$|;($.,$/,$,$\,$",$;,$^,$#,$~,$\*,$:,@%)=(  
> $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;  
> $\_++;$\_++;($\_,$\,$,)=($~.$"."$;$/$%[$?]$\_$\$,$:$%[$?]",$"&$~,$#,);$,++  
> ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.\>&$=`

---

<div class="post-metadata">

### Author: ![Eric\_J\_Roode](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@Eric\_J\_Roode](https://rubytalk.org/u/Eric_J_Roode)
#### Post date: [28 December 2005 23:04 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/8 "2005-12-28T23:04:26Z")

</div>

"Robert Klemme" \<bob.news@gmx.net\> wrote in  
news:41fiouF1e6diiU1@individual.net:

> I used to use GetopLong in the beginning but switched to OptionParser.  
> Agreee, the documentation could be better but if you look carefully at  
> the example given in RDoc you get pretty much everything you need.  
> I'm not sure about a standard parser but my impression was that  
> OptionParser is more widely used. But I may be wrong here.

I disagree about the example being all you need.

Here's the first option in the example:

&nbsp;&nbsp;&nbsp;&nbsp;# Mandatory argument.  
&nbsp;&nbsp;&nbsp;&nbsp;opts.on("-r", "--require LIBRARY",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Require the LIBRARY before executing your script") do |lib|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.library \<\< lib  
&nbsp;&nbsp;&nbsp;&nbsp;end

I am new to ruby, so I don't know what "options.library \<\< lib" means. I  
thought that that introduced a here-doc string.

The second example:

&nbsp;&nbsp;&nbsp;&nbsp;# Optional argument; multi-line description.  
&nbsp;&nbsp;&nbsp;&nbsp;opts.on("-i", "--inplace [EXTENSION]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Edit ARGV files in place",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" (make backup if EXTENSION supplied)") do |ext|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

It took me a lot of staring at the code before it dawned on me that it  
was the \*brackets\* that made it an optional argument.

The final example:

&nbsp;&nbsp;&nbsp;&nbsp;# Another typical switch to print the version.  
&nbsp;&nbsp;&nbsp;&nbsp;opts.on\_tail("--version", "Show version") do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts OptionParser::Version.join('.')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
&nbsp;&nbsp;&nbsp;&nbsp;end

I can't figure out how the on\_tail method differs from the on method or  
the on\_head method. Reading the source isn't helping. The example  
sucks.

The previous example has this comment:

&nbsp;&nbsp;&nbsp;&nbsp;# No argument, shows at tail. This will print an options summary.

"Shows at tail"??

At the end of the parse() method (and why is it self.parse, not just  
parse?), there is this:

&nbsp;&nbsp;&nbsp;opts.parse!(args)

I can't tell what function that performs for the class.

I can't tell how to make an option mandatory, how to configure it so that  
some options require certain others or conflict with certain others.

It looks like a fine, powerful module. But its lack of documentation  
makes it pretty much useless for anyone who doesn't already know how to  
use it.

I come from the Perl world. You simply do not release a module to Perl's  
CPAN unless it is thoroughly documented. The vast majority of CPAN  
modules have quite good documentation -- or at least, thorough  
documentation. I'm quite surprised to find that the situation is so  
different in the ruby world.

> **···**
>
> --  
> Eric  
> `$=`;$\_=\%!;($\_)=/(.)/;$==++$|;($.,$/,$,$\,$",$;,$^,$#,$~,$\*,$:,@%)=(  
> $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;  
> $\_++;$\_++;($\_,$\,$,)=($~.$"."$;$/$%[$?]$\_$\$,$:$%[$?]",$"&$~,$#,);$,++  
> ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.\>&$=`

---

<div class="post-metadata">

### Author: ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)
#### Post date: [28 December 2005 16:52 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/9 "2005-12-28T16:52:53Z")

</div>

Will there be stunt scenes with options and a high speed car chase?

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Jim Freeze \<jim@freeze.org\> wrote:
> 
> > If you have any questions, just post a question or send me  
> > a private email. I plan to post a video on the usage this week. Maybe  
> > a rails like video will generate more interest. 🙂

---

<div class="post-metadata">

### Author: ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)
#### Post date: [29 December 2005 00:30 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/10 "2005-12-29T00:30:48Z")

</div>

Hi Eric

You may want to give CommandLine a try.  
From the examples you give, this is how I would write an app:

require 'rubygems'  
require 'commandline'

class MyApp \< CommandLine::Application  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;# Mandatory argument  
&nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--require -r),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_description =\> "Require the LIBRARY "+  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"before executing your script",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "LIBRARY",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_arg,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_not\_found =\> required

&nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--inplace -i),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arity =\> [0,1],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_description =\> "Edit ARGV files in place",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "[EXTENSION]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_arg,  
&nbsp;&nbsp;end

&nbsp;&nbsp;def main  
&nbsp;&nbsp;&nbsp;&nbsp;#put your code here  
&nbsp;&nbsp;&nbsp;&nbsp;p opts  
&nbsp;&nbsp;end  
end#class MyApp

I don't know what #on\_tail does either.

> **···**
>
> --  
> Jim Freeze

---

<div class="post-metadata">

### Author: ![GS1](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@GS1](https://rubytalk.org/u/GS1)
#### Post date: [29 December 2005 05:37 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/11 "2005-12-29T05:37:54Z")

</div>

Eric J. Roode wrote:

> mathew \<meta@pobox.com\> wrote in news:wjosf.9901$9e.8623  
> @tornado.texas.rr.com:
> 
> \> Eric J. Roode wrote:  
> \>\> GetoptLong suffers from being "the same old Getopt".  
> \>  
> \> Well, what do you expect from an options parser? It's pretty much a  
> \> solved problem, and using the standard getopt algorithms has the  
> \> advantage that users know what to expect.
> 
> Well... that's not \*bad\*, but I guess I had expected something a bit more  
> ruby-like. Whatever that means. 🙂

That's a good expectation to have.

You mention OptionParser has poor documentation. While it's not  
complete, I find the example at [1] is quite adequate.

[1]  
[http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html)

Gavin

---

<div class="post-metadata">

### Author: ![GS1](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@GS1](https://rubytalk.org/u/GS1)
#### Post date: [29 December 2005 05:47 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/12 "2005-12-29T05:47:54Z")

</div>

Eric J. Roode wrote:

> I disagree about the example being all you need.

Please disregard my other reply in this thread. Your points are  
perfectly valid and I'll try to improve the documentation.

> Here's the first option in the example:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Mandatory argument.  
> &nbsp;&nbsp;&nbsp;&nbsp;opts.on("-r", "--require LIBRARY",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Require the LIBRARY before executing your script") do |lib|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.library \<\< lib  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> I am new to ruby, so I don't know what "options.library \<\< lib" means. I  
> thought that that introduced a here-doc string.

The documentation's not at fault here: knowledge of Ruby is assumed 🙂

> The second example:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Optional argument; multi-line description.  
> &nbsp;&nbsp;&nbsp;&nbsp;opts.on("-i", "--inplace [EXTENSION]",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Edit ARGV files in place",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" (make backup if EXTENSION supplied)") do |ext|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
> 
> It took me a lot of staring at the code before it dawned on me that it  
> was the \*brackets\* that made it an optional argument.

That will be easy to point out in the docs. Thanks.

> The final example:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Another typical switch to print the version.  
> &nbsp;&nbsp;&nbsp;&nbsp;opts.on\_tail("--version", "Show version") do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts OptionParser::Version.join('.')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> I can't figure out how the on\_tail method differs from the on method or  
> the on\_head method. Reading the source isn't helping. The example  
> sucks.

It means to show the "--version" option at the end (the "tail") of the  
options list.

> At the end of the parse() method (and why is it self.parse, not just  
> parse?), there is this:
> 
> &nbsp;&nbsp;&nbsp;opts.parse!(args)
> 
> I can't tell what function that performs for the class.

Hopefully I can clear this up. The difference between parse and parse!  
is that parse! swallows ARGV.

> I can't tell how to make an option mandatory, how to configure it so that  
> some options require certain others or conflict with certain others.

A "mandatory option" is a contradiction 🙂 You need to test for the  
presence of an option in your own code.

> I come from the Perl world. You simply do not release a module to Perl's  
> CPAN unless it is thoroughly documented. The vast majority of CPAN  
> modules have quite good documentation -- or at least, thorough  
> documentation. I'm quite surprised to find that the situation is so  
> different in the ruby world.

While not disagreeing in the least, I bet there are plenty of  
badly-documented Perl modules, but they're not the often-used ones, so  
they don't detract from the "Perl modules are well documented" meme.

Ruby is certainly worse, though!

Gavin

---

<div class="post-metadata">

### Author: ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)
#### Post date: [29 December 2005 10:47 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/13 "2005-12-29T10:47:54Z")

</div>

It seems to me that your posting pretty much confirms what I said: I didn't claim it's "all you need" but that it gets you pretty far, if you "look carefully". 🙂 Admittedly it's not so easy for someone new to Ruby...

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Eric J. Roode \<sdn.girths00869@zoemail.net\> wrote:
> 
> > "Robert Klemme" \<bob.news@gmx.net\> wrote in  
> > news:41fiouF1e6diiU1@individual.net:
> > 
> > > I used to use GetopLong in the beginning but switched to  
> > > OptionParser. Agreee, the documentation could be better but if you  
> > > look carefully at the example given in RDoc you get pretty much  
> > > everything you need. I'm not sure about a standard parser but my  
> > > impression was that OptionParser is more widely used. But I may be  
> > > wrong here.
> > 
> > I disagree about the example being all you need.

---

<div class="post-metadata">

### Author: ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)
#### Post date: [28 December 2005 17:09 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/14 "2005-12-28T17:09:09Z")

</div>

It will be action packed. There will be trains, rubies  
and high speed typing! 😉

> **···**
>
> On 12/28/05, Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > Will there be stunt scenes with options and a high speed car chase?
> 
> --  
> Jim Freeze

---

<div class="post-metadata">

### Author: ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)
#### Post date: [29 December 2005 07:42 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/15 "2005-12-29T07:42:54Z")

</div>

Gavin Sinclair wrote:

\<snip\>

> While not disagreeing in the least, I bet there are plenty of  
> badly-documented Perl modules, but they're not the often-used ones, so  
> they don't detract from the "Perl modules are well documented" meme.
> 
> Ruby is certainly worse, though!
> 
> Gavin

How's that shell documentation coming along?

Dan

---

<div class="post-metadata">

### Author: ![Rover\_Rhubarb](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@Rover\_Rhubarb](https://rubytalk.org/u/Rover_Rhubarb)
#### Post date: [3 May 2007 11:54 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/16 "2007-05-03T11:54:48Z")

</div>

> A "mandatory option" is a contradiction 🙂 You need to test for the  
> presence of an option in your own code.

This is a fair point, but for complex command lines we sometimes really  
need options that are not optional. For example, I'm working on a ruby  
xmltv-like scraper, that has a bunch of regular options, plus essential  
arguments like --config CONFIG\_FILE and --scraper SCRAPER\_FILE for  
specifying files that are necessary.

I want to use options for these rather than arguments because they are  
both files and I prefer that the user specify the option --config or  
--scraper rather than having to get the order of them right on the  
command line.

So you see, I need an "option" that is mandatory in addition to it  
having a mandatory argument.

Sure I could do it in my own code - but given this is a common case I'm  
tempted to extend OptionParser to do it so that I can get at the option  
description when I report that its missing you see? This is why it  
should probably be a feature of OptionParser - to tie the error message  
to the switch that's missing. I don't think I can be bothered wading  
into that code myself, so I guess I'll do something a little hackier -  
test my options and repeat the option descriptions.

But how about it Gavin?

Also - even if you don't - you should probably clearly document the  
fact this isn't available. The frequent use of "MANDATORY" in the  
example led me and probably many others to think it was bug that my  
mandatory arguments weren't reported as missing. It took me a while to  
realize that the word "argument" was key here. It also took me a while  
to realize that the square brackets were important.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![GS1](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@GS1](https://rubytalk.org/u/GS1)
#### Post date: [29 December 2005 12:07 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/17 "2005-12-29T12:07:54Z")

</div>

Daniel Berger wrote:

> How's that shell documentation coming along?

Not too well, just yet!

Gavin

---

<div class="post-metadata">

### Author: ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)
#### Post date: [3 May 2007 12:12 UTC](https://rubytalk.org/t/command-line-option-parsing/23639/18 "2007-05-03T12:12:36Z")

</div>

I agree completely.

The wording should be: mandatory option argument, not option.

And yes, it makes sense to /also/ make some options mandatory.

> **···**
>
> On 5/3/07, Rover Rhubarb \<rover.rhubarb@gmail.com\> wrote:
> 
> > \> A "mandatory option" is a contradiction 🙂 You need to test for the  
> > \> presence of an option in your own code.
> > 
> > This is a fair point, but for complex command lines we sometimes really  
> > need options that are not optional. For example, I'm working on a ruby  
> > xmltv-like scraper, that has a bunch of regular options, plus essential  
> > arguments like --config CONFIG\_FILE and --scraper SCRAPER\_FILE for  
> > specifying files that are necessary.
> > 
> > I want to use options for these rather than arguments because they are  
> > both files and I prefer that the user specify the option --config or  
> > --scraper rather than having to get the order of them right on the  
> > command line.
> > 
> > So you see, I need an "option" that is mandatory in addition to it  
> > having a mandatory argument.
> > 
> > Sure I could do it in my own code - but given this is a common case I'm  
> > tempted to extend OptionParser to do it so that I can get at the option  
> > description when I report that its missing you see? This is why it  
> > should probably be a feature of OptionParser - to tie the error message  
> > to the switch that's missing. I don't think I can be bothered wading  
> > into that code myself, so I guess I'll do something a little hackier -  
> > test my options and repeat the option descriptions.
> > 
> > But how about it Gavin?
> > 
> > Also - even if you don't - you should probably clearly document the  
> > fact this isn't available. The frequent use of "MANDATORY" in the  
> > example led me and probably many others to think it was bug that my  
> > mandatory arguments weren't reported as missing. It took me a while to  
> > realize that the word "argument" was key here. It also took me a while  
> > to realize that the square brackets were important.
> 
> --  
> Felipe Contreras
