適当なコマンドラインパーサ

ライブラリないのかな?


def clp(argv)
params = {}
while param = argv.shift
if param =~ /^-.+/
param = param.sub("-", "")
vals = clpvals(argv)
case vals.length
when 0
params[param] = nil
when 1
params[param] = vals.first
else
params[param] = vals
end
end
end
return params
end

def clpvals(argv)
vals = []
until argv.empty? or argv.first =~ /^-.+/
vals << argv.shift
end
return vals
end