From: Steve Litt Date: 2005-12-15T10:42:41+09:00 Subject: Re: GetoptLong example 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 > slitt@troubleshooters.com 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( [ '--help', '-h', GetoptLong::NO_ARGUMENT ], [ '--short', '-s', GetoptLong::OPTIONAL_ARGUMENT ], [ '--offset', '-o', GetoptLong::REQUIRED_ARGUMENT ] ) def usage() puts "Usage:" 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 getoptlong.each do |opt, arg| case opt when "--help" puts "dia help" when "--short" print "Short option. Arg is=>" print arg short_arg = arg print "<\n" when "--offset" print "Offset option. Arg is=>" print arg offset_arg = arg print "<\n" end end rescue StandardError=>my_error_message puts puts my_error_message usage() puts exit end unless short_arg == "" puts print "Short arg is=>" print short_arg puts "<" end print "Offset arg is=>" print offset_arg puts "<" Steve Litt http://www.troubleshooters.com slitt@troubleshooters.com