Forum: Ruby getoptlong, getopt, parseopt, which one to use for command l

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4e1cda7ca71ce8ae801331a30c319257?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-02-17 20:17
(Received via mailing list)
There seems to be several options out there.  Which one should be used
when creating command line tools?
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-17 20:26
(Received via mailing list)
On Sat, 18 Feb 2006, Charlie Bowman 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
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-17 20:38
(Received via mailing list)
On 2/17/06, Charlie Bowman <cmbowma@castlebranch.com> 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}"
This topic is locked and can not be replied to.