iterative / shtab

↔️ Automagic shell tab completion for Python CLI applications
https://docs.iterative.ai/shtab
Other
362 stars 35 forks source link

zsh: File completion shows directories, directory completion shows files #127

Closed JanEbbing closed 1 year ago

JanEbbing commented 1 year ago

Hi, I'm adding shtab support to our python CLI application via the "Library usage" mode. While doing so, I noticed what I think is a bug and can reproduce in the minimal pathcomplete example.

Env:

Setup: Make new empty folder, start a new venv, install shtab. Make a folder pathcomplete, inside a file main.py which contains the pathcomplete.py example code. Notably, a positional argument for an input file and a --dir argument for a directory. python pathcomplete -s zsh > ~/zsh_completions/_pathcomplete (which is where I store my completion files for zsh, it's on my fpath) Fill that with some sample files and directories with mkdir and touch. I called all files file_X, all directories dir_Y.

Checking it works: Type python pathcomplete - in zsh, press TAB, I get

--dir                   --
--help              -h  -- show this help message and exit
--print-completion  -s  -- print shell completion script

The bug/problem: python pathcomplete, then TAB, gives me

dir_2/           dir_4/           file_1           file_3           file_5           pathcomplete/
dir_1/           dir_3/           dir_5/           file_2           file_4           main.py          venv/

Expected: Shouldn't this filter out all folders, since it expects the positional argument now?

python pathcomplete --d, then TAB, autocompletes to --dir, another TAB gives me

dir_2/           dir_4/           file_1           file_3           file_5           pathcomplete/
dir_1/           dir_3/           dir_5/           file_2           file_4           main.py          venv/

Again, it should filter out the files here? Am I doing something wrong? (I also tried --dir=, but to no avail).

casperdcl commented 1 year ago
  1. wanted: just files, got: both folders and files: this is expected behaviour (see e.g. shell builtins like cat <TAB>) as the folders may actually contain files so need to be in the completion list.
  2. wanted: just folders, got: both folders and files: that sounds like a bug. However I can't reproduce it...
shtab (main)$ PYTHONPATH=$PWD python examples/pathcomplete.py -s zsh > ~/zsh_completions/_pathcomplete
shtab (main)$ pathcomplete --dir <TAB>
 -- dir --
build/           examples/        meta/            shtab.egg-info/
docs/            feedstock/       shtab/           tests/

maybe you have an old version of completions installed somewhere?

JanEbbing commented 1 year ago

Thanks, I got it to work with your example. The autocomplete seems to work even if you use python pathcomplete ... for arguments (like --dir), but the file-restriction doesn't work for that. It works for pathcomplete, however.