How to use the argcomplete.compat.ensure_str function in argcomplete

To help you get started, we’ve selected a few argcomplete examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kislyuk / argcomplete / argcomplete / __init__.py View on Github external
#     print(v, os.environ[v], stream=debug_stream)

        ifs = os.environ.get("_ARGCOMPLETE_IFS", "\013")
        if len(ifs) != 1:
            debug("Invalid value for IFS, quitting [{v}]".format(v=ifs))
            exit_method(1)

        dfs = os.environ.get("_ARGCOMPLETE_DFS")
        if dfs and len(dfs) != 1:
            debug("Invalid value for DFS, quitting [{v}]".format(v=dfs))
            exit_method(1)

        comp_line = os.environ["COMP_LINE"]
        comp_point = int(os.environ["COMP_POINT"])

        comp_line = ensure_str(comp_line)
        cword_prequote, cword_prefix, cword_suffix, comp_words, last_wordbreak_pos = split_line(comp_line, comp_point)

        # _ARGCOMPLETE is set by the shell script to tell us where comp_words
        # should start, based on what we're completing.
        # 1: 
github kislyuk / argcomplete / argcomplete / __init__.py View on Github external
def _get_option_completions(self, parser, cword_prefix):
        self._display_completions.update(
            [[tuple(ensure_str(x) for x in action.option_strings
             if ensure_str(x).startswith(cword_prefix)), action.help]
             for action in parser._actions
             if action.option_strings])

        option_completions = []
        for action in parser._actions:
            if not self.print_suppressed:
                completer = getattr(action, "completer", None)
                if isinstance(completer, SuppressCompleter) and completer.suppress():
                    continue
                if action.help == argparse.SUPPRESS:
                    continue
            if not self._action_allowed(action, parser):
                continue
            if not isinstance(action, argparse._SubParsersAction):
                option_completions += self._include_options(action, cword_prefix)
github kislyuk / argcomplete / argcomplete / __init__.py View on Github external
def _include_options(self, action, cword_prefix):
        if len(cword_prefix) > 0 or self.always_complete_options is True:
            return [ensure_str(opt) for opt in action.option_strings if ensure_str(opt).startswith(cword_prefix)]
        long_opts = [ensure_str(opt) for opt in action.option_strings if len(opt) > 2]
        short_opts = [ensure_str(opt) for opt in action.option_strings if len(opt) <= 2]
        if self.always_complete_options == "long":
            return long_opts if long_opts else short_opts
        elif self.always_complete_options == "short":
            return short_opts if short_opts else long_opts
        return []
github kislyuk / argcomplete / argcomplete / __init__.py View on Github external
def filter_completions(self, completions):
        """
        Ensures collected completions are Unicode text, de-duplicates them, and excludes those specified by ``exclude``.
        Returns the filtered completions as an iterable.

        This method is exposed for overriding in subclasses; there is no need to use it directly.
        """
        # On Python 2, we have to make sure all completions are unicode objects before we continue and output them.
        # Otherwise, because python disobeys the system locale encoding and uses ascii as the default encoding, it will
        # try to implicitly decode string objects using ascii, and fail.
        completions = [ensure_str(c) for c in completions]

        # De-duplicate completions and remove excluded ones
        if self.exclude is None:
            self.exclude = set()
        seen = set(self.exclude)
        return [c for c in completions if c not in seen and not seen.add(c)]
github kislyuk / argcomplete / argcomplete / __init__.py View on Github external
def _include_options(self, action, cword_prefix):
        if len(cword_prefix) > 0 or self.always_complete_options is True:
            return [ensure_str(opt) for opt in action.option_strings if ensure_str(opt).startswith(cword_prefix)]
        long_opts = [ensure_str(opt) for opt in action.option_strings if len(opt) > 2]
        short_opts = [ensure_str(opt) for opt in action.option_strings if len(opt) <= 2]
        if self.always_complete_options == "long":
            return long_opts if long_opts else short_opts
        elif self.always_complete_options == "short":
            return short_opts if short_opts else long_opts
        return []
github kislyuk / argcomplete / argcomplete / __init__.py View on Github external
def _include_options(self, action, cword_prefix):
        if len(cword_prefix) > 0 or self.always_complete_options is True:
            return [ensure_str(opt) for opt in action.option_strings if ensure_str(opt).startswith(cword_prefix)]
        long_opts = [ensure_str(opt) for opt in action.option_strings if len(opt) > 2]
        short_opts = [ensure_str(opt) for opt in action.option_strings if len(opt) <= 2]
        if self.always_complete_options == "long":
            return long_opts if long_opts else short_opts
        elif self.always_complete_options == "short":
            return short_opts if short_opts else long_opts
        return []
github kislyuk / argcomplete / argcomplete / __init__.py View on Github external
def _get_option_completions(self, parser, cword_prefix):
        self._display_completions.update(
            [[tuple(ensure_str(x) for x in action.option_strings
             if ensure_str(x).startswith(cword_prefix)), action.help]
             for action in parser._actions
             if action.option_strings])

        option_completions = []
        for action in parser._actions:
            if not self.print_suppressed:
                completer = getattr(action, "completer", None)
                if isinstance(completer, SuppressCompleter) and completer.suppress():
                    continue
                if action.help == argparse.SUPPRESS:
                    continue
            if not self._action_allowed(action, parser):
                continue
            if not isinstance(action, argparse._SubParsersAction):
                option_completions += self._include_options(action, cword_prefix)
        return option_completions