class Getopt::Declare::Arg

Class used to handle other arguments (flags, etc)

Constants

Helpcmd
Versioncmd

Attributes

actions[RW]
args[RW]
desc[RW]
ditto[RW]
flag[RW]
id[RW]
nocase[RW]
repeatable[RW]
required[RW]
requires[RW]

Public Class Methods

besthelp() click to toggle source
# File lib/Getopt/Declare.rb, line 470
def Arg.besthelp
  for i in Helpcmd; return i if @@helpcmdH[i]; end
end
bestversion() click to toggle source
# File lib/Getopt/Declare.rb, line 485
def Arg.bestversion
  for i in Versioncmd; return i if @@versioncmdH[i]; end
end
clear() click to toggle source
# File lib/Getopt/Declare.rb, line 498
def Arg.clear
  @@flags = []
  @@nextid = 0
  @@posflagpat  = nil
  @@negflagpath = nil
end
helppat() click to toggle source

Create regex of help flags based on help shortcuts left

# File lib/Getopt/Declare.rb, line 475
def Arg.helppat
  @@helpcmdH.keys.join('|')
end
negflagpat(*t) click to toggle source

Return string with regex that avoids all flags in declaration

# File lib/Getopt/Declare.rb, line 506
def Arg.negflagpat(*t)
  if !@@negflagpat && @@flags
    @@negflagpat = ( @@flags.map { |i| 
                      "(?!" + Regexp::quote(i) + ")" } ).join('')
  else
    @@negflagpat
  end
end
new(spec, desc, dittoflag) click to toggle source

Constructor

# File lib/Getopt/Declare.rb, line 543
def initialize(spec, desc, dittoflag)
  first = 1


  @@nextid += 1
  @flag     = ''
  @foundid  = nil
  @args     = []
  @actions  = []
  @ditto    = dittoflag
  @required = false
  @requires = nil
  @id       = @@nextid
  @desc     = spec.dup
  @items    = 0
  @nocase   = false

  @desc.sub!(/\A\s*(.*?)\s*\Z/,'\1')

  while spec && spec != ''
    begin

      # OPTIONAL
      if spec.sub!( /\A(\s*)\[/, '\1' )
        @args.push( StartOpt.new )
        next
      elsif spec.sub!(/\A\s*\]/,"")
        @args.push( EndOpt.new )
        next
      end

      # ARG

      se  = DelimScanner::new( spec )
      tmp = se.scanBracketed('<>')

      arg = nows = nil
      arg, spec, nows = tmp[:match], tmp[:suffix], tmp[:prefix] if tmp


      if arg
        arg =~ /\A(\s*)(<)([a-zA-Z]\w*)(:[^>]+|)>/ or
          raise "Error: bad Getopt::Declare parameter variable specification near '#{arg}'\n"

        # NAME,TYPE,NOW
        details = [ "#$3", "#$4", !first && !(nows.length>0) ]

        if spec && spec.sub!( /\A\.\.\./, "")      # ARRAY ARG
          @args.push( ArrayArg.new(*details) )
        else  # SCALAR ARG
          @args.push( ScalarArg.new(*details) )
        end
        @items += 1
        next

        # PUNCTUATION
      elsif spec.sub!( /\A(\s*)((\\.|[^\] \t\n\[<])+)/, '' )
        ows, punct = $1, $2
        punct.gsub!( /\\(?!\\)(.)/, '\1' )

        if first
          spec  =~ /\A(\S+)/
          @foundid = "#{punct}#{$1}"
          @flag = punct
          @@flags.push( punct )
        else
          @args.push( Punctuator.new(punct, !(ows.size > 0)) )
          @items += 1
        end

      else 
        break
      end # if arg/spec.sub
    ensure
      first = nil
    end
  end # while

  @@helpcmdH.delete(@flag)    if @@helpcmdH.key?(@flag)
  @@versioncmdH.delete(@flag) if @@versioncmdH.key?(@flag)
end
posflagpat(*t) click to toggle source

Return string with regex that matches any of the flags in declaration

# File lib/Getopt/Declare.rb, line 516
def Arg.posflagpat(*t)
  if !@@posflagpat && @@flags
    @@posflagpat = '(?:' + ( @@flags.map { |i| 
                              Regexp::quote(i) } ).join('|') + ')'
  else
    @@posflagpat
  end
end
versionpat() click to toggle source

Create regex of version flags based on help shortcuts left

# File lib/Getopt/Declare.rb, line 490
def Arg.versionpat
  @@versioncmdH.keys.join('|')
end

Public Instance Methods

code(*t) click to toggle source

Return String with code to parse this argument (ie. flag)

# File lib/Getopt/Declare.rb, line 628
    def code(*t)
      owner = t[0]
      mod   = t[1]


      code = "\n"
      flag = @flag
      clump = owner.clump
      i = 0
      nocasei = ((Getopt::Declare::nocase || @nocase) ? 'i' : '')

      code << "          catch(:paramout) do\n            while "
      code += !@repeatable? "!_FOUND_['" + self.foundid + "']" : "true"

      if (flag && (clump==1 && flag !~ /\A[^a-z0-9]+[a-z0-9]\Z/i ||
                   (clump<3 && @args.size > 0 )))
        code << ' and !_lastprefix'
      end

      code << '
              begin
                catch(:param) do
                  _pos = _nextpos if _args
                  _PUNCT_ = {}
              '

      if flag != ''
        # This boundary is to handle -- option, so that if user uses
        # --foo and --foo is not a flag, it does not become
        # --  and unused: 'foo', but an error saying flag '--foo' not defined.
        boundary = ''
        boundary = '(\s+|\Z)' if flag =~ /^(--|-|\+|\+\+)$/

        code << '
                  _args && _pos = gindex( _args, /\G[\s|\0]*' + 
          Regexp::quote(flag) + boundary + '/' + nocasei + ", _pos) or throw(:paramout) 
                  unless @_errormsg
                    @_errormsg = %q|incorrect specification of '" + flag + "' parameter|
                  end
"
      elsif ( ScalarArg::stdtype(@args[0].type)||'') !~ /\%F/
        code << "\n                  throw(:paramout) if @_errormsg\n"
      end


      code << "\n                  _PARAM_ = '" + self.name + "'\n"


      trailer = []
      i = @args.size-1
      while i > 0
        trailer[i-1] = @args[i].trailer
        trailer[i-1] = trailer[i] unless trailer[i-1]
        i -= 1
      end # while i

      if @args
        code << "\n"+'                 _args && _pos = gindex( _args, /\G'

        @args.each_with_index { |arg, j|
          code << arg.ows(arg.matcher(trailer[j]))
        }

        code << '/x' + nocasei + ", _pos ) or throw(:paramout)\n"
      end # if @args

      @args.each_with_index { |arg, j|
        code << arg.code(j,mod)        #, $flag ????
      }

      if flag
        mutexlist = owner.mutex[flag] ? 
        (  owner.mutex[flag].map {|j| "'#{j}'"} ).join(',') : ''

        code << "
                  if _invalid.has_key?('#{flag}')
                    @_errormsg = %q|parameter '#{flag}' not allowed with parameter '| + _invalid['#{flag}'] + %q|'|
                    throw(:paramout)
                  else
                    for i in [#{mutexlist}]
                        _invalid[i] = '#{flag}'
                    end
                  end  #if/then

"
      end



      for action in @actions
        #action.sub!( /(\s*\{)/, '\1 module '+mod )  # @TODO
        code << "\n                  " + action + "\n"
      end

      if flag && @items==0
        code << "\n                  @cache['#{flag}'] = '#{flag}'\n"
        if @ditto
          code << "\n                  @cache['#{@ditto.flag}'] = '#{flag}'\n"
        end
      end

      if @items > 1
        code << "                  @cache['#{self.name}'] = {} unless @cache['#{self.name}'].kind_of?(Hash)\n"
        if @ditto
          code << "\n                  @cache['#{@ditto.name}'] = {} unless @cache['#{@ditto.name}'].kind_of?(Hash)\n"
        end
      end

      for subarg in @args
        code << subarg.cachecode(self.name,@items)
        if ditto
        code << subarg.cachecode(@ditto.name,@items)
        end
      end

      if flag =~ /\A([^a-z0-9]+)/i
        code << '                  _lastprefix = "'+ Regexp::quote("#$1") + '"' + "\n"
      else
        code << "                  _lastprefix = nil\n"
      end

      code << "
                  _FOUND_['"+ self.foundid + "'] = 1
                  throw :arg if _pos > 0
                  _nextpos = _args.size
                  throw :alldone
                end  # catch(:param)
              end  # begin
            end # while
          end # catch(:paramout)
"

      code
    end
found_requires() click to toggle source
# File lib/Getopt/Declare.rb, line 531
def found_requires
  expr = @requires.gsub(/((?:&&|\|\|)?\s*(?:[!(]\s*)*)([^ \t\n|&\)]+)/x,
                        '\1_FOUND_[\'\2\']')
  
  if !valid_syntax?( expr )
    raise "Error: bad condition in [requires: #{original}]\n"
  end
  expr
end
foundid() click to toggle source

Return foundid of argument, which can be flag’s name or variable’s name

# File lib/Getopt/Declare.rb, line 773
def foundid
  return @foundid || self.name
end
name() click to toggle source

Return name of argument, which can be flag’s name or variable’s name

# File lib/Getopt/Declare.rb, line 764
def name
  return @flag unless @flag.empty?
  for i in @args
    return "<#{i.name}>" if i.respond_to?(:name)
  end
  raise "Unknown flag name for parameter #{self.desc}"
end