1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# parsearg.rb - parse arguments
|
|
|
|
# $Release Version: $
|
|
|
|
# $Revision$
|
|
|
|
# $Date$
|
|
|
|
# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2004-02-25 14:44:58 +03:00
|
|
|
warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: parsearg is deprecated after Ruby 1.8.1; use optparse instead"
|
2004-02-18 18:27:06 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
$RCS_ID=%q$Header$
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
require "getopts"
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2006-08-04 22:05:50 +04:00
|
|
|
# parseArgs is obsolete. Use OptionParser instead.
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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"
|
2002-07-29 10:14:10 +04:00
|
|
|
exit!(-1)
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if ARGV.length < argc
|
|
|
|
printUsageAndExit()
|
|
|
|
end
|
|
|
|
return noOptions
|
|
|
|
end
|