80命令行应用程序开发

import click

@click.command()
@click.option("--text",default="hello",help="the text to print")
def fun1(text):
    """Run the main program."""
    print(text)

if __name__ == '__main__':
    fun1()

 

输出如下:

(base) (.venv) PS D:\cx_dev\cx_git\chatgpt_server\fast-api\myfile> python cmd_text.py --help  
Usage: cmd_text.py [OPTIONS]

  Run the main program.

Options:
  --text TEXT  the text to print
  --help       Show this message and exit.
(base) (.venv) PS D:\cx_dev\cx_git\chatgpt_server\fast-api\myfile> python .\cmd_text.py
hello
(base) (.venv) PS D:\cx_dev\cx_git\chatgpt_server\fast-api\myfile> python .\cmd_text.py --text 1
1
(base) (.venv) PS D:\cx_dev\cx_git\chatgpt_server\fast-api\myfile> python .\cmd_text.py --text asdfa
asdfa

 

click 功能非常强大,可以组合很复杂的命令,上面这个例子只是基础使用介绍

 

 

 

posted @ 2025-07-07 18:05  冷夜O  阅读(6)  评论(0)    收藏  举报