# GetoptLong example

**URL:** https://rubytalk.org/t/getoptlong-example/23301
**Category:** ruby-talk
**Created:** [15 December 2005 00:38 UTC](https://rubytalk.org/t/getoptlong-example/23301 "2005-12-15T00:38:45Z")
**Posts on this page:** 14
**Page:** 1

<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: [15 December 2005 00:38 UTC](https://rubytalk.org/t/getoptlong-example/23301/1 "2005-12-15T00:38:45Z")

</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 Litt  
[http://www.troubleshooters.com](http://www.troubleshooters.com)  
[slitt@troubleshooters.com](mailto:slitt@troubleshooters.com)

---

<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: [15 December 2005 01:42 UTC](https://rubytalk.org/t/getoptlong-example/23301/2 "2005-12-15T01:42:41Z")

</div>

Hi all,

I cruised the net, found an example, and turned it into a small  
program that works. So if anyone wants to use GetoptLong, here it  
is:

#!/usr/bin/ruby  
require "getoptlong"

getoptlong = GetoptLong.new(  
&nbsp;&nbsp;&nbsp;['--help', '-h', GetoptLong::NO\_ARGUMENT],  
&nbsp;&nbsp;&nbsp;['--short', '-s', GetoptLong::OPTIONAL\_ARGUMENT],  
&nbsp;&nbsp;&nbsp;['--offset', '-o', GetoptLong::REQUIRED\_ARGUMENT]  
)

def usage()  
&nbsp;&nbsp;puts "Usage:"  
&nbsp;&nbsp;puts "myprog [--help] [--short optional\_arg] [--offset  
required\_arg]"  
end

short\_arg = nil # declare local var to store arg  
offset\_arg = nil # declare local var to store arg

begin  
&nbsp;&nbsp;getoptlong.each do |opt, arg|  
&nbsp;&nbsp;&nbsp;&nbsp;case opt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "--help"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "dia help"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "--short"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Short option. Arg is=\>"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print arg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;short\_arg = arg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\<\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "--offset"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Offset option. Arg is=\>"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print arg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset\_arg = arg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\<\n"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
rescue StandardError=\>my\_error\_message  
&nbsp;&nbsp;puts  
&nbsp;&nbsp;puts my\_error\_message  
&nbsp;&nbsp;usage()  
&nbsp;&nbsp;puts  
&nbsp;&nbsp;exit  
end

unless short\_arg == ""  
&nbsp;&nbsp;puts  
&nbsp;&nbsp;print "Short arg is=\>"  
&nbsp;&nbsp;print short\_arg  
&nbsp;&nbsp;puts "\<"  
end

print "Offset arg is=\>"  
print offset\_arg  
puts "\<"

Steve Litt

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

slitt@troubleshooters.com

> **···**
>
> On Wednesday 14 December 2005 07:38 pm, Steve Litt 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.
> > 
> > Thanks
> > 
> > SteveT
> > 
> > Steve Litt  
> > [http://www.troubleshooters.com](http://www.troubleshooters.com)  
> > slitt@troubleshooters.com

---

<div class="post-metadata">

### Author: ![Nick\_Sieger](https://avatars.discourse-cdn.com/v4/letter/n/c57346/32.png) [@Nick\_Sieger](https://rubytalk.org/u/Nick_Sieger)
#### Post date: [15 December 2005 04:10 UTC](https://rubytalk.org/t/getoptlong-example/23301/3 "2005-12-15T04:10:13Z")

</div>

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

> **···**
>
> On 12/14/05, Steve Litt \<slitt@earthlink.net\> 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.

---

<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: [15 December 2005 12:32 UTC](https://rubytalk.org/t/getoptlong-example/23301/4 "2005-12-15T12:32:17Z")

</div>

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.

> **···**
>
> On 12/14/05, Nick Sieger \<nicksieger@gmail.com\> wrote:
> 
> > On 12/14/05, Steve Litt \<slitt@earthlink.net\> 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.
> 
> --  
> Jim Freeze

---

<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: [15 December 2005 22:50 UTC](https://rubytalk.org/t/getoptlong-example/23301/5 "2005-12-15T22:50:58Z")

</div>

Here is the above code re-writen using CommandLine as  
requested by Steve. It is a fully working example.

require 'rubygems'  
require 'commandline'

class App \< CommandLine::Application  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;synopsis "[-d] [-s [opt\_arg]] [-o req\_arg]"  
&nbsp;&nbsp;&nbsp;&nbsp;short\_description "A sample program"  
&nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(-short -s),  
&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;:arg\_description =\> "opt\_arg"  
&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 =\> nil

&nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--offset -o),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "req\_arg",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_args

&nbsp;&nbsp;&nbsp;&nbsp;option :help  
&nbsp;&nbsp;end

&nbsp;&nbsp;def main  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Short arg is=\>#{opt["--short"]}\<" unless opt["--short"].nil?  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Offset arg is=\>#{opt["--offset"]}\<"  
&nbsp;&nbsp;end

end#class App

> **···**
>
> On 12/14/05, Steve Litt \<slitt@earthlink.net\> wrote:
> 
> > I cruised the net, found an example, and turned it into a small  
> > program that works. So if anyone wants to use GetoptLong, here it  
> > is:
> > 
> > #!/usr/bin/ruby  
> > require "getoptlong"
> > 
> > getoptlong = GetoptLong.new(  
> > &nbsp;&nbsp;&nbsp;['--help', '-h', GetoptLong::NO\_ARGUMENT],  
> > &nbsp;&nbsp;&nbsp;['--short', '-s', GetoptLong::OPTIONAL\_ARGUMENT],  
> > &nbsp;&nbsp;&nbsp;['--offset', '-o', GetoptLong::REQUIRED\_ARGUMENT]  
> > )
> > 
> > def usage()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Usage:"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "myprog [--help] [--short optional\_arg] [--offset  
> > required\_arg]"  
> > end
> > 
> > short\_arg = nil # declare local var to store arg  
> > offset\_arg = nil # declare local var to store arg
> > 
> > begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getoptlong.each do |opt, arg|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case opt  
> > &nbsp;&nbsp;&nbsp;&nbsp;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sp;&nbsp;offset\_arg = arg  
> > &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;print "\<\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > rescue StandardError=\>my\_error\_message  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts my\_error\_message  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> > end
> > 
> > unless short\_arg == ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Short arg is=\>"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print short\_arg  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "\<"  
> > end
> > 
> > print "Offset arg is=\>"  
> > print offset\_arg  
> > puts "\<"
> 
> --  
> Jim Freeze

---

<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: [16 December 2005 21:52 UTC](https://rubytalk.org/t/getoptlong-example/23301/6 "2005-12-16T21:52:45Z")

</div>

Steve Litt wrote:

> def usage()  
> &nbsp;&nbsp;puts "Usage:"  
> &nbsp;&nbsp;puts "myprog [--help] [--short optional\_arg] [--offset required\_arg]"  
> end

Handy hint: use RDoc::usage

e.g.

#!/usr/bin/ruby

# == Synopsis

> **···**
>
> #  
> # What the program does  
> #  
> # == Usage  
> #  
> # progname [-h | --help] [--arg1 val1 | -a val1] [--flag] arg  
> #  
> # arg:  
> # The thing to do stuff to.  
> #  
> # val1:  
> # An optional value.  
> #  
> # --flag makes the program do something else.  
> #  
> # == Author  
> # Your Name Here (mailto:you@example.com)  
> #  
> # == Copyright  
> # Copyright (c) 2005 Your Name. Licensed under the same terms as Ruby.
> 
> require 'getoptlong'  
> require 'rdoc/usage'
> 
> opts = GetoptLong.new(  
> &nbsp;&nbsp;&nbsp;['--help', '-h', GetoptLong::NO\_ARGUMENT],  
> &nbsp;&nbsp;&nbsp;['--flag', '-f', GetoptLong::NO\_ARGUMENT],  
> &nbsp;&nbsp;&nbsp;['--arg1', '-a', GetoptLong::REQUIRED\_ARGUMENT]  
> )
> 
> @flag = false  
> @arg1 = nil  
> opts.each do |opt, arg|  
> &nbsp;&nbsp;&nbsp;case opt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--help'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RDoc::usage  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--flag'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@flag = true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--val1'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@arg1 = arg  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Unrecognized option #{opt} (try --help)"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 0  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> 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: ![Steve\_Litt](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Steve\_Litt](https://rubytalk.org/u/Steve_Litt)
#### Post date: [15 December 2005 13:47 UTC](https://rubytalk.org/t/getoptlong-example/23301/7 "2005-12-15T13:47:42Z")

</div>

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 Litt  
[http://www.troubleshooters.com](http://www.troubleshooters.com)  
slitt@troubleshooters.com

> **···**
>
> On Thursday 15 December 2005 07:32 am, Jim Freeze wrote:
> 
> > On 12/14/05, Nick Sieger \<nicksieger@gmail.com\> wrote:  
> > \> On 12/14/05, Steve Litt \<slitt@earthlink.net\> 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: ![Steve\_Litt](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Steve\_Litt](https://rubytalk.org/u/Steve_Litt)
#### Post date: [15 December 2005 23:20 UTC](https://rubytalk.org/t/getoptlong-example/23301/8 "2005-12-15T23:20:26Z")

</div>

The preceding has a couple typos that keep it from running. The  
following cures the typos, and also adds an argument called  
"--myflag" that is a flag that can go on or off, but errors out if  
you try to give it an argument:

#!/usr/bin/env ruby  
require 'rubygems'  
require 'commandline'

class App \< CommandLine::Application  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;synopsis "[-d] [-s [opt\_arg]] [-o req\_arg]"  
&nbsp;&nbsp;&nbsp;&nbsp;short\_description "A sample program"  
&nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--short -s),  
&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;:arg\_description =\> "opt\_arg",  
&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 =\> nil

&nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--offset -o),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "req\_arg",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_args

&nbsp;&nbsp;&nbsp;&nbsp;option :help  
&nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--myflag -m),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arity =\> [0,0],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "flag",  
&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_arg  
&nbsp;&nbsp;end

&nbsp;&nbsp;def main  
&nbsp;&nbsp;puts "Short arg is=\>#{opt["--short"]}\<" unless opt["--short"].nil?  
&nbsp;&nbsp;puts "Offset arg is=\>#{opt["--offset"]}\<"  
&nbsp;&nbsp;puts "Myarg arg is=\>#{opt["--myflag"]}\<"  
&nbsp;&nbsp;end

end#class App

> **···**
>
> On Thursday 15 December 2005 05:50 pm, Jim Freeze wrote:
> 
> > Here is the above code re-writen using CommandLine as  
> > requested by Steve. It is a fully working example.
> > 
> > require 'rubygems'  
> > require 'commandline'
> > 
> > class App \< CommandLine::Application  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;synopsis "[-d] [-s [opt\_arg]] [-o req\_arg]"  
> > &nbsp;&nbsp;&nbsp;&nbsp;short\_description "A sample program"  
> > &nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(-short -s),
> > 
> > &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;:arg\_description =\> "opt\_arg"  
> > &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 =\> nil
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--offset -o),
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "req\_arg",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_args
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;option :help  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def main  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Short arg is=\>#{opt["--short"]}\<" unless  
> > opt["--short"].nil? puts "Offset arg is=\>#{opt["--offset"]}\<"  
> > &nbsp;&nbsp;end
> > 
> > end#class App

---

<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: [15 December 2005 23:22 UTC](https://rubytalk.org/t/getoptlong-example/23301/9 "2005-12-15T23:22:47Z")

</div>

> \> I cruised the net, found an example, and turned it into a small

[clip]

> Here is the above code re-writen using CommandLine as  
> requested by Steve. It is a fully working example.

NOW I think I understand CommandLine::Application. It's like a  
GetoptLong that also gives you a usage() subroutine for free, and  
if you change your arguments, usage() automatically changes in  
sync. Very nice!

SteveT

Steve Litt  
[http://www.troubleshooters.com](http://www.troubleshooters.com)  
slitt@troubleshooters.com

> **···**
>
> On Thursday 15 December 2005 05:50 pm, Jim Freeze wrote:
> 
> > On 12/14/05, Steve Litt \<slitt@earthlink.net\> wrote:

---

<div class="post-metadata">

### Author: ![Wybo\_Dekker](https://avatars.discourse-cdn.com/v4/letter/w/ee59a6/32.png) [@Wybo\_Dekker](https://rubytalk.org/u/Wybo_Dekker)
#### Post date: [15 December 2005 15:00 UTC](https://rubytalk.org/t/getoptlong-example/23301/10 "2005-12-15T15:00:29Z")

</div>

[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 [http://www.servalys.nl/scripts/mk](http://www.servalys.nl/scripts/mk) with its  
rdoc at [http://www.servalys.nl/scripts/doc/files/mk.html](http://www.servalys.nl/scripts/doc/files/mk.html)

> **···**
>
> On Thu, 15 Dec 2005, Steve Litt 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.
> 
> --  
> Wybo

---

<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: [15 December 2005 15:06 UTC](https://rubytalk.org/t/getoptlong-example/23301/11 "2005-12-15T15:06:06Z")

</div>

Hi Steve

> **···**
>
> On 12/15/05, Steve Litt \<slitt@earthlink.net\> 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:
> 
> &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)
> 
> Please help me out and post some comments on what you like and don't  
> like. I am still making improvements in the docs.
> 
> --  
> Jim Freeze

---

<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: [15 December 2005 23:25 UTC](https://rubytalk.org/t/getoptlong-example/23301/12 "2005-12-15T23:25:26Z")

</div>

> The preceding has a couple typos that keep it from running. The  
> following cures the typos, and also adds an argument called  
> "--myflag" that is a flag that can go on or off, but errors out if  
> you try to give it an argument:

Thanks for fixing the errors. I should have mentioned that it  
was untested. And thanks for adding the shebang. Forgot  
that too.

> #!/usr/bin/env ruby  
> require 'rubygems'  
> require 'commandline'
> 
> class App \< CommandLine::Application  
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;synopsis "[-d] [-s [opt\_arg]] [-o req\_arg]"  
> &nbsp;&nbsp;&nbsp;&nbsp;short\_description "A sample program"  
> &nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--short -s),  
> &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;:arg\_description =\> "opt\_arg",  
> &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 =\> nil
> 
> &nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--offset -o),  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "req\_arg",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_args
> 
> &nbsp;&nbsp;&nbsp;&nbsp;option :help  
> &nbsp;&nbsp;&nbsp;&nbsp;option :names =\> %w(--myflag -m),  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arity =\> [0,0],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:arg\_description =\> "flag",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:opt\_found =\> get\_arg

A flag option can be written as:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option :flag, :names =\> %w(--myflag -m),

The get\_arg won't do anything since there is no argment for this option.

> **···**
>
> On 12/15/05, Steve Litt \<slitt@earthlink.net\> wrote:
> 
> > On Thursday 15 December 2005 05:50 pm, Jim Freeze wrote:
> 
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def main  
> > &nbsp;&nbsp;puts "Short arg is=\>#{opt["--short"]}\<" unless opt["--short"].nil?  
> > &nbsp;&nbsp;puts "Offset arg is=\>#{opt["--offset"]}\<"  
> > &nbsp;&nbsp;puts "Myarg arg is=\>#{opt["--myflag"]}\<"  
> > &nbsp;&nbsp;end
> > 
> > end#class App
> 
> --  
> Jim Freeze

---

<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: [15 December 2005 23:27 UTC](https://rubytalk.org/t/getoptlong-example/23301/13 "2005-12-15T23:27:37Z")

</div>

Uh huh. An notice that you never actually have to type App.new  
or App.run

> **···**
>
> On 12/15/05, Steve Litt \<slitt@earthlink.net\> wrote:
> 
> > NOW I think I understand CommandLine::Application. It's like a  
> > GetoptLong that also gives you a usage() subroutine for free, and  
> > if you change your arguments, usage() automatically changes in  
> > sync. Very nice!
> 
> --  
> 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: [15 December 2005 18:01 UTC](https://rubytalk.org/t/getoptlong-example/23301/14 "2005-12-15T18:01:08Z")

</div>

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, 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 Litt

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

slitt@troubleshooters.com

> **···**
>
> On Thursday 15 December 2005 10:06 am, Jim Freeze wrote:
> 
> > Hi Steve
> > 
> > On 12/15/05, Steve Litt \<slitt@earthlink.net\> 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.ht](http://rubyforge.org/docman/view.php/632/233/posted-docs.index.ht)  
> > ml
> > 
> > Please help me out and post some comments on what you like and  
> > don't like. I am still making improvements in the docs.
