Python command completion tool argcomplete

ceee1e33a62d74d3ab6921e628c8e0df.png

(Forever free, scan code to join)

Author: Yunfeng Wang

https://juejin.cn/post/7242676843987124279

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 [1] 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 have 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 a little more comfortable.

References

[1]

argcomplete: https://github.com/kislyuk/argcomplete

Finally, I would like to recommend our member group, currently there are venture capital angel investors, headhunters HR, Douyin big V, emotional bloggers, lawyers, psychological counselors, medical sales, real estate, insurance, piano teachers, operators, business consulting, Students from cross-border e-commerce, construction, data analysts in the Internet industry, back-end development, python testing and other industries will join.

WeChat consultation: coder_v5 (be sure to note your intention)

Great value for money planet

At present, there are 430+ people on the planet, and 41 cheats have been updated in the content of the column. Every day, planets publish their own experiences. You can learn for only one dollar:

Python: 44 lessons of python introductory course + 9 lessons of Django column + interesting practical cases

chatgpt: entry, advanced, fun office, advanced courses

AI painting: Mj's basics, entry, advanced, Xiaohongshu gameplay

If you want to learn Python, ChatGPT, and AI painting, and you just want to spend a little money, welcome to join our planet member group, and you can meet a lot of great people!

Join to send ChatGPT independent account

993449669c2f7ef68fb99d4aae1684d6.jpeg

Also send ChatGPT advanced video courses

The original price is 99, and now it is free to send planet members

c16943e33427131dd4f7a539a98400c2.jpeg

WeChat long press to try the content

If you are not satisfied within three days, you can directly refund! ! !

34f23b82027cd4e0f3275eb09c823351.png

推荐阅读:
入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影
趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|

Guess you like

Origin blog.csdn.net/cainiao_python/article/details/131587762