命令 CLI 参数

与具有单个命令的 CLI 应用程序一样,子命令(或简称“命令”)也可以有自己的CLI 参数

import typer

app = typer.Typer()


@app.command()
def create(username: str):
    print(f"Creating user: {username}")


@app.command()
def delete(username: str):
    print(f"Deleting user: {username}")


if __name__ == "__main__":
    app()
// Check the help for create
$ python main.py create --help

Usage: main.py create [OPTIONS] USERNAME

Options:
  --help  Show this message and exit.

// Call it with a CLI argument
$ python main.py create Camila

Creating user: Camila

// The same for delete
$ python main.py delete Camila

Deleting user: Camila

提示

命令右侧的所有内容都是该命令的CLI 参数CLI 参数CLI 选项)。

技术详情

实际上,它是在该命令右侧的所有内容,在任何子命令之前

可以有子命令组,就像一个命令也有子命令一样。然后,这些子命令可以有自己的CLI 参数,采用自己的CLI 参数

您将在另一部分中稍后了解它们。