# # opts.rb -- # parse the options # # Copyright (C) 1999 by Takaaki Tateishi # Time-stamp: <1999-03-21 23:08:38 ttate> # # Example: # 1) $ARGV = ["-b","a","-a","arg1","-h","arg2","arg3"] # opts = OPTS.new($ARGV,{'-a' => 1, '-h' => 0, '-b' => 1}) # opts['-a'] ==> ["arg1"] # opts['-h'] ==> true # opts['-b'] ==> ["a"] # opts[0] ==> "arg2" # opts[1] ==> "arg3" # # 2) $ARGV = ["-a","-b","-c","cmd","-p","1","10","arg"] # opts = OPTS.new($ARGV,{'-a'=>0, '-c'=>1, '-p'=>2}) # opts['-a'] ==> true # opts['-c'] ==> ["cmd"] # opts['-p'] ==> ["1","2"] # opts[0] ==> "-b" # opts[1] ==> "arg" class OPTS def initialize(argv=ARGV,opts={}) @opts = {} @args = [] parse(argv,opts) end def OPTS.parse(argv=ARGV,opts={}) OPT.new(argv,opts) end def [](key) case key when Integer @args[key] when String @opts[key] else raise(TypeError,"key must be a integer or string") end end def []=(key,val) case key when Integer @args[key] = val when String @opts[key] = val else raise(TypeError,"key must be a integer or string") end end def parse(argv,opts={}) args = argv.clone while( args[0] ) case (val = opts[args[0]]) when Integer if( val > 0 ) key = args[0] args.shift @opts[key] = [] for i in 1 .. val @opts[key].push(args[0]) args.shift end else @opts[args[0]] = true args.shift end else @args.push(args[0]) args.shift end end end private :parse end