I have been struggling with a more general form of the problem. The Python 3.11 code from Kiran N S assumes that whitespace is a delimiter. The problem I am trying to solve is how to get bash-like completion of filenames, dealing with whitespace, quoting, and escapes. So if I have files with names "f 1" and "f 2", then I want
ls "f<tab>
to add a space, and then another to tell me about "f 1" and "f 2". Bash presumably uses readline, but I haven't been able to figure out how to do this through the readline module in Python.
One problem is that I can't see a set of delimiters that is correct in all cases. In this input:
ls "f<space>tab>
the space is not a delimiter, it is part of the text to be completed. But for this input:
ls abc
spaces are delimiters.
I suppose I could parse the line, and modify the tokenizer with delimiters for the input I'm seeing, but that feels wrong to me. And I haven't tried it yet so I don't even know that it's workable. On the other hand, I can't come up with anything better.