如何解决一个简单的点击工具的 python 语法错误?

How to solve python syntax error for a simple click tool?

我正在为 CLI 创建一个简单的点击工具。代码如下所示:

import click

@click.command(help="This is just a hello app. It does nothing.")   #click menu setup and help show
@click.option("--name", prompt="I need your name", help="Need name")    #something that accepts the parameters as options then probbaly a help message
@click.option("--color", prompt="I need your color", help="This is your color") #color setup with a prompt for color request and help clause as well
def hello(name, color):
    if name == "Samuel":
        click.echo("Samuel, you are always DevOps blue.")
        click.echo(click.style(f"Hello {name}!", fg="blue"))
    else:
        else:
            click.echo(f"Your color is {color}!")
            click.echo(click.style(f"Hello {name}!", fg=color))

if __name__ == "__main__":      #only run this block if it runs as a script and it's close to having a CLI
    hello()

在 运行 这个命令 python2 click.py 之后,结果错误如下所示:

File "click.py", line 9
    click.echo(click.style(f"Hello {name}!", fg="blue"))
                                          ^
SyntaxError: invalid syntax

有人可以帮忙吗?

python2 click.py 表示您 运行 使用 Python 2,但是像 f"Hello {name}!" 这样的 f-strings 仅可用 since Python 3.6,所以你应该 运行 这个代码用 Python 3 代替。

要与 Python 2 保持兼容,请使用 str.format:

"Hello {}!".format(name)