1998-01-16 07:13:05 -05:00
|
|
|
#
|
2000-03-23 03:37:35 -05:00
|
|
|
# getopts.rb -
|
|
|
|
# $Release Version: $
|
|
|
|
# $Revision$
|
|
|
|
# $Date$
|
|
|
|
# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
# --
|
2000-02-23 00:23:12 -05:00
|
|
|
# this is obsolete; use getoptlong
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2000-03-23 03:37:35 -05:00
|
|
|
# 2000-03-21
|
|
|
|
# modified by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2002-03-04 16:15:47 -05:00
|
|
|
# 2002-03-05
|
|
|
|
# rewritten by Akinori MUSHA <knu@ruby-lang.org>
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
$RCS_ID=%q$Header$
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
|
2002-03-13 04:44:59 -05:00
|
|
|
def getopts(single_options, *options)
|
2000-03-23 03:37:35 -05:00
|
|
|
boolopts = {}
|
|
|
|
valopts = {}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
#
|
2002-03-04 16:15:47 -05:00
|
|
|
# set defaults
|
2000-03-23 03:37:35 -05:00
|
|
|
#
|
2002-03-04 16:15:47 -05:00
|
|
|
single_options.scan(/.:?/) do |opt|
|
|
|
|
if opt.size == 1
|
|
|
|
boolopts[opt] = false
|
|
|
|
else
|
|
|
|
valopts[opt[0, 1]] = nil
|
|
|
|
end
|
2002-03-13 04:44:59 -05:00
|
|
|
end if single_options
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-03-04 16:15:47 -05:00
|
|
|
options.each do |arg|
|
|
|
|
opt, val = arg.split(':', 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-03-04 16:15:47 -05:00
|
|
|
if val
|
|
|
|
valopts[opt] = val.empty? ? nil : val
|
|
|
|
else
|
|
|
|
boolopts[opt] = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2002-03-04 16:15:47 -05:00
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
#
|
|
|
|
# scan
|
|
|
|
#
|
|
|
|
c = 0
|
2002-03-04 16:15:47 -05:00
|
|
|
argv = ARGV
|
2000-03-23 03:37:35 -05:00
|
|
|
|
2002-03-04 16:15:47 -05:00
|
|
|
while arg = argv.shift
|
|
|
|
case arg
|
2000-03-23 03:37:35 -05:00
|
|
|
when /\A--(.*)/
|
2002-03-04 16:15:47 -05: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 03:37:35 -05:00
|
|
|
else
|
2002-03-04 16:15:47 -05:00
|
|
|
argv.unshift arg
|
2001-05-06 11:06:00 -04:00
|
|
|
return nil
|
|
|
|
end
|
2000-03-23 03:37:35 -05:00
|
|
|
|
2002-03-04 16:15:47 -05:00
|
|
|
c += 1
|
2000-03-23 03:37:35 -05:00
|
|
|
when /\A-(.+)/
|
2002-03-04 16:15:47 -05:00
|
|
|
opts = $1
|
2000-03-23 03:37:35 -05:00
|
|
|
|
2002-03-04 16:15:47 -05:00
|
|
|
until opts.empty?
|
|
|
|
opt = opts.slice!(0, 1)
|
2000-03-23 03:37:35 -05:00
|
|
|
|
2002-03-04 16:15:47 -05: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 03:37:35 -05:00
|
|
|
|
2002-03-04 16:15:47 -05: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 07:13:05 -05:00
|
|
|
end
|
2002-03-04 16:15:47 -05:00
|
|
|
end
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
#
|
|
|
|
# set
|
|
|
|
#
|
2002-11-03 14:05:33 -05:00
|
|
|
$OPT = {}
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
boolopts.each do |opt, val|
|
2002-11-03 14:05:33 -05:00
|
|
|
$OPT[opt] = val
|
|
|
|
|
|
|
|
sopt = opt.gsub(/[^A-Za-z0-9_]/, '_')
|
|
|
|
eval "$OPT_#{sopt} = val"
|
2002-03-04 16:15:47 -05:00
|
|
|
end
|
2000-03-23 03:37:35 -05:00
|
|
|
valopts.each do |opt, val|
|
2002-11-03 14:05:33 -05:00
|
|
|
$OPT[opt] = val
|
|
|
|
|
|
|
|
sopt = opt.gsub(/[^A-Za-z0-9_]/, '_')
|
|
|
|
eval "$OPT_#{sopt} = val"
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-03-23 03:37:35 -05:00
|
|
|
|
|
|
|
c
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|