将参数传递给结构任务

发布于 2024-12-28 16:27:54 字数 335 浏览 1 评论 0原文

从命令行调用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心是晴朗的。 2025-01-04 16:27:54

Fabric 2 任务参数文档:

http: //docs.pyinvoke.org/en/latest/concepts/invoking-tasks.html#task-command-line-arguments


Fabric 1.X 使用以下语法将参数传递给任务:

 fab task:'hello world'
 fab task:something='hello'
 fab task:foo=99,bar=True
 fab task:foo,bar

您可以在 中阅读更多相关信息结构文档

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:

 fab task:'hello world'
 fab task:something='hello'
 fab task:foo=99,bar=True
 fab task:foo,bar

You can read more about it in Fabric docs.

她如夕阳 2025-01-04 16:27:54

在 Fabric 2 中,只需将参数添加到任务函数中即可。例如,要将 version 参数传递给任务 deploy

@task
def deploy(context, version):
    ...

按如下方式运行:

fab -H host deploy --version v1.2.3

Fabric 甚至会自动记录选项:

$ fab --help deploy
Usage: fab [--core-opts] deploy [--options] [other tasks here ...]

Docstring:
  none

Options:
  -v STRING, --version=STRING

In Fabric 2, simply add the argument to your task function. For example, to pass the version argument to task deploy:

@task
def deploy(context, version):
    ...

Run it as follows:

fab -H host deploy --version v1.2.3

Fabric even documents the options automatically:

$ fab --help deploy
Usage: fab [--core-opts] deploy [--options] [other tasks here ...]

Docstring:
  none

Options:
  -v STRING, --version=STRING

Fabric 1.x 参数可以通过非常基本的字符串解析来理解,因此您必须小心发送它们的方式。

以下是向以下测试函数传递参数的不同方法的一些示例:

@task
def test(*args, **kwargs):
    print("args:", args)
    print("named args:", kwargs)

$ fab "test:hello world"
('args:', ('hello world',))
('named args:', {})

$ fab "test:hello,world"
('args:', ('hello', 'world'))
('named args:', {})

$ fab "test:message=hello world"
('args:', ())
('named args:', {'message': 'hello world'})

$ fab "test:message=message \= hello\, world"
('args:', ())
('named args:', {'message': 'message = hello, world'})

我在这里使用双引号将 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:

@task
def test(*args, **kwargs):
    print("args:", args)
    print("named args:", kwargs)

$ fab "test:hello world"
('args:', ('hello world',))
('named args:', {})

$ fab "test:hello,world"
('args:', ('hello', 'world'))
('named args:', {})

$ fab "test:message=hello world"
('args:', ())
('named args:', {'message': 'hello world'})

$ fab "test:message=message \= hello\, world"
('args:', ())
('named args:', {'message': 'message = hello, world'})

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

尐偏执 2025-01-04 16:27:54

您需要将所有 Python 变量作为字符串传递,特别是如果您使用子进程来运行脚本,否则您将收到错误。您需要分别将变量转换回 int/boolean 类型。

def print_this(var):
    print str(var)

fab print_this:'hello world'
fab print_this='hello'
fab print_this:'99'
fab print_this='True'

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.

def print_this(var):
    print str(var)

fab print_this:'hello world'
fab print_this='hello'
fab print_this:'99'
fab print_this='True'
故事↓在人 2025-01-04 16:27:54

如果有人希望将参数从 Fabric2 中的一个任务传递到另一个任务,只需使用环境字典即可:

@task
def qa(ctx):
  ctx.config.run.env['counter'] = 22
  ctx.config.run.env['conn'] = Connection('qa_host')

@task
def sign(ctx):
  print(ctx.config.run.env['counter'])
  conn = ctx.config.run.env['conn']
  conn.run('touch mike_was_here.txt')

然后运行:

fab2 qa sign

If someone is looking to pass parameters from one task to another in fabric2, just use the environment dictionary for that:

@task
def qa(ctx):
  ctx.config.run.env['counter'] = 22
  ctx.config.run.env['conn'] = Connection('qa_host')

@task
def sign(ctx):
  print(ctx.config.run.env['counter'])
  conn = ctx.config.run.env['conn']
  conn.run('touch mike_was_here.txt')

And run:

fab2 qa sign
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文