1 | #
|
---|
2 | # parsearg.rb - parse arguments
|
---|
3 | # $Release Version: $
|
---|
4 | # $Revision: 11708 $
|
---|
5 | # $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
|
---|
6 | # by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
|
---|
7 | #
|
---|
8 | # --
|
---|
9 | #
|
---|
10 | #
|
---|
11 | #
|
---|
12 |
|
---|
13 | warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: parsearg is deprecated after Ruby 1.8.1; use optparse instead"
|
---|
14 |
|
---|
15 | $RCS_ID=%q$Header$
|
---|
16 |
|
---|
17 | require "getopts"
|
---|
18 |
|
---|
19 | def printUsageAndExit()
|
---|
20 | if $USAGE
|
---|
21 | eval($USAGE)
|
---|
22 | end
|
---|
23 | exit()
|
---|
24 | end
|
---|
25 |
|
---|
26 | def setParenthesis(ex, opt, c)
|
---|
27 | if opt != ""
|
---|
28 | ex = sprintf("%s$OPT_%s%s", ex, opt, c)
|
---|
29 | else
|
---|
30 | ex = sprintf("%s%s", ex, c)
|
---|
31 | end
|
---|
32 | return ex
|
---|
33 | end
|
---|
34 |
|
---|
35 | def setOrAnd(ex, opt, c)
|
---|
36 | if opt != ""
|
---|
37 | ex = sprintf("%s$OPT_%s %s%s ", ex, opt, c, c)
|
---|
38 | else
|
---|
39 | ex = sprintf("%s %s%s ", ex, c, c)
|
---|
40 | end
|
---|
41 | return ex
|
---|
42 | end
|
---|
43 |
|
---|
44 | def setExpression(ex, opt, op)
|
---|
45 | if !op
|
---|
46 | ex = sprintf("%s$OPT_%s", ex, opt)
|
---|
47 | return ex
|
---|
48 | end
|
---|
49 | case op.chr
|
---|
50 | when "(", ")"
|
---|
51 | ex = setParenthesis(ex, opt, op.chr)
|
---|
52 | when "|", "&"
|
---|
53 | ex = setOrAnd(ex, opt, op.chr)
|
---|
54 | else
|
---|
55 | return nil
|
---|
56 | end
|
---|
57 | return ex
|
---|
58 | end
|
---|
59 |
|
---|
60 | # parseArgs is obsolete. Use OptionParser instead.
|
---|
61 |
|
---|
62 | def parseArgs(argc, nopt, single_opts, *opts)
|
---|
63 | if (noOptions = getopts(single_opts, *opts)) == nil
|
---|
64 | printUsageAndExit()
|
---|
65 | end
|
---|
66 | if nopt
|
---|
67 | ex = nil
|
---|
68 | pos = 0
|
---|
69 | for o in nopt.split(/[()|&]/)
|
---|
70 | pos += o.length
|
---|
71 | ex = setExpression(ex, o, nopt[pos])
|
---|
72 | pos += 1
|
---|
73 | end
|
---|
74 | begin
|
---|
75 | if !eval(ex)
|
---|
76 | printUsageAndExit()
|
---|
77 | end
|
---|
78 | rescue
|
---|
79 | print "Format Error!! : \"" + nopt + "\"\t[parseArgs]\n"
|
---|
80 | exit!(-1)
|
---|
81 | end
|
---|
82 | end
|
---|
83 | if ARGV.length < argc
|
---|
84 | printUsageAndExit()
|
---|
85 | end
|
---|
86 | return noOptions
|
---|
87 | end
|
---|