pypa / pipx

Install and Run Python Applications in Isolated Environments
https://pipx.pypa.io
MIT License
10.11k stars 408 forks source link

Support shell completion with pipx.pyz #1072

Closed TomiBelan closed 8 months ago

TomiBelan commented 11 months ago

How would this feature be useful?

Users of pipx.pyz don't get shell completion for pipx. The current instructions printed by pipx completions don't work because the register-python-argcomplete command is missing. This is a limitation compared to "full" installations of pipx made with pip install --user, apt, brew, etc.

To be specific, I mean users who move pipx.pyz to ~/.local/bin/pipx (or similar) and mark it executable. I think it's probably impossible to support completion for users who run it explicitly as python3 /path/to/pipx.pyz ..., so they are out of scope.

Describe the solution you'd like

It turns out this is already possible. It just needs to be documented in pipx completions. Instead of

eval "$(register-python-argcomplete pipx)"

your bash config file must contain:

eval "$(SHIV_CONSOLE_SCRIPT=register-python-argcomplete pipx pipx 2>/dev/null)"

I only tested bash. Someone please test the other shells.

Explanation: SHIV_CONSOLE_SCRIPT tells the shiv bootstrap code to use another script bundled inside pipx.pyz. The first pipx is the program to run. The second pipx is an argument to register-python-complete (telling it what to generate completions for). The 2>/dev/null is necessary to work around linkedin/shiv#240, at least for now.

Describe alternatives you've considered

Another option could be to stop using argcomplete and rewrite the whole pipx CLI code with click. pipx already depends on click indirectly because of userpath. click's completion support does not depend on other programs, so it works nicely with pyz files and doesn't need SHIV_CONSOLE_SCRIPT. However all existing users of completion would have to change their configs.

I don't use pipx completion and I don't personally care about this issue. I'm just filing it because I noticed it doesn't already exist.

gaborbernat commented 9 months ago

PR to document this in the FAQ is welcome.

TomiBelan commented 9 months ago

FAQ? What FAQ?

pipx has the pipx completions command. The command does not edit any shell config files directly, instead it just prints instructions to enable completion. The main goal of this issue is that pipx completions should print working instructions.

I found a working command for Bash but someone needs to test the other shells.

Personally I'm not interested in sending a PR but I encourage anyone to do so. ❤️

gaborbernat commented 9 months ago

Could be under https://pipx.pypa.io/stable/troubleshooting/

TomiBelan commented 9 months ago

Good idea -- preferably in addition to improving the output of pipx completions.

1trapbox commented 8 months ago

How would this feature be useful?

Users of pipx.pyz don't get shell completion for pipx. The current instructions printed by pipx completions don't work because the register-python-argcomplete command is missing. This is a limitation compared to "full" installations of pipx made with pip install --user, apt, brew, etc.

To be specific, I mean users who move pipx.pyz to ~/.local/bin/pipx (or similar) and mark it executable. I think it's probably impossible to support completion for users who run it explicitly as python3 /path/to/pipx.pyz ..., so they are out of scope.

Describe the solution you'd like

It turns out this is already possible. It just needs to be documented in pipx completions. Instead of

eval "$(register-python-argcomplete pipx)"

your bash config file must contain:

eval "$(SHIV_CONSOLE_SCRIPT=register-python-argcomplete pipx pipx 2>/dev/null)"

I only tested bash. Someone please test the other shells.

Explanation: SHIV_CONSOLE_SCRIPT tells the shiv bootstrap code to use another script bundled inside pipx.pyz. The first pipx is the program to run. The second pipx is an argument to register-python-complete (telling it what to generate completions for). The 2>/dev/null is necessary to work around linkedin/shiv#240, at least for now.

Describe alternatives you've considered

Another option could be to stop using argcomplete and rewrite the whole pipx CLI code with click. pipx already depends on click indirectly because of userpath. click's completion support does not depend on other programs, so it works nicely with pyz files and doesn't need SHIV_CONSOLE_SCRIPT. However all existing users of completion would have to change their configs.

I don't use pipx completion and I don't personally care about this issue. I'm just filing it because I noticed it doesn't already exist.

pipx completions Doesn't work for me (installed using asdf, other installation methods have not been tested)

This solved my problem, thanks

pipx install argcomplete

# Zsh (add to .zshrc)
eval "$(register-python-argcomplete pipx)"