You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Shell completion does't parse escaped slashes or spaces correctly #1708
When a value with an escape string, for instance my\ file, is passed to a click.ParamType, the incomplete value which is passed to click.ParamType.shell_complete will only be "my\ " instead of my file.
changed the title [-]Shell completion does not handle escape sequences[/-][+]Shell completion does't parse escaped slashes or spaces correctly[/+]on Nov 9, 2020
I've updated the issue with a simpler example and direct explanations for expected and actual behavior.
The issue seems to be with how split_arg_string handles slashes and spaces. It's trying to emulate how sys.argv would be populated to apply it to the CWORDS env var, but doesn't get it right in all cases.
Adding some debugging code to ZshComplete.get_completion_args, when my\ f is completed:
os.environ['COMP_WORDS']='example my\\ f'
cwords=['example', 'my\\', 'f']
cword=1
args=[]
incomplete='my\\'
The shell is passing my\ a, which is the Python string "my\\ f". This is getting parsed into two values "my\\" and "f". Instead, it should get parsed into one value "my\\ f".
You can find some other weird behaviors by tab completing earlier, or with a single opening quote, or within quotes. The splitter should be able to handle these.
That's about the extent of the time I can devote to this for now though. Your best bet for getting this fixed before 8.0 is released is to make a PR.
Ok, thanks. Yes, that example was a bit of a mess.
From a quick look, split_arg_string attempts to do something similar to the quite complex shlex.split. Any reason why you are not using the latter? Bad performance?
Those two examples should be the only cases where shlex.split gets into a failed state, so I think handling those cases with shlex.shelx should be enough.
Hopefully. Also, unpaired quotes should ideally be preserved during the completion process: An input of my\ f should be completed to my\ file while an input of 'my f should be completed to 'my file'.
I'll need to think a bit about this and see if can submit a PR that fixes more than it breaks. But not tonight, it's already late here.
Activity
davidism commentedon Nov 9, 2020
samschott commentedon Nov 9, 2020
[-]Shell completion does not handle escape sequences[/-][+]Shell completion does't parse escaped slashes or spaces correctly[/+]davidism commentedon Nov 9, 2020
I've updated the issue with a simpler example and direct explanations for expected and actual behavior.
The issue seems to be with how
split_arg_stringhandles slashes and spaces. It's trying to emulate howsys.argvwould be populated to apply it to theCWORDSenv var, but doesn't get it right in all cases.Adding some debugging code to
ZshComplete.get_completion_args, whenmy\ fis completed:The shell is passing
my\ a, which is the Python string"my\\ f". This is getting parsed into two values"my\\"and"f". Instead, it should get parsed into one value"my\\ f".You can find some other weird behaviors by tab completing earlier, or with a single opening quote, or within quotes. The splitter should be able to handle these.
That's about the extent of the time I can devote to this for now though. Your best bet for getting this fixed before 8.0 is released is to make a PR.
samschott commentedon Nov 9, 2020
Ok, thanks. Yes, that example was a bit of a mess.
From a quick look,
split_arg_stringattempts to do something similar to the quite complexshlex.split. Any reason why you are not using the latter? Bad performance?davidism commentedon Nov 9, 2020
Because
shlex.splitexpects a complete command line string, but completion by definition needs to work with an incomplete string.davidism commentedon Nov 9, 2020
You might be able to get around that by manually iterating over a
shlex.shlexobject and doing some cleanup when it raises an error.samschott commentedon Nov 9, 2020
I was about to propose exactly that! Any remainder after a
ValueErrorcan be taken as an incomplete argument.davidism commentedon Nov 10, 2020
Those two examples should be the only cases where
shlex.splitgets into a failed state, so I think handling those cases withshlex.shelxshould be enough.samschott commentedon Nov 10, 2020
Hopefully. Also, unpaired quotes should ideally be preserved during the completion process: An input of
my\ fshould be completed tomy\ filewhile an input of'my fshould be completed to'my file'.I'll need to think a bit about this and see if can submit a PR that fixes more than it breaks. But not tonight, it's already late here.