Python command completion tool argcomplete

1 Overview

When using Python commands or Python command-line tools, one pain point is that there is no completion. For example, if you enter the package name after python -m, there will be no prompt. Every time you want to run an http server, you need to search for the package name of the http service. In addition, there are no prompts for commands like pip and pipx, which is not very convenient to use.

I came across the argcomplete library by chance, and you can add auto-completion to the Python command line by pressing the tab key, which is simply an artifact of using Python.

Specifically, argcomplete has the following characteristics:

  • Official support supports bash and zsh two shells, tcsh and fish are supported by third-party contributors (sorry Windows users are treated as second-class citizens here again)

  • Can complete python commands and pip commands

  • Any other command of a third-party package parsed by argparse can be automatically completed, just add a few lines of code of argcomplete

The following describes how to enable auto-completion for existing tools, and how to make your own Python package support argcomplete.

2. Enable autocompletion for Python and pip

First install argcomplete through the pip command:

pip install argcomplete

Then execute the following statement to enable autocompletion for Python and pip:

activate-global-python-argcomplete

Restart the Shell, try to enter pip and press tab, and all command options will be listed.

3. How to enable auto-completion for other third-party libraries

The command-line programs of some libraries already support argcomplete completion, just use the following command to activate:

eval "$(register-python-argcomplete <python-app-name>)"

For example, after the pipx package is installed, a command-line program pipx will be installed in the system, and pipx already supports argcomplete, we can use the following command to activate auto-completion:

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

After activation, enter pipx in and press the tab key to list all pipx commands starting with in, and then press the tab key to switch between candidate commands.

⚠️ Note: This activation command is only effective for programs that already support the argcomplete statement in the code. If there are no such statements in the code, it will not take effect.

4. How to make your own Python library support auto-completion

Just add the following lines of code to enable autocompletion on the command line of your library:

# 在ArgumentParser对象初始化前增加这两行
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse

# 原有代码
parser = argparse.ArgumentParser() 
...

# 在调用parse_args()函数前增加这一行
argcomplete.autocomplete(parser)

# 原有代码
args = parser.parse_args()
...

Then after your package is installed, the corresponding command line program can be completed with eval "$(register-python-argcomplete )".

⚠️Note: If it takes a long time for the program to execute to the place where argcomplete.autocomplete() is called, the user will experience a significant delay when pressing tab. So try to put some time-consuming operations behind the argcomplete.autocomplete() statement, such as some import statements, which are often time-consuming and can be put later.

Hope this program will make your Python development more comfortable.

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/132542958