shtab icon indicating copy to clipboard operation
shtab copied to clipboard

Path completion overrides subparser completion

Open Simon-1-1 opened this issue 3 years ago • 5 comments

My application has a subparser called "build" and the application is generally used inside a directory that contains another directory named "build". I want the subparser to be completed right after the application, like this:

my_app bui
<tab>
my_app build

but instead, I get:

my_app bui
<tab>
my_app build/

That is, there's an unwanted slash at the end, as if it tries to complete the directory. I haven't added shtab.FILE or shab.DIR to the base parser

Simon-1-1 avatar Feb 21 '22 09:02 Simon-1-1

Interesting... what shell is this on?

casperdcl avatar Feb 23 '22 10:02 casperdcl

Bash

Simon-1-1 avatar Feb 23 '22 10:02 Simon-1-1

I think the reason is that we put the filenames option on the final call to complete: https://github.com/iterative/shtab/blob/7f25585e4b7a5552daeb1cc03d857108332f1a86/shtab/init.py#L431

I tried removing it but of course filepath completion won't work when it should in that case. Then I tried reverting this change: https://github.com/iterative/shtab/pull/38/files#diff-495887aece10efc2f1fd9b8dcc23f4c1d445a353018727c6c210c19af8fc4c43L19

and adding the nospace option to complete where the filenames option was before.

Now there is file path completion and my original issue with "build" is solved. The problem is that the nospace option affects normal flags as well, not just incomplete paths. So I have to press space a lot.

So the current behaviour is:

$ my_app bui
<tab>
$ my_app build 

and

$ my_app --give-me-a-path ~/files/com
<tab>
$ my_app --give-me-a-path ~/files/completed

without the nospace option, I'd get a space after completed, even if completed was a directory and I wanted to provide a file within it. This is weird because I added back the -S flag, like I mentioned before:

compgen -f -S "/" -- $1  # files

Simon-1-1 avatar Feb 23 '22 13:02 Simon-1-1

Cannot reproduce anymore. Not sure what causes the difference from before but one change is a newer Ubuntu version, and possibly a new bash version as a result of that. Closing.

Simon-1-1 avatar Feb 14 '24 10:02 Simon-1-1

Nope, spoke too soon. Sorry about that. My CWD didn't have a folder named "build" as I had thought. When I create the folder I still see the bug. Re-opening.

Reproduced on two different Ubuntu versions. 23.10 and 22.10.

Simon-1-1 avatar Feb 14 '24 10:02 Simon-1-1