# Getoptlong, getopt, parseopt, which one to use for command l

**URL:** <https://www.ruby-forum.com/t/getoptlong-getopt-parseopt-which-one-to-use-for-command-l/69796>\
**Category:** Ruby\
**Created:** [February 17, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/getoptlong-getopt-parseopt-which-one-to-use-for-command-l/69796 "2006-02-17T20:17:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 17, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/getoptlong-getopt-parseopt-which-one-to-use-for-command-l/69796/1 "2006-02-17T20:17:09Z")

</div>

There seems to be several options out there. Which one should be used  
when creating command line tools?

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 17, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/getoptlong-getopt-parseopt-which-one-to-use-for-command-l/69796/2 "2006-02-17T20:26:17Z")

</div>

On Sat, 18 Feb 2006, Charlie B. wrote:

> There seems to be several options out there. Which one should be used  
> when creating command line tools?

it keeps things exciting to mix them up in unpredictable ways -  
preferably in  
obfusicated code.

😉

seriously - use getoptlong for simple stuff and optparse if your needs  
are  
more elaborate.

the big limitation on getoptlong is that it eats ARGV only. most times  
this  
is fine.

regards.

-a

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 17, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/getoptlong-getopt-parseopt-which-one-to-use-for-command-l/69796/3 "2006-02-17T20:38:28Z")

</div>

On 2/17/06, Charlie B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There seems to be several options out there. Which one should be used  
> when creating command line tools?

I was just looking into that last night, but in my case I wanted to  
stick with a standard library so the choices were GetoptLong and  
OptionParser. Of those it seems to me that OptionParser is a little  
better. Here’s some example code that demonstrates using it. If I’m  
not using it in the best way, I’m definitely open to feedback. I’m  
also interested in hearing whether there is a better alternative.

# To get usage info.: ruby optionparserdemo.rb -h

# To take defaults: ruby optionparserdemo.rb

# To specify all options: ruby optionparserdemo.rb -n Mark -d 19 -c blue

-f

# Example of an invalid option: ruby optionparserdemo.rb -z 42

# Example of an invalid value: ruby optionparserdemo.rb -c yellow

# Example of another invalid value: ruby optionparserdemo.rb -d foo

require ‘optparse’

# Create a Hash containing option default values.

# Specified options will replace these.

options = {:name=\>‘unknown’, :duration=\>5, :color=\>‘red’, :final=\>false}

# Configure an OptionParser.

parser = OptionParser.new  
parser.on(‘-h’, ‘–help’,  
‘displays usage information’) do  
puts parser  
exit  
end  
parser.on(‘-n=NAME’, ‘–name=NAME’,  
‘name of person running experiment’) do |v|  
options[:name] = v  
end  
parser.on(‘-d=DURATION’, ‘–duration=DURATION’, Integer,  
‘duration in seconds’) do |v|  
options[:duration] = v  
end  
parser.on(‘-c=COLOR’, ‘–color=COLOR’, %w(red green blue),  
‘color of light’) do |v|  
options[:color] = v  
end  
parser.on(‘-f’, ‘–final’, TrueClass,  
‘true if final run; false otherwise’) do |v|  
options[:final] = true  
end

# Parse command-line options.

begin  
parser.parse($\*)  
rescue OptionParser::ParseError  
puts $!  
exit  
end

# Output the options that will be used.

puts “Options are #{options.inspect}”
