def collect_completions(self, active_parsers, parsed_args, cword_prefix, debug):
completions = []<EOL>for parser in active_parsers:<EOL><INDENT>debug("<STR_LIT>", parser)<EOL>for action in parser._actions:<EOL><INDENT>debug("<STR_LIT>", action)<EOL>if isinstance(action, argparse._SubParsersAction):<EOL><INDENT>subparser_activated = False<EOL>for subparser in action._name_parser_map.values():<EOL><INDENT>if subparser in active_parsers:<EOL><INDENT>subparser_activated = True<EOL><DEDENT><DEDENT>if subparser_activated:<EOL><INDENT>completions = []<EOL><DEDENT>else:<EOL><INDENT>completions += [subcmd for subcmd in action.choices.keys() if subcmd.startswith(cword_prefix)]<EOL><DEDENT><DEDENT>elif self.always_complete_options or (len(cword_prefix) > <NUM_LIT:0> and cword_prefix[<NUM_LIT:0>] in parser.prefix_chars):<EOL><INDENT>completions += [option for option in action.option_strings if option.startswith(cword_prefix)]<EOL><DEDENT><DEDENT>debug("<STR_LIT>".format(l=len(parser.active_actions), a=parser.active_actions))<EOL>if len(cword_prefix) == <NUM_LIT:0> or cword_prefix[<NUM_LIT:0>] not in parser.prefix_chars:<EOL><INDENT>for active_action in parser.active_actions:<EOL><INDENT>if not active_action.option_strings: <EOL><INDENT>if action_is_satisfied(active_action) and not action_is_open(active_action):<EOL><INDENT>debug("<STR_LIT>", active_action)<EOL>continue<EOL><DEDENT><DEDENT>debug("<STR_LIT>", active_action, active_action._orig_class)<EOL>completer = getattr(active_action, '<STR_LIT>', None)<EOL>if completer is None and active_action.choices is not None:<EOL><INDENT>if not isinstance(active_action, argparse._SubParsersAction):<EOL><INDENT>completer = completers.ChoicesCompleter(active_action.choices)<EOL><DEDENT><DEDENT>if completer:<EOL><INDENT>if len(active_action.option_strings) > <NUM_LIT:0>: <EOL><INDENT>if not action_is_satisfied(active_action):<EOL><INDENT>debug("<STR_LIT>", active_action, "<STR_LIT>")<EOL>completions = []<EOL><DEDENT><DEDENT>if callable(completer):<EOL><INDENT>completions += [c for c in completer(prefix=cword_prefix, action=active_action,<EOL>parsed_args=parsed_args)<EOL>if self.validator(c, cword_prefix)]<EOL><DEDENT>else:<EOL><INDENT>debug("<STR_LIT>")<EOL>for i in range(<NUM_LIT>):<EOL><INDENT>next_completion = completer.complete(cword_prefix, i)<EOL>if next_completion is None:<EOL><INDENT>break<EOL><DEDENT>if self.validator(next_completion, cword_prefix):<EOL><INDENT>completions.append(next_completion)<EOL><DEDENT><DEDENT><DEDENT>debug("<STR_LIT>", completions)<EOL><DEDENT>elif not isinstance(active_action, argparse._SubParsersAction):<EOL><INDENT>pass<EOL>"""<STR_LIT>"""<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return completions<EOL>
Visits the active parsers and their actions, executes their completers or introspects them to collect their
option strings. Returns the resulting completions as a list of strings.

This method is exposed for overriding in subclasses; there is no need to use it directly.