98
從命令行調用「fab」時,如何將參數傳遞到結構任務?例如:將參數傳遞到結構任務
def task(something=''):
print "You said %s" % something
$ fab task "hello"
You said hello
Done.
是否有可能做到這一點而不fabric.operations.prompt
提示?
從命令行調用「fab」時,如何將參數傳遞到結構任務?例如:將參數傳遞到結構任務
def task(something=''):
print "You said %s" % something
$ fab task "hello"
You said hello
Done.
是否有可能做到這一點而不fabric.operations.prompt
提示?
面料使用以下語法傳遞參數給任務:
fab task:'hello world'
fab task:something='hello'
fab task:foo=99,bar=True
fab task:foo,bar
您可以在Fabric docs閱讀更多關於它。
您需要將所有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'
引號是沒有必要的;所有的參數都是字符串:「因爲這個過程涉及到字符串解析,所以所有的值都會以Python字符串結尾,因此需要做相應的規劃(我們希望在未來的Fabric版本中進行改進,只要可以找到直觀的語法)。」 –
「hello world」周圍的引用似乎有必要嗎? – PEZ
@PEZ如果這是真的,那麼引用在這個例子中可能是必須的,因爲終端或結構的命令行解析器會看到空間並認爲這是該任務的所有內容的結束,並且「世界」是一項新任務。 –