如何在 Python fabric 中将 warn_only 选项与 "local" 命令一起使用?

How to use warn_only option with "local" command in Python fabric?

将 warn_only 与 "run" 和 "sudo" 一起使用是可以的。但是使用 "local",它会产生以下错误:

TypeError: local() got an unexpected keyword argument 'warn_only'

我应该改用 env.warn_only=True 吗?

不,那是那种你忘记了然后又来咬你屁股的事情。如果你能完全避免访问 env 至少在你还在熟悉 fabric 的时候会很棒。相反,我谦虚地建议您使用 with setting(warn_only=True) 这样您只会暂时覆盖它。

@task
@setting(warn_only=True)
def task_that_always_throws_warning():
    local('sudo ....')
    local('sudo ....') # if this throws an error you will NOT know about it


@task
def task_that_always_throws_warning_but_i_need_to_catch_the_second_local():
    with setting(warn_only=True):
        local('sudo ....')
    local('sudo ....') # if this throws an error you will know about it

你也可以这样做,(虽然我不推荐这样做):

env.warn_only=True

@task
def task_that_always_throws_warning_but_i_need_to_catch_the_second_local():
    local('sudo ....')
    with setting(warn_only=False):
        local('sudo ....') # if this throws an error you will know about it

底线是你可以随心所欲地做,"im" 不重写系统默认值的忠实拥护者。