예제 #1
0
    def CommandCompleter(self, args):
        """Returns the command/group completion choices for args or None.

    Args:
      args: The CLI tree parsed command args.

    Returns:
      (choices, offset):
        choices - The list of completion strings or None.
        offset - The completion prefix offset.
    """
        arg = args[-1]

        if arg.value.startswith('-'):
            return None, 0

        if arg.token_type == parser.ArgTokenType.GROUP:
            if not self.empty:
                return [], 0
            node = arg.tree
            prefix = ''

        elif arg.token_type == parser.ArgTokenType.PREFIX:
            prefix = arg.value
            node = self.parser.root
        elif arg.token_type == parser.ArgTokenType.UNKNOWN:
            prefix = arg.value
            if (self.manpage_generator and not prefix and len(args) == 2
                    and args[0].value):
                node = generate_cli_trees.LoadOrGenerate(args[0].value)
                if not node:
                    return None, 0
                self.parser.root[parser.LOOKUP_COMMANDS][args[0].value] = node
            elif args[-2].token_type == parser.ArgTokenType.GROUP:
                node = args[-2].tree
            else:
                return None, 0

        else:
            return None, 0

        return [
            k for k, v in six.iteritems(node[parser.LOOKUP_COMMANDS])
            if k.startswith(prefix) and not self.IsSuppressed(v)
        ], -len(prefix)
예제 #2
0
  def CommandCompleter(self, args):
    """Returns the command/group completion choices for args or None.

    Args:
      args: The CLI tree parsed command args.

    Returns:
      (choices, offset):
        choices - The list of completion strings or None.
        offset - The completion prefix offset.
    """
    arg = args[-1]

    if arg.value.startswith('-'):
      # A flag, not a command.
      return None, 0

    elif arg.token_type == parser.ArgTokenType.PREFIX:
      # The root command name arg ("argv[0]"), the first token at the beginning
      # of the command line or the next token after a shell statement separator.
      node = self.parser.root
      prefix = arg.value

    elif arg.token_type in (parser.ArgTokenType.COMMAND,
                            parser.ArgTokenType.GROUP) and not self.empty:
      # A command/group with an exact CLI tree match. See if it's also a prefix
      # of other command/groups.
      node = args[-2].tree if len(args) > 1 else self.parser.root
      prefix = arg.value
      for c in node[parser.LOOKUP_COMMANDS]:
        if c.startswith(prefix) and c != prefix:
          break
      else:
        return None, 0

    elif arg.token_type == parser.ArgTokenType.GROUP:
      # A command group with an exact CLI tree match.
      if not self.empty:
        return [], 0
      node = arg.tree
      prefix = ''

    elif arg.token_type == parser.ArgTokenType.UNKNOWN:
      # Unknown command arg type.
      prefix = arg.value
      if (self.manpage_generator and not prefix and
          len(args) == 2 and args[0].value):
        node = generate_cli_trees.LoadOrGenerate(args[0].value)
        if not node:
          return None, 0
        self.parser.root[parser.LOOKUP_COMMANDS][args[0].value] = node
      elif len(args) > 1 and args[-2].token_type == parser.ArgTokenType.GROUP:
        node = args[-2].tree
      else:
        return None, 0

    else:
      # Don't know how to complete this arg.
      return None, 0

    return [k for k, v in six.iteritems(node[parser.LOOKUP_COMMANDS])
            if k.startswith(prefix) and not self.IsSuppressed(v)], -len(prefix)