# Interested in writing a commandline option parser for ruby?

**URL:** <https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417>\
**Category:** Ruby\
**Created:** [May 11, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417 "2010-05-11T17:39:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [May 11, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417/1 "2010-05-11T17:39:36Z")

</div>

Don’t.

Just use Trollop.

Seriously. Don’t even think. Just obey.

OBEY.

Don’t write a “framework” to do it. Don’t write an overly-pedantic DSL.  
Don’t write a complicated OO system where for some INSANE reason you  
decide that inheritance NATURALLY should be a critical component of  
parsing the commandline.

That kind of behavior is NOT acceptable.

If I have to write more than one line of code per option, you’re doing  
it wrong. If I have to think about it, you’re doing it wrong. If I have  
to SUBCLASS something, for crying out loud, you’re doing it VERY wrong.

But don’t worry about it. Don’t fret. I’ve already done it right, just  
for you. With Trollop.

I have been perfecting Trollop for over three years. OVER THREE YEARS.  
That probably makes it the longest-lived project in all Ruby history.  
Trollop is not just epic. It is EPOCH. It is ART. It is PURE. And it is  
1.9-compatible.

Seriously, people. What is it about option parsing that makes you go  
crazy like this?

---

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [May 11, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417/2 "2010-05-11T19:53:40Z")

</div>

On Wed, May 12, 2010 at 12:39 AM, William M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Don’t.
> 
> Just use Trollop.
> 
> Seriously. Don’t even think. Just obey.
> 
> OBEY.

I have no idea what caused this rage of you, but would you kindly tell  
me what’s wrong with OptionParser (optparse.rb in stdlib)?

---

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [May 11, 2010, 10:24pm UTC](https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417/3 "2010-05-11T22:24:07Z")

</div>

On May 11, 2010, at 08:39 , William M. wrote:

> I have been perfecting Trollop for over three years. OVER THREE YEARS.  
> That probably makes it the longest-lived project in all Ruby history.  
> Trollop is not just epic. It is EPOCH. It is ART. It is PURE. And it is  
> 1.9-compatible.

hah! awesome.

---

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [May 12, 2010, 4:44am UTC](https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417/4 "2010-05-12T04:44:33Z")

</div>

Reformatted excerpts from Michael F.'s message of 2010-05-11:

> I have no idea what caused this rage of you,

Suboptimal software.

> but would you kindly tell me what’s wrong with OptionParser  
> (optparse.rb in stdlib)?

1. Too many positional arguments. Which is the correct version?

opts.on( ‘-f’, ‘–float NUM’, Float, “Convert to float” )  
opts.on( ‘-f’, ‘–float NUM’, “Convert to float”, Float )

If you have to look it up, optparse fails.

1. The use of blocks. 99% of the time you’re just throwing stuff in a  
hash anyways, in which case you now have to maintain the mapping between  
hash keys and arguments. The rest of the time you’re doing complicated  
stuff in the blocks, in which case you have code mixed in the middle of  
your argument list.

2. No notion of default arguments. You have to explicitly write them  
into your code, e.g. by using a hash and initializing it.

3. If optparse knows your argument is named ‘–float’, why doesn’t it  
automatically generate a short option ‘-f’? (Likewise, if optparse knew  
what your default value was, it could infer the type-checking for you.)

4. Complicated API. on\_head, on\_tail, make\_switch, etc.

5. I like for my help pages to be wrapped nicely, e.g.

Options:  
–delete, -d: Delete everything  
–restore, -r: Restore everything. Of course this doesn’t actually  
work. What’s gone is gone. But it might make the  
user  
feel hopeful for a second, and, after all, isn’t  
that  
valuable in and of itself? (default: true)  
–help, -h: Show this message

Optparse just makes a big blob.

That’s all I can think of now…

---

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [May 12, 2010, 5:58am UTC](https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417/5 "2010-05-12T05:58:54Z")

</div>

On Tue, May 11, 2010 at 11:39 PM, William M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just use Trollop.

would it be nice if you could include arguments(ARGV) and then infer  
the usage, so  
1 it would be included in -h display  
2 i wouldn’t have to create a separate banner for it

?

simple illustration: what if i need at least two args for the command  
mycopy source dest [dest2, dest3,…]

then mycopy -h should show at least  
syntax: mycopy source dest [dest,…]  
options:…

i haven’t lookuped the api though.  
overall, trollop is quite nifty/speedy.

thanks and best regards -botp

---

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [May 12, 2010, 7:08pm UTC](https://www.ruby-forum.com/t/interested-in-writing-a-commandline-option-parser-for-ruby/188417/6 "2010-05-12T19:08:03Z")

</div>

On Wed, May 12, 2010 at 11:44 AM, William M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â opts.on( ‘-f’, ‘–float NUM’, Float, “Convert to float” )  
> Â opts.on( ‘-f’, ‘–float NUM’, “Convert to float”, Float )
> 
> If you have to look it up, optparse fails.

The first one on first glance, but I’m using that library for years now.

> 1. The use of blocks. 99% of the time you’re just throwing stuff in a  
> hash anyways, in which case you now have to maintain the mapping between  
> hash keys and arguments. The rest of the time you’re doing complicated  
> stuff in the blocks, in which case you have code mixed in the middle of  
> your argument list.

The first one is true, it used to be nicer in 1.8 when people abused  
the block pipes 😉  
You can put longer code into methods and use them as blocks:  
o.on(‘-h’, ‘–help’, ‘read me’, &method(:help))  
See [ver/bin/ver at master · manveru/ver · GitHub](http://github.com/manveru/ver/blob/master/bin/ver#L167) as an  
example.

> 1. No notion of default arguments. You have to explicitly write them  
> into your code, e.g. by using a hash and initializing it.

You have to put your defaults somewhere, might as well be a Hash.

> 1. If optparse knows your argument is named ‘–float’, why doesn’t it  
> automatically generate a short option ‘-f’? (Likewise, if optparse knew  
> what your default value was, it could infer the type-checking for you.)

Because you’d rather not provide a short version for some stuff,  
especially if there are two options called --float-min and  
–float-max?  
Again, a default value doesn’t need to have anything to do with the  
accepted values.

> 1. Complicated API. on\_head, on\_tail, make\_switch, etc.

The API is actually very powerful, it takes a little effort to learn,  
but you can go very far with it.  
For example, I’ve implemented the ramaze binary using optparse so i  
can actually handle subcommands without code duplication:

> <https://github.com/manveru/ramaze/blob/modular-bin/lib/ramaze/bin.rb>

> <https://github.com/manveru/ramaze/blob/modular-bin/lib/ramaze/bin/start.rb>

> <https://github.com/manveru/ramaze/blob/modular-bin/lib/ramaze/bin/stop.rb>

> Optparse just makes a big blob.  
> Formatting can be controlled when initializing:  
> [http://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L784-791](http://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L784-791)  
> And it can be even more controlled when you do the actual output:  
> [http://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L359-371](http://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L359-371)

Not that I ever cared about that, I don’t even understand what you  
mean with big blob,  
just pointing out it might be possible to get your preferred formatting  
too 🙂

> That’s all I can think of now…

Thanks.
