How to use the argcomplete.split_line 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 codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
def complete_command(self, command):
        """
        Given a command string, return a list of suggestions to complete the last token.
        """
        parser = Commands.build_parser(self)
        cf = argcomplete.CompletionFinder(parser)
        cword_prequote, cword_prefix, _, comp_words, first_colon_pos = argcomplete.split_line(command, len(command))

        # Strip whitespace and parse according to shell escaping rules
        try:
            clean = lambda s: shlex.split(s.strip())[0] if s else ''
        except ValueError as e:
            raise UsageError(e.message)
        return map(clean, cf._get_completions(comp_words, cword_prefix, cword_prequote, first_colon_pos))
github NitorCreations / nitor-deploy-tools / n_utils / ndt.py View on Github external
ifs = os.environ.get("_ARGCOMPLETE_IFS", "\v")
    if len(ifs) != 1:
        sys.exit(1)
    current = os.environ["COMP_CUR"]
    prev = os.environ["COMP_PREV"]
    comp_line = os.environ["COMP_LINE"]
    comp_point = int(os.environ["COMP_POINT"])

    # Adjust comp_point for wide chars
    if USING_PYTHON2:
        comp_point = len(comp_line[:comp_point].decode(SYS_ENCODING))
    else:
        comp_point = len(comp_line.encode(SYS_ENCODING)[:comp_point].decode(SYS_ENCODING))

    comp_line = ensure_str(comp_line)
    comp_words = split_line(comp_line, comp_point)[3]
    if "COMP_CWORD" in os.environ and os.environ["COMP_CWORD"] == "1":
        keys = [x for x in list(COMMAND_MAPPINGS.keys()) if x.startswith(current)]
        output_stream.write(ifs.join(keys).encode(SYS_ENCODING))
        output_stream.flush()
        sys.exit(0)
    else:
        command = prev
        if len(comp_words) > 1:
            command = comp_words[1]
        if command not in COMMAND_MAPPINGS:
            sys.exit(1)
        command_type = COMMAND_MAPPINGS[command]
        if command_type == "shell":
            command = command + ".sh"
        if command_type == "ndtshell":
            command = command + ".sh"
github wapiflapi / gxf / gxf / commands.py View on Github external
def complete(self, text, word):

        # We never want to fallback on readline.
        # Even more so when argcomplete does this by calling bash -c compgen.
        check_output = argcomplete.subprocess.check_output
        argcomplete.subprocess.check_output = lambda *a, **k: b""

        (cword_prequote, cword_prefix, cword_suffix,
         comp_words, first_colon_pos) = argcomplete.split_line(text)

        comp_words.insert(0, sys.argv[0])

        try:
            completions = self.completer._get_completions(
                comp_words, cword_prefix, cword_prequote, first_colon_pos)
        except GdbCompleterRequired as e:
            return e.gdbcompleter
        except BaseException as e:
            # This is not expected, but gdb doesn't give us a
            # backtrace in this case, we print it ourself.
            print("\nAn exception occured during auto-completion code.")
            print("%s> %s %s" % (traceback.format_exc(), self.cmdname, text),
                  end="")
            return []
        finally:
github romankh / gsm-assessment-toolkit / core / common / completer.py View on Github external
def __complete(self, line, parser):
        comp_line = line
        comp_point = len(line)
        cword_prequote, cword_prefix, cword_suffix, comp_words, first_colon_pos = argcomplete.split_line(comp_line,
                                                                                                         comp_point)
        # reset display strings for options
        self.option_display_str = []

        # actions already chosen, we don't provide them anymore
        visited_actions = []

        # list of possible completion values
        completions = []

        # is the last action completed ?
        last_action_finished = True

        # are we completing a subcommand ?
        is_sub_command_active = False
github NitorCreations / nitor-deploy-tools / n_utils / cf_bootstrap.py View on Github external
def create_stack():
    """ Create a stack from a template
    """
    parser = argparse.ArgumentParser(description=create_stack.__doc__, add_help=False)
    _add_default_params(parser)
    parser.add_argument("-h", "--help", action='store_true')
    if "_ARGCOMPLETE" in os.environ:
        ac_args = argcomplete.split_line(os.environ['COMP_LINE'],
                                         os.environ['COMP_POINT'])[3]
        if len(ac_args) >= 2:
            template = ac_args[1]
            template_yaml = load_template(template)
            if template_yaml and "ContextClass" in template_yaml:
                context = load_class(template_yaml["ContextClass"])()
                context.add_context_arguments(parser)
    argcomplete.autocomplete(parser)
    args, _ = parser.parse_known_args()
    if args.template:
        template_yaml = load_template(args.template)
        if "ContextClass" in template_yaml:
            context = load_class(template_yaml["ContextClass"])()
            template_yaml.pop("ContextClass", None)
            parser = argparse.ArgumentParser(description=context.__doc__)
            _add_default_params(parser)