如何使用python写一个命令行工具?

类似 systemctl start myservice 后台启动后,程序初始化后,就可以在linux界面输入 myservice command arg.. 这样来完成一些工作。希望了解以下大致的实现过程,以及能帮得上忙的模块或者教程,我自己再去研究一下。
主要是想把现在零散的运维工作汇集一下,能灵活得管理工作中使用的各个脚本、程序

阅读 5.8k
4 个回答

搜索下python daemon就可以找到一堆。

如果想更普遍一点弄个:
先开发个server端,开个命名管道等待cli client接入发送指令。
再开发个简单的cli client把指令通过命名管道发给server。命令解析用argpare就可以了

可以使用谷歌出品的Fire构建命令行/

github地址:https://github.com/google/pyt...
官方文档介绍如下。

Python Fire is a library for creating command line interfaces (CLIs) from absolutely any Python object.
Python Fire is a simple way to create a CLI in Python.
Python Fire is a helpful tool for developing and debugging Python code.
Python Fire helps with exploring existing code or turning other people’s code into a CLI.
Python Fire makes transitioning between Bash and Python easier.
Python Fire makes using a Python REPL easier by setting up the REPL with the modules and variables you’ll need already imported and created.

prompt_toolkit # 强大的命令行交互
click # 命令行神器
cli_helpers # 强大的输出更好看

结合以上三种的pg神器pgcli开源全python命令行交互 https://github.com/dbcli/pgcl...

新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题