def GetCurrentInvocation(invocations, pos):
  """Determine the current invocation given a cursor position.

  Args:
    invocations: a list of GcloudInvocations
    pos: an int giving the current cursor position

  Returns:
    The corresponding GcloudInvocation at position pos, or None.
  """
  for invocation in invocations:
    tokens = invocation.tokens
    if tokens:
      start = tokens[0].start
      end = tokens[-1].end
      if ((start <= pos <= end) or
          (invocations.index(invocation) == len(invocations)-1)):
        return tokens
  return None