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
|
|
|
#
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
$RCS_ID=%q$Header$
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
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 = []
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
#
|
|
|
|
# set default
|
|
|
|
#
|
|
|
|
if single_opts then
|
|
|
|
single_opts.each_byte do |byte|
|
|
|
|
boolopts[ byte.chr ] = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
unless options.empty? then
|
|
|
|
single_colon = ''
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
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
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
valopts.each do |opt, dflt|
|
|
|
|
if opt.size == 1 then
|
|
|
|
single_colon << opt
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
if single_colon.empty? then
|
|
|
|
single_colon = single_colon_exp = nil
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
2000-03-23 11:37:35 +03:00
|
|
|
single_colon_exp = /[#{single_colon}]/
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
#
|
|
|
|
# scan
|
|
|
|
#
|
|
|
|
c = 0
|
|
|
|
arg = argv.shift
|
|
|
|
while arg do
|
|
|
|
case arg
|
|
|
|
when /\A--?\z/ # xinit -- -bpp 24
|
|
|
|
newargv.concat argv
|
1998-01-16 15:13:05 +03:00
|
|
|
break
|
2000-03-23 11:37:35 +03:00
|
|
|
|
|
|
|
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
|
2001-05-06 19:06:00 +04:00
|
|
|
return nil
|
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
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
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
c += 1
|
|
|
|
|
|
|
|
break
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
else # ruby test.rb
|
|
|
|
newargv.push arg
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
|
|
|
|
arg = argv.shift
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
#
|
|
|
|
# set
|
|
|
|
#
|
|
|
|
boolopts.each do |opt, val|
|
|
|
|
eval "$OPT_#{opt} = val"
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
valopts.each do |opt, val|
|
|
|
|
eval "$OPT_#{opt} = #{val == 0 ? 'nil' : 'val'}"
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2000-03-23 11:37:35 +03:00
|
|
|
argv.replace newargv
|
|
|
|
|
|
|
|
c
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|