1998-01-16 15:13:05 +03:00
|
|
|
#
|
2000-03-23 11:37:35 +03:00
|
|
|
# getopts.rb -
|
|
|
|
# $Release Version: $
|
|
|
|
# $Revision$
|
|
|
|
# $Date$
|
|
|
|
# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# --
|
2000-02-23 08:23:12 +03:00
|
|
|
# this is obsolete; use getoptlong
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
2000-03-23 11:37:35 +03:00
|
|
|
# 2000-03-21
|
|
|
|
# modified by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
2002-03-05 00:15:47 +03:00
|
|
|
# 2002-03-05
|
|
|
|
# rewritten by Akinori MUSHA <knu@ruby-lang.org>
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2004-11-05 02:43:40 +03:00
|
|
|
warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: getopts is deprecated after Ruby 1.8.1; use optparse instead" if caller[0]
|
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
|
|
|
|
|
|
|
|
2002-03-13 12:44:59 +03:00
|
|
|
def getopts(single_options, *options)
|
2000-03-23 11:37:35 +03:00
|
|
|
boolopts = {}
|
|
|
|
valopts = {}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
#
|
2002-03-05 00:15:47 +03:00
|
|
|
# set defaults
|
2000-03-23 11:37:35 +03:00
|
|
|
#
|
2002-03-05 00:15:47 +03:00
|
|
|
single_options.scan(/.:?/) do |opt|
|
|
|
|
if opt.size == 1
|
|
|
|
boolopts[opt] = false
|
|
|
|
else
|
|
|
|
valopts[opt[0, 1]] = nil
|
|
|
|
end
|
2002-03-13 12:44:59 +03:00
|
|
|
end if single_options
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-03-05 00:15:47 +03:00
|
|
|
options.each do |arg|
|
|
|
|
opt, val = arg.split(':', 2)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-03-05 00:15:47 +03:00
|
|
|
if val
|
|
|
|
valopts[opt] = val.empty? ? nil : val
|
|
|
|
else
|
|
|
|
boolopts[opt] = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
2002-03-05 00:15:47 +03:00
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
#
|
|
|
|
# scan
|
|
|
|
#
|
|
|
|
c = 0
|
2002-03-05 00:15:47 +03:00
|
|
|
argv = ARGV
|
2000-03-23 11:37:35 +03:00
|
|
|
|
2002-03-05 00:15:47 +03:00
|
|
|
while arg = argv.shift
|
|
|
|
case arg
|
2000-03-23 11:37:35 +03:00
|
|
|
when /\A--(.*)/
|
2002-03-05 00:15:47 +03:00
|
|
|
if $1.empty? # xinit -- -bpp 24
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
opt, val = $1.split('=', 2)
|
|
|
|
|
|
|
|
if opt.size == 1
|
|
|
|
argv.unshift arg
|
|
|
|
return nil
|
|
|
|
elsif valopts.key? opt # imclean --src +trash
|
|
|
|
valopts[opt] = val || argv.shift or return nil
|
|
|
|
elsif boolopts.key? opt # ruby --verbose
|
|
|
|
boolopts[opt] = true
|
2000-03-23 11:37:35 +03:00
|
|
|
else
|
2002-03-05 00:15:47 +03:00
|
|
|
argv.unshift arg
|
2001-05-06 19:06:00 +04:00
|
|
|
return nil
|
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
|
2002-03-05 00:15:47 +03:00
|
|
|
c += 1
|
2000-03-23 11:37:35 +03:00
|
|
|
when /\A-(.+)/
|
2002-03-05 00:15:47 +03:00
|
|
|
opts = $1
|
2000-03-23 11:37:35 +03:00
|
|
|
|
2002-03-05 00:15:47 +03:00
|
|
|
until opts.empty?
|
|
|
|
opt = opts.slice!(0, 1)
|
2000-03-23 11:37:35 +03:00
|
|
|
|
2002-03-05 00:15:47 +03:00
|
|
|
if valopts.key? opt
|
|
|
|
val = opts
|
|
|
|
|
|
|
|
if val.empty? # ruby -e 'p $:'
|
|
|
|
valopts[opt] = argv.shift or return nil
|
|
|
|
else # cc -ohello ...
|
|
|
|
valopts[opt] = val
|
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
|
2002-03-05 00:15:47 +03:00
|
|
|
c += 1
|
|
|
|
break
|
|
|
|
elsif boolopts.key? opt
|
|
|
|
boolopts[opt] = true # ruby -h
|
|
|
|
c += 1
|
|
|
|
else
|
|
|
|
argv.unshift arg
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
argv.unshift arg
|
|
|
|
break
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2002-03-05 00:15:47 +03:00
|
|
|
end
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
#
|
|
|
|
# set
|
|
|
|
#
|
2002-11-03 22:05:33 +03:00
|
|
|
$OPT = {}
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
boolopts.each do |opt, val|
|
2002-11-03 22:05:33 +03:00
|
|
|
$OPT[opt] = val
|
|
|
|
|
|
|
|
sopt = opt.gsub(/[^A-Za-z0-9_]/, '_')
|
|
|
|
eval "$OPT_#{sopt} = val"
|
2002-03-05 00:15:47 +03:00
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
valopts.each do |opt, val|
|
2002-11-03 22:05:33 +03:00
|
|
|
$OPT[opt] = val
|
|
|
|
|
|
|
|
sopt = opt.gsub(/[^A-Za-z0-9_]/, '_')
|
|
|
|
eval "$OPT_#{sopt} = val"
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
|
|
|
|
c
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|