summaryrefslogtreecommitdiff
path: root/lib/getopts.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-23 08:37:35 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-23 08:37:35 +0000
commit688169fd83b24564b653c03977c168cea50ccd35 (patch)
treeb4724e5397cf5da5b554ab5795842a93145a88be /lib/getopts.rb
parent5c13dd59db1ee6c04cdac4ce2ee97d5934115439 (diff)
2000-03-23
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@648 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/getopts.rb')
-rw-r--r--lib/getopts.rb206
1 files changed, 96 insertions, 110 deletions
diff --git a/lib/getopts.rb b/lib/getopts.rb
index b513f89f31..490523b878 100644
--- a/lib/getopts.rb
+++ b/lib/getopts.rb
@@ -1,141 +1,127 @@
#
-# getopts.rb -
-# $Release Version: $
-# $Revision$
-# $Date$
-# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
+# getopts.rb -
+# $Release Version: $
+# $Revision$
+# $Date$
+# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
#
# --
# this is obsolete; use getoptlong
#
+# 2000-03-21
+# modified by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
#
$RCS_ID=%q$Header$
-def isSingle(lopt)
- if lopt.index(":")
- if lopt.split(":")[0].length == 1
- return true
- end
- end
- return nil
-end
-def getOptionName(lopt)
- return lopt.split(":")[0]
-end
+def getopts( single_opts, *options )
+ single_opts_exp = (single_opts && !single_opts.empty?) ?
+ /[#{single_opts}]/ : nil
+ single_colon_exp = nil
+ single_colon = nil
+ opt = arg = val = nil
+ boolopts = {}
+ valopts = {}
+ argv = ARGV
+ newargv = []
-def getDefaultOption(lopt)
- od = lopt.split(":")[1]
- if od
- return od
+ #
+ # set default
+ #
+ if single_opts then
+ single_opts.each_byte do |byte|
+ boolopts[ byte.chr ] = false
end
- return nil
end
+ unless options.empty? then
+ single_colon = ''
-def setOption(name, value)
- eval("$OPT_" + name + " = " + 'value')
+ options.each do |opt|
+ m = /\A([^:]+):(.*)\z/.match( opt )
+ if m then
+ valopts[ m[1] ] = m[2].empty? ? 0 : m[2]
+ else
+ boolopts[ opt ] = false
end
-
-def setDefaultOption(lopt)
- d = getDefaultOption(lopt)
- if d
- setOption(getOptionName(lopt), d)
end
+ valopts.each do |opt, dflt|
+ if opt.size == 1 then
+ single_colon << opt
end
-
-def setNewArgv(newargv)
- ARGV.clear
- for na in newargv
- ARGV << na
end
-end
-
-def getopts(single_opts, *options)
- if options
- single_colon = ""
- long_opts = []
- sc = 0
- for o in options
- setDefaultOption(o)
- if isSingle(o)
- single_colon[sc, 0] = getOptionName(o)
- sc += 1
+ if single_colon.empty? then
+ single_colon = single_colon_exp = nil
else
- long_opts.push(o)
- end
+ single_colon_exp = /[#{single_colon}]/
end
end
- opts = {}
- count = 0
- newargv = []
- while ARGV.length != 0
- compare = nil
- case ARGV[0]
- when /^--?$/
- ARGV.shift
- newargv += ARGV
+ #
+ # scan
+ #
+ c = 0
+ arg = argv.shift
+ while arg do
+ case arg
+ when /\A--?\z/ # xinit -- -bpp 24
+ newargv.concat argv
break
- when /^--.*/
- compare = ARGV[0][2, (ARGV[0].length - 2)]
- if long_opts != ""
- for lo in long_opts
- if lo.index(":") && getOptionName(lo) == compare
- if ARGV.length <= 1
- return nil
- end
- setOption(compare, ARGV[1])
- opts[compare] = true
- ARGV.shift
- count += 1
- break
- elsif lo == compare
- setOption(compare, true)
- opts[compare] = true
- count += 1
- break
- end
- end
- end
- if compare.length <= 1
- return nil
+
+ when /\A--(.*)/
+ opt = $1
+ if valopts.key? opt then # imclean --src +trash
+ return nil if argv.empty?
+ valopts[ opt ] = argv.shift
+ elsif boolopts.key? opt then # ruby --verbose
+ boolopts[ opt ] = true
+ else
+ return nil
+ end
+ c += 1
+
+ when /\A-(.+)/
+ arg = $1
+ 0.upto( arg.size - 1 ) do |idx|
+ opt = arg[idx, 1]
+ if single_opts and single_opts_exp === opt then
+ boolopts[ opt ] = true # ruby -h
+ c += 1
+
+ elsif single_colon and single_colon_exp === opt then
+ val = arg[ (idx+1)..-1 ]
+ if val.empty? then # ruby -e 'p $:'
+ return nil if argv.empty?
+ valopts[ opt ] = argv.shift
+ else # cc -ohello ...
+ valopts[ opt ] = val
end
- when /^-.*/
- for idx in 1..(ARGV[0].length - 1)
- compare = ARGV[0][idx, 1]
- if single_opts && compare =~ "[" + single_opts + "]"
- setOption(compare, true)
- opts[compare] = true
- count += 1
- elsif single_colon != "" && compare =~ "[" + single_colon + "]"
- if ARGV[0][idx..-1].length > 1
- setOption(compare, ARGV[0][(idx + 1)..-1])
- opts[compare] = true
- count += 1
- elsif ARGV.length <= 1
- return nil
- else
- setOption(compare, ARGV[1])
- opts[compare] = true
- ARGV.shift
- count += 1
- end
- break
- end
+ c += 1
+
+ break
+ else
+ return nil
+ end
+ end
+
+ else # ruby test.rb
+ newargv.push arg
end
- else
- compare = ARGV[0]
- opts[compare] = true
- newargv << ARGV[0]
+
+ arg = argv.shift
end
- ARGV.shift
- if !opts.has_key?(compare)
- return nil
+ #
+ # set
+ #
+ boolopts.each do |opt, val|
+ eval "$OPT_#{opt} = val"
end
+ valopts.each do |opt, val|
+ eval "$OPT_#{opt} = #{val == 0 ? 'nil' : 'val'}"
end
- setNewArgv(newargv)
- return count
+ argv.replace newargv
+
+ c
end