def code(*t)
package = t[0] || ''
code = %q%
@_deferred = []
@_errormsg = nil
@_finished = nil
begin
def defer(&i)
@_deferred.push( i )
end
def reject(*i)
if !i || i[0]
@_errormsg = i[1] if i[1]
throw :paramout
end
end
def finish(i)
if !i || i[0]
@_finished = 1
end
end
@unused = []
@cache = {}
_FOUND_ = {}
_errors = 0
_invalid = {}
_lastprefix = nil
_pos = 0 # current position to match from
_nextpos = 0 # next position to match from
catch(:alldone) do
while !@_finished
begin
catch(:arg) do
@_errormsg = nil
# This is used for clustering of flags
while _lastprefix
substr = _args[_nextpos..-1]
substr =~ /^(?!\s|\0|\Z)% +
Getopt::Declare::Arg::negflagpat() + %q%/ or
begin
_lastprefix=nil
break
end
"#{_lastprefix}#{substr}" =~ /^(% +
Getopt::Declare::Arg::posflagpat() + %q%)/ or
begin
_lastprefix=nil
break
end
_args = _args[0.._nextpos-1] + _lastprefix + _args[_nextpos..-1]
break
end # while _lastprefix
% + '' + %q%
_pos = _nextpos if _args
usage(0) if _args && gindex(_args,/\G(% + @helppat + %q%)(\s|\0|\Z)/,_pos)
version(0) if _args && _args =~ /(% + @verspat + %q%)(\s|\0|\Z)/
%
for arg in @args
code << arg.code(self,package)
end
code << %q%
if _lastprefix
_pos = _nextpos + _lastprefix.length()
_lastprefix = nil
next
end
_pos = _nextpos
_args && _pos = gindex( _args, /\G(?:\s|\0)*(\S+)/, _pos ) or throw(:alldone)
if @_errormsg
$stderr.puts( "Error#{source}: #{@_errormsg}\n" )
else
@unused.push( @@m[0] )
end
_errors += 1 if @_errormsg
end # catch(:arg)
ensure # begin
_pos = 0 if _pos.nil?
_nextpos = _pos if _args
if _args and _args.index( /\G(\s|\0)*\Z/, _pos )
_args = _get_nextline.call(self) if !@_finished
throw(:alldone) unless _args
_pos = _nextpos = 0
_lastprefix = ''
end # if
end # begin/ensure
end # while @_finished
end # catch(:alldone)
end # begin
%
for arg in @args
next unless arg.required
code << %q%
unless _FOUND_['% + arg.name + %q%'] %
if @mutex[arg.name]
for m in @mutex[arg.name]
code << %q# or _FOUND_['# + m + %q#']#
end
end
code << %q%
$stderr.puts "Error#{@source}: required parameter '% + arg.name + %q%' not found."
_errors += 1
end
%
end
for arg in @args
next unless arg.requires
code << %q%
if _FOUND_['% + arg.name + %q%'] && !(% + _enfound(arg.requires) +
%q%)
$stderr.puts "Error#{@source}.: parameter '% + arg.name + %q%' can only be specified with '% + _enbool( arg.requires ) + %q%'"
_errors += 1
end
%
end
code << %q%
#################### Add unused arguments
if _args && _nextpos > 0 && _args.length() > 0
@unused.push( _args[_nextpos..-1].split(' ' ) )
end
for i in @unused
i.tr!( "\0", " " )
end
%
if @strict
code << %q%
#################### Handle strict flag
unless _nextpos < ( _args ? _args.length : 0 )
for i in @unused
$stderr.puts "Error#{@source}: unrecognizable argument ('#{i}')"
_errors += 1
end
end
%
end
code << %q%
#################### Print help hint
if _errors > 0 && !@source.nil?
$stderr.puts "\n(try '#$0 % + Getopt::Declare::Arg::besthelp + %q%' for more information)"
end
## cannot just assign unused to ARGV in ruby
unless @source != ''
ARGV.clear
@unused.map { |i| ARGV.push(i) }
end
unless _errors > 0
for i in @_deferred
begin
i.call()
rescue => e
raise "Error: action in Getopt::Declare specification produced:\n" + e
end
end
end
!(_errors>0) # return true or false (false for errors)
%
return code
end