kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.39k stars 129 forks source link

Passing input arguments to argcomplete #344

Closed naba-nyan closed 2 years ago

naba-nyan commented 3 years ago

Hi, all contributors to argcomplete!

In my test program (named testarc),

import sys

import argparse
import argcomplete

input_args = sys.argv
parser = argparse.ArgumentParser()

parser.add_argument("--dir")

top_arg, remaining_args = parser.parse_known_args(input_args[1:])
print("top_arg = {}, remaining_args = {}".format(top_arg,remaining_args))

subparsers = parser.add_subparsers
parser.add_argument("--foo")

argcomplete.autocomplete(parser)

completions woks fine. However, in debug mode (_ARC_DEBUG), I notice that the arguments for this program is not passed to argcomplete. Indee, in my bash, testarc --dir mydir --foo provides top_args = Namespace(dir='mydir'), remainging_args = [--foo]. However, at testarc --dir mydir, I push , then I have the following debug message:

top_arg = Namespace(dir=None), remaining_args = []

word foo split, lexer state: ' '
In trailing whitespace

LINE: 'testarc --dir mydir '
POINT: 18
PREQUOTE: ''
PREFIX: ''
SUFFIX: ''
WORDS: ['testarc', '--dir', 'mydir']

Here, I want the input arguments to be passed to argcomplete. For example, in my environment, the completion for parse.add_argument("--foo").completer = SomeClass(top_arg) does not work fine since top_arg is Namespace(dir=None) in argcomplete.

Are there any solutions to this? Or if there are any mistakes, please report me!

evanunderscore commented 3 years ago

Thanks for the report. You're correct that arguments are not passed via the command line, only via the COMP_LINE environment variable. I'm not sure whether argcomplete could be updated to also pass arguments via the command line using e.g. COMP_WORDS. You could try altering the completion function and see what it does, e.g. removing the end of the slice here or adding the contents of COMP_WORDS here. However, it would probably need a lot of thought and testing before such a change could be merged back in.

kislyuk commented 2 years ago

Thanks for your interest in argcomplete. Please refer to this quote in the argcomplete documentation (https://github.com/kislyuk/argcomplete#argcompleteautocompleteparser):

argcomplete.autocomplete(parser) This method is the entry point to the module. It must be called after ArgumentParser construction is complete, but before the ArgumentParser.parse_args() method is called. The method looks for an environment variable that the completion hook shellcode sets, and if it's there, collects completions, prints them to the output stream (fd 8 by default), and exits. Otherwise, it returns to the caller immediately.

argcomplete does not support programs that call the parser.parse_args() or parser.parse_known_args() method before argcomplete.autocomplete(parser). argcomplete's behavior in such programs is undefined.