def self.getopts(switches)
args = switches.split(/ */)
hash = {}
while !ARGV.empty? && ARGV.first =~ /^-(.)(.*)/s
first, rest = $1, $2
pos = switches.index(first)
raise Error, "invalid option '#{first}'" unless pos
if args[pos+1] == ":"
ARGV.shift
if rest.empty?
rest = ARGV.shift
if rest.nil? || rest.empty?
raise Error, "missing argument for '-#{args[pos]}'"
end
temp_args = args.map{ |e| "-#{e}" }
if temp_args.include?(rest) || temp_args.include?(rest[1..-1])
err = "cannot use switch '#{rest}' as argument "
err << "to another switch"
raise Error, err
end
if hash.has_key?(first)
hash[first] = [hash[first], rest].flatten
else
hash[first] = rest
end
else
if args.include?(rest) || args.include?(rest[1..-1])
err = "cannot use switch '#{rest}' as argument "
err += "to another switch"
raise Error, err
end
end
else
hash[first] = true
if rest.empty?
ARGV.shift
else
ARGV[0] = "-#{rest}"
end
end
end
hash
end