# GetoptLong example

**URL:** <https://www.ruby-forum.com/t/getoptlong-example/65026>\
**Category:** Ruby\
**Created:** [December 15, 2005, 1:39am UTC](https://www.ruby-forum.com/t/getoptlong-example/65026 "2005-12-15T01:39:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [December 15, 2005, 1:39am UTC](https://www.ruby-forum.com/t/getoptlong-example/65026/1 "2005-12-15T01:39:30Z")

</div>

Hi all,

Anyone have a short and easy GetoptLong example in Ruby? It’s not  
object oriented in Perl or C, so I’m in unfamiliar territory.

Thanks

SteveT

Steve L.

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

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [December 15, 2005, 5:13am UTC](https://www.ruby-forum.com/t/getoptlong-example/65026/2 "2005-12-15T05:13:13Z")

</div>

On 12/14/05, Steve L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> Anyone have a short and easy GetoptLong example in Ruby? It’s not  
> object oriented in Perl or C, so I’m in unfamiliar territory.

Funny I was just in the same position yesterday. A google of “ruby  
getoptlong” gave this as the second link, which is how I got on with  
it:

> **[Radar - O’Reilly](https://www.oreilly.com/radar/?page=2)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

Cheers,  
/Nick

---

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [December 15, 2005, 1:32pm UTC](https://www.ruby-forum.com/t/getoptlong-example/65026/3 "2005-12-15T13:32:59Z")

</div>

On 12/14/05, Nick S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 12/14/05, Steve L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi all,
> > 
> > Anyone have a short and easy GetoptLong example in Ruby? It’s not  
> > object oriented in Perl or C, so I’m in unfamiliar territory.

Have you tried OptionParser? It is fully OO and integrated with  
Application.

Yes, that was the sound of my own horn. 🙂

I know CommandLine cannot solve everyones needs, but I would be  
interested  
to hear about it if it doesn’t meet your needs. Maybe there is something  
I can change or add to the library.

My nefarious global take over plans are to see CommandLine - which  
has a Ruby flavor - be put in the stdlib and surplant the existing  
optparse  
and getoptlong parsers which use aging methodologies.

---

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [December 15, 2005, 2:48pm UTC](https://www.ruby-forum.com/t/getoptlong-example/65026/4 "2005-12-15T14:48:28Z")

</div>

On Thursday 15 December 2005 07:32 am, Jim F. wrote:

> Yes, that was the sound of my own horn. 🙂
> 
> I know CommandLine cannot solve everyones needs, but I would be  
> interested to hear about it if it doesn’t meet your needs. Maybe  
> there is something I can change or add to the library.
> 
> My nefarious global take over plans are to see CommandLine -  
> which has a Ruby flavor - be put in the stdlib and surplant the  
> existing optparse and getoptlong parsers which use aging  
> methodologies.

Hi Jim,

I came across OptionParser. I tried reading the documentation, but I  
saw no simple proof of concept to get me started. I therefore opted  
for GetoptLong. At least it’s not a total stranger, I’ve used it in  
C and Perl.

Perhaps I just came across the wrong documentation.

I’d like to have the OptionParser documentation contain a proof of  
concept, and a couple more examples that follow logically, without  
skipping steps.

Thanks

SteveT

Steve L.

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

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [December 15, 2005, 4:09pm UTC](https://www.ruby-forum.com/t/getoptlong-example/65026/5 "2005-12-15T16:09:13Z")

</div>

Hi Steve

On 12/15/05, Steve L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I came across OptionParser. I tried reading the documentation, but I  
> saw no simple proof of concept to get me started. I therefore opted  
> for GetoptLong. At least it’s not a total stranger, I’ve used it in  
> C and Perl.
> 
> Perhaps I just came across the wrong documentation.
> 
> I’d like to have the OptionParser documentation contain a proof of  
> concept, and a couple more examples that follow logically, without  
> skipping steps.

Is this the documentation that you are referring to that needs  
improvement:

[http://rubyforge.org/docman/view.php/632/233/posted-docs.index.html](http://rubyforge.org/docman/view.php/632/233/posted-docs.index.html)

Please help me out and post some comments on what you like and don’t  
like. I am still making improvements in the docs.

---

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [December 15, 2005, 4:03pm UTC](https://www.ruby-forum.com/t/getoptlong-example/65026/6 "2005-12-15T16:03:09Z")

</div>

On Thu, 15 Dec 2005, Steve L. wrote:

> I came across OptionParser. I tried reading the documentation, but I  
> saw no simple proof of concept to get me started. I therefore opted  
> for GetoptLong. At least it’s not a total stranger, I’ve used it in  
> C and Perl.
> 
> Perhaps I just came across the wrong documentation.
> 
> I’d like to have the OptionParser documentation contain a proof of  
> concept, and a couple more examples that follow logically, without  
> skipping steps.

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

is good, although I don’t like the use of ostruct. For a cleaner, yet  
extensive, construction see [Servalys – Alles over technologie](http://www.servalys.nl/scripts/mk) with its  
rdoc at [Servalys – Alles over technologie](http://www.servalys.nl/scripts/doc/files/mk.html)

---

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [December 15, 2005, 7:04pm UTC](https://www.ruby-forum.com/t/getoptlong-example/65026/7 "2005-12-15T19:04:11Z")

</div>

On Thursday 15 December 2005 10:06 am, Jim F. wrote:

> > I’d like to have the OptionParser documentation contain a proof  
> > Please help me out and post some comments on what you like and  
> > don’t like. I am still making improvements in the docs.

Hi Jim,

I’m pretty sure those are the docs I couldn’t fathom. IMHO what you  
need is a quickstart – all code, no theory. Start with the  
simplest possible program that the user can paste into his editor  
and then give him the command line to run, and show the expected  
output.

I think the idea is to get the user to say “hey, I can do this”.  
Once he’s hooked, he has incentive to dig into the incredibly rich  
array of tools you offer.

Personally, when I write documentation on [Troubleshooters.Com](http://Troubleshooters.Com), I  
always start with a “Hello World” and then, one feature at a time,  
I move from the known to the unknown (my wife’s a teacher :-).

Another thing. I usually show full programs (in blue background) so  
the user can paste them into his editor and run them immediately,  
and then the expected results in yellow background so the user can  
compare his results to the expected results. Once the user sees  
both the code and the results, he can then figure out why it works  
like it does, and he can also experiment with the code to learn  
even more.

For an example of how I write my documentation, see this:

> **[Ruby Basic Tutorial](http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm)**
>
> A tutorial on Ruby basics

Please keep in mind that I’m displaying this as an example of my  
documentation methods, not as an example of particularly good Ruby  
documentation. I wrote most of this material with less than a  
week’s Ruby experience, and before I signed up with this mailing  
list. In fact, a companion document called “Ruby the Right Way” was  
my reason for my “They say I write Ruby like Perl” post.

If any of you have the book “Samba Unleashed”, the chapters with my  
name on them are another good example of my documentation methods.

In summary – IMHO a quickstart would be the catalyst to greater  
acceptance of OptionParser. Use whole but tiny programs, without  
error checking and the like, to make your points. That way the user  
can cut and paste right onto his box and follow your examples on  
his own. Within 10 minutes the user understands the power of  
OptionParser and is ready to learn more.

Thanks

SteveT

Steve L.

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

removed\_email\_address@domain.invalid
