def getInputArgs()

in scripts/yapl/Utilities.py [0:0]


def getInputArgs(argsSignature,args=None):
  """
    Return a dictionary with the strings in sys.argv processed as name-value pairs or "switch" keyword args.
    
    NOTE: When running wsadmin, sys.argv[0] is the first actual argument, not the name of the script being invoked.
    However, when running with Jython directly, the first argument is the name of the Jython script.  An exception 
    will be raised because if the name of the script is not listed in the argsSignature dictionary.  The simplest 
    thing to do when running with Jython directly is to pass in sys.argv[1:].
              
    Input: An args "signature" dictionary with the keyword entries in it and the expected type of the argument value. 
    The recognized types are:
        string, integer, int, float, boolean and switch.
  
    A "switch" type argument is one that is a keyword only and doesn't have a value.  If it is present in the argv list, 
    a boolean true (1) is assigned to its corresponding arg name.

    The keywords in the arg signature are assumed to begin with a dash (-) or a double dash (--).  (The double dash is 
    occasionally necessary to avoid conflicts with the wsadmin command line args, e.g. --help to emit usage info. The
    dashes are stripped off to create an arg name when assigning key-value pairs in the output dictionary of actual args.

    If a string type keyword appears more than once in the argsv[] array, then a list of values for that keyword is created 
    for that entry in the output dictionary.  This is handy for writing scripts where you want to be able to allow the user 
    repeat a particular argument multiple times so as to provide a list.  The other approach to providing a list is to use a 
    comma or space separated string and then create the list with split.  We didn't provide this capability for the other 
    types of arguments since we couldn't come up with a realistic scenario for passing in a list of numbers or booleans 
    using multiple keyword args.  If we do, we'll modify the method.

    NOTE: If a script has all optional arguments then the args argument may end up being the empty list.  We explicitly check 
    for args == None to cover cases where no args at all are passed in and in that case sys.argv is used for args. Be careful 
    when using Jython directly, because sys.argv[0] holds the name of the Jython script.  It is recommended that the caller pass 
    in sys.argv[1:] when running with Jython directly.  When running with wsadmin, the first element in sys.argv is stripped off 
    by wsadmin.

  """
  if (args == None):
    # assume it is appropriate to default to sys.argv
    args = sys.argv
  #endIf
  
  argsDict = {}
  i = 0
  while (i < len(args)):
    keyword = args[i]

    if (not argsSignature.has_key(keyword)):
      raise Exception("Unknown command line argument: %s" % keyword)
    
    if (keyword.startswith("--")):
      argName = keyword[2:len(keyword)] # strip the leading dashes
    else:
      argName = keyword[1:len(keyword)] # strip single leading dash
    #endIf

    if (argsSignature[keyword] == 'string'):
      i += 1  # index of arg value
      argValue = args[i]
      # If argValue is enclosed in double-quotes, strip the double-quotes.
      # This handles cases where incoming args from a shell are quoted to
      # avoid evaluation of shell special characters, e.g., *
      if (argValue[0] == '"' and argValue[-1] == '"'):
        argValue = argValue[1:-1]
      #endIf
      currentValue = argsDict.get(argName)
      if (currentValue != None):
        if (type(currentValue) == type([])):
          # current value already a list
          argsDict[argName] = currentValue.append(argValue)
        else:
          # current value is a string, so make a list
          argsDict[argName] = [currentValue, argValue]
        #endIf
      else:
        argsDict[argName] = argValue
      #endIf
    elif (argsSignature[keyword] == 'integer' or argsSignature[keyword] == 'int'):
      i += 1
      argsDict[argName] = int(args[i])
    elif (argsSignature[keyword] == 'float'):
      i += 1
      argsDict[argName] = float(args[i])
    elif (argsSignature[keyword] == 'boolean'):
      i += 1
      argsDict[argName] = toBoolean(args[i])
    elif (argsSignature[keyword] == 'switch'):
      # for a "switch" type arg, the index doesn't get advanced
      argsDict[argName] = True
    else:
      raise Exception("Unknown argument type in command line argument signature: %s" % argsSignature[keyword])
    
    i += 1  # index of next keyword
  #endWhile

  return argsDict