def complete(self, text, state):
        if state == 0:
            if readline:
                line = readline.get_line_buffer()
            else:
                line = text
            part = RE_SPACING.split(line)

            if part and part[0]:
                cmnd = part[0]
                if len(part) == 1:
                    self.matches = [key
                        for key in self.commands.keys()
                        if key.startswith(text) and (
                            self.commands[key].is_global or \
                            self.commands[key].valid_in_context(self.context))]
                elif cmnd in self.commands:
                    text = text.replace(cmnd, '').lstrip()
                    self.matches = self.commands[cmnd].complete(text, state)
                else:
                    self.matches = []
            else:
                self.matches = [key
                    for key in self.commands.keys()
                    if self.commands[key].is_global or \
                        self.commands[key].valid_in_context(self.context)]

        try:
            return self.matches[state]
        except IndexError:
            pass
dataset/ETHPy150Open tehmaze/nagios-cli/nagios_cli/cli.py/CLI.complete/original