1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/parsearg.rb
gsinclair 1399c1bca0 * lib/cgi-lib.rb: deprecated after 1.8.1
* lib/getopts.rb:   ditto
 * lib/importenv.rb: ditto
 * lib/parsearg.rb:  ditto


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5771 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-02-18 15:27:06 +00:00

85 lines
1.4 KiB
Ruby

#
# parsearg.rb - parse arguments
# $Release Version: $
# $Revision$
# $Date$
# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
#
# --
#
#
#
warn "Warning: parsearg is deprecated after Ruby 1.8.1; use optparse instead"
$RCS_ID=%q$Header$
require "getopts"
def printUsageAndExit()
if $USAGE
eval($USAGE)
end
exit()
end
def setParenthesis(ex, opt, c)
if opt != ""
ex = sprintf("%s$OPT_%s%s", ex, opt, c)
else
ex = sprintf("%s%s", ex, c)
end
return ex
end
def setOrAnd(ex, opt, c)
if opt != ""
ex = sprintf("%s$OPT_%s %s%s ", ex, opt, c, c)
else
ex = sprintf("%s %s%s ", ex, c, c)
end
return ex
end
def setExpression(ex, opt, op)
if !op
ex = sprintf("%s$OPT_%s", ex, opt)
return ex
end
case op.chr
when "(", ")"
ex = setParenthesis(ex, opt, op.chr)
when "|", "&"
ex = setOrAnd(ex, opt, op.chr)
else
return nil
end
return ex
end
def parseArgs(argc, nopt, single_opts, *opts)
if (noOptions = getopts(single_opts, *opts)) == nil
printUsageAndExit()
end
if nopt
ex = nil
pos = 0
for o in nopt.split(/[()|&]/)
pos += o.length
ex = setExpression(ex, o, nopt[pos])
pos += 1
end
begin
if !eval(ex)
printUsageAndExit()
end
rescue
print "Format Error!! : \"" + nopt + "\"\t[parseArgs]\n"
exit!(-1)
end
end
if ARGV.length < argc
printUsageAndExit()
end
return noOptions
end