将参数传递给结构任务
从命令行调用“fab”时,如何将参数传递给结构任务?例如:
def task(something=''):
print "You said %s" % something
$ fab task "hello"
You said hello
Done.
是否可以在不使用fabric.operations.prompt
提示的情况下执行此操作?
How can I pass a parameter to a fabric task when calling "fab" from the command line? For example:
def task(something=''):
print "You said %s" % something
$ fab task "hello"
You said hello
Done.
Is it possible to do this without prompting with fabric.operations.prompt
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Fabric 2 任务参数文档:
http: //docs.pyinvoke.org/en/latest/concepts/invoking-tasks.html#task-command-line-arguments
Fabric 1.X 使用以下语法将参数传递给任务:
您可以在 中阅读更多相关信息结构文档。
Fabric 2 task arguments documentation:
http://docs.pyinvoke.org/en/latest/concepts/invoking-tasks.html#task-command-line-arguments
Fabric 1.X uses the following syntax for passing arguments to tasks:
You can read more about it in Fabric docs.
在 Fabric 2 中,只需将参数添加到任务函数中即可。例如,要将
version
参数传递给任务deploy
:按如下方式运行:
Fabric 甚至会自动记录选项:
In Fabric 2, simply add the argument to your task function. For example, to pass the
version
argument to taskdeploy
:Run it as follows:
Fabric even documents the options automatically:
Fabric 1.x 参数可以通过非常基本的字符串解析来理解,因此您必须小心发送它们的方式。
以下是向以下测试函数传递参数的不同方法的一些示例:
我在这里使用双引号将 shell 排除在等式之外,但单引号对于某些平台可能更好。另请注意结构认为分隔符的字符的转义。
文档中的更多详细信息:
http://docs.fabfile.org/en/ 1.14/usage/fab.html#per-task-arguments
Fabric 1.x arguments are understood with very basic string parsing, so you have to be a bit careful with how you send them.
Here are a few examples of different ways to pass arguments to the following test function:
I use double quote here to take the shell out of the equation, but single quotes may be better for some platforms. Also note the escapes for characters that fabric considers delimiters.
More details in the docs:
http://docs.fabfile.org/en/1.14/usage/fab.html#per-task-arguments
您需要将所有 Python 变量作为字符串传递,特别是如果您使用子进程来运行脚本,否则您将收到错误。您需要分别将变量转换回 int/boolean 类型。
You need to pass all Python variables as strings, especially if you are using sub-process to run the scripts, or you will get an error. You will need to convert the variables back to int/boolean types separately.
如果有人希望将参数从 Fabric2 中的一个任务传递到另一个任务,只需使用环境字典即可:
然后运行:
If someone is looking to pass parameters from one task to another in fabric2, just use the environment dictionary for that:
And run: