这是我的 tasks.json
的一个例子:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "test",
"suppressTaskName": true,
"command": "python",
"args": [
"tests/brewer_tests.py"
],
"isTestCommand": true
}
]
}
我可以用
shift+cmd+alt+b
运行它.我也可以用 alt+t
运行它,然后从菜单中选择它。是否可以在该菜单中传递其他参数?例如![enter image description here](/image/Lg4cA.png)
你可以像这样将它构建到你的任务中:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "test",
"suppressTaskName": true,
"command": "python",
"args": [
"tests/brewer_tests.py",
$arg1 # would resolve to "ARG1"
],
"isTestCommand": true
}
]
}
或者类似的东西?
最佳答案
我使用了来自 this answer 的解决方案直到现在,但由于 Visual Studio Code 现在有 official support for task prompts我会在这里添加它作为答案。
在您的 tasks.json 文件中,您添加 key inputs
在您的旁边 tasks
.该键包含一个包含所有可能参数的数组。请注意,并非每个任务都必须使用所有这些输入。
所有这些输入都有一个 id
,您将使用它来引用任务中的输入。
现在,您只需要在任务中添加 ${input:myInputId}
无论您在哪里需要参数。
例子:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo param",
"type": "shell",
"command": "echo ${input:param1}",
"problemMatcher": []
},
{
"label": "Echo without param",
"type": "shell",
"command": "echo Hello",
"problemMatcher": []
},
],
"inputs": [
{
"id": "param1",
"description": "Param1:",
"default": "Hello",
"type": "promptString"
},
]
}
任务
Echo param
将打开一个提示,让你输入一个字符串值,然后它会打印这个值。任务Echo without param
将简单地打印“你好”。
关于visual-studio-code - 是否可以将参数传递给 Visual Studio Code 中的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45582698/