python实现交互命令行
2019-05-29 本文已影响0人
Duckweed丶Yang
使用prompt_toolkit打造交互式命令工具
1.安装prompt_toolkit库
pip install prompt_toolkit
2.使用prompt_toolkit库
![](https://img.haomeiwen.com/i13975392/c5df14620ad4b938.png)
3.实现上下键位的命令提示功能
调用prompt_toolkit.history模板下的FileHistory类
![](https://img.haomeiwen.com/i13975392/b17591c3a6d0f8b0.png)
查看当前目录下的文件,便可以发现上述程序制定的history.txt文件
![](https://img.haomeiwen.com/i13975392/79183cd3dacfe9d9.png)
4.实现输入提示功能
既然已经拥有了查看历史输入的功能,我们可以通过auto_suggest 模块下的AutoSuggestFromHistory类从输入记录里为用户进行提示功能
![](https://img.haomeiwen.com/i13975392/366562879ce341b3.png)
![](https://img.haomeiwen.com/i13975392/c830b23ee7e2c845.png)
测试结果如上,输入789后,我们再次输入7,其后就会用浅色给我们提示历史输入