--Message-Boundary-14942 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Content-description: Mail message body On 11 Apr 2002, at 22:16, Daniel Berger wrote: > I'm a bit confused by GetoptLong. Originally, I thought it was a > subclass of Hash, but that doesn't seem to be the case. Hi Daniel, some months ago I also wanted to access the options via a Hash- like interface. I attach the class GetoptHash I wrote back then and some unit tests. I remember there has been an improved way to process command line options, something that let you define the options and a description of the options all in one place and then automatically derive usage information from that. If you search the ruby-talk archives or the RAA you should be able to find that. Regards, Pit --Message-Boundary-14942 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Content-description: Text from file 'getopthash_test.rb' # # Tests for class GetoptHash # # (c) 2002 Pit Capitain # require 'getopthash' require 'test/unit' class GetoptHash_test < Test::Unit::TestCase ID @(#) $Id: getopthash_test.rb,v 1.1 2002/03/15 13:46:47 Pit Exp $' def set_up @args RGV.dup @opts [ '-a', GetoptLong::OPTIONAL_ARGUMENT ], [ '-b', GetoptLong::OPTIONAL_ARGUMENT ] ] end def tear_down set_args( *@args ) end def set_args( *args ) ARGV.clear.concat( args ) end def test1_new o etoptHash.new() assert_equal( {}, o ) end def test2_empty set_args o etoptHash.new( *@opts ) assert_equal( {}, o ) assert_equal( [], ARGV ) end def test3_simple set_args( '-a' ) o etoptHash.new( *@opts ) assert_equal( { '-a' '' }, o ) assert_equal( [], ARGV ) end def test4_rest set_args( '-a', '12', '34' ) o etoptHash.new( *@opts ) assert_equal( { '-a' '12' }, o ) assert_equal( [ '34' ], ARGV ) end def test5_default set_args( '-b' ) o etoptHash.new( { '-b' '33', '-a' '33' }, *@opts ) assert_equal( { '-a' '33', '-b' '' }, o ) assert_equal( [], ARGV ) end end --Message-Boundary-14942 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Content-description: Text from file 'getopthash.rb' # # Hash interface to GetoptLong # # (c) 2002 Pit Capitain # require 'getoptlong' class GetoptHash < Hash ID @(#) $Id: getopthash.rb,v 1.1 2002/03/15 13:46:40 Pit Exp $' def initialize( *options ) super() if options && options[ 0 ].kind_of?( Hash ) update( options.shift ) end GetoptLong.new( *options ).each { | opt, arg | self[ opt ] rg } end end --Message-Boundary-14942--