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( > [ '--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 "<" 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 def initialize synopsis "[-d] [-s [opt_arg]] [-o req_arg]" short_description "A sample program" option :names => %w(-short -s), :arity => [0,1], :arg_description => "opt_arg" :opt_found => get_arg, :opt_not_found => nil option :names => %w(--offset -o), :arg_description => "req_arg", :opt_found => get_args option :help end def main puts "Short arg is=>#{opt["--short"]}<" unless opt["--short"].nil? puts "Offset arg is=>#{opt["--offset"]}<" end end#class App -- Jim Freeze