Pip with shell completion

I am writing a command line tool in python and use pip to distribute it. I wrote several scripts (one for bash and one for zsh) to allow the tool to have tab completion. Is there a way to get pip to install these scripts when someone installs a pip installation?

For example: I have a complete.bash file. When someone does

pip install mypackage

It will also send a bash file.

I'm sure I can do this for linux and bash by placing the script in the data_files section of the setup.py script file.

data_file=[ ('/etc/bash_completion.d', ['bin/completion.bash']) ], 

But how can I do this, so it is platform and shell independent? I need it to work for mac / linux in both bash and zsh. If possible, even support windows.

Is it possible? And if so, how?


In case that matters, here is my bash script:

 _foo_complete() { COMPREPLY=() local words=( "${COMP_WORDS[@]}" ) local word="${COMP_WORDS[COMP_CWORD]}" words=("${words[@]:1}") local completions="$(foo completer --cmplt=\""${words[*]}"\")" COMPREPLY=( $(compgen -W "$completions" -- "$word") ) } complete -F _foo_complete foo 

I am currently installing it simply by running source completion.bash

+5
source share
1 answer

You ask a few different questions.

Firstly, there is no cross-platform or cross-shell solution for defining custom shell add-ons. The one you posted works for bash, but in tcsh, for example, you use the tcsh complete command, which works differently than bash.

Secondly, searching for files containing these termination definitions during pip install will not do much good. Terminations may work in this session, but you probably want them to take effect in future sessions (i.e. shell calls). To do this, your files must be received each time the shell starts (for example, from within the .bashrc user, in the case of bash ).

This means that โ€œinstallingโ€ your files simply means placing them somewhere, and suggesting that users should send them from the corresponding dot-rc file. Even if you could, you should not try to "force" him. Give your users the option to add them to the dot-rc file if they want.

The best approach would probably be to include termination definitions in your package for a supported shell, for example. complete.bash , complete.tcsh , and God knows that for Windows (sorry, I'm not a Windows user).

+2
source

Source: https://habr.com/ru/post/1203162/


All Articles