VsCode:如何从命令生成自定义任务输入选项?
假设我们有以下自定义任务:
{
"version": "2.0.0",
"tasks": [
{
"label": "do smthg",
"type": "shell",
"command": "echo \"selected option: ${input:option_name}\"",
"problemMatcher": [],
"presentation": {
"panel": "dedicated",
"focus": true
}
}
],
"inputs": [
{
"type": "pickString",
"id": "option_name",
"description": "select an option :",
"options": [
// want possible options to be the output of a command
],
"default": ""
}
]
}
但是我希望可用的选项是命令的结果,
喜欢ls
或cat smthg.txt | grep -op'\“ value \:\” \ k \ w*'
,
我该怎么做?有可能吗?
Let's say we have the following custom task :
{
"version": "2.0.0",
"tasks": [
{
"label": "do smthg",
"type": "shell",
"command": "echo \"selected option: ${input:option_name}\"",
"problemMatcher": [],
"presentation": {
"panel": "dedicated",
"focus": true
}
}
],
"inputs": [
{
"type": "pickString",
"id": "option_name",
"description": "select an option :",
"options": [
// want possible options to be the output of a command
],
"default": ""
}
]
}
But I want the available options to be the result of a command,
like ls
, or a cat smthg.txt | grep -oP '\"value\:\"\K\w*'
,
how can I do that ? Is it only possible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用扩展名
noreferrer”
pickString
命名extension.commandvariable.pickstringremember
的替换。此命令可以从文件中读取选项,您可以用REGEXP确定“任务的问题匹配器”的格式。
一个示例:
如果您没有 label - value 行,则可以简化
模式
,如果您没有静态选项(<代码>始终1/2 在示例中)您可以从
args
中删除选项
属性。如果grep-ping和文件重定向可能是一个问题,则使用命令或shell脚本创建文件(将输出文件名,使用变量作为参数传递到脚本)。
您可以构建一系列任务,一个复合任务,请参阅VSC Doc。
You can use the extension Command Variable v1.34.0
Use the replacement for
pickString
namedextension.commandvariable.pickStringRemember
.This command can read options from a file, you determine the format with a regexp like the problem matcher of tasks.
An example:
If you don't have label - value lines you can simplify the
pattern
toIf you don't have static options (
always 1/2
in example) you can remove theoptions
property fromargs
.With an additional task you create the file with a command or a shell script if grep-ping and file redirection might be a problem (pass the output file name, using variables, as argument to the script).
You can construct a sequence of tasks, a compound task, see VSC doc.
这种粗略方法避免使用自定义或外部扩展。唯一的假设是系统上已经安装了 Python。使用 python 模块 json (假设您的
task.json
),执行“预任务”以修改 inputs 键列表中的 options 键在task.json
中。在此示例中,“主任务”将在“预任务”运行后使用终端中的 echo 向更新的输入选项中选定的用户说“嗨”。一个目录有一组遵循命名方案 USER_[insert_username] 的文件夹:使用 python,“前置任务”将扫描与此架构匹配的目录并更新选项 键作为输入中的列表。下面的块是代码在典型 python 脚本中的正常外观:
这是单行代码:
“前置任务”是使用双引号转义后的单行代码和 Python 命令参数 ( -c)已被使用。
现在定义完毕后,下面是 Windows、OSX 和 Linux 的
task.json
之前的状态:这是之后的状态:
下面是任务更新用户选择的演示 在更改

task.json
后选择用户 Mario 执行:This rough approach avoids the use of either a custom or external extension. The only presumption is that Python is already installed on the system. Using the python module json (assuming there are no comments in your
task.json
), a "pre-task" is made to modify the options key within the list for the inputs key intask.json
. In this example, the "main task" will use echo in the terminal to say "Hi" to a chosen user from the updated input options after the "pre-task" runs. A directory has a set of folders that follow a naming scheme USER_[insert_username]:With python, the "pre-task" will scan the directory that matches this schema and update the options key as a list within inputs. The block below is how the code normally looks in a typical python script:
And here is the code in a single line:
The "pre-task" is created using the single line code after double quotes have been escaped and the Python command argument (-c) has been used.
Now with that defined, here is the before state of
task.json
for Windows, OSX, and Linux:And this is the after state:
Below is a demonstration of the task Update for User Selection being executed with the user Mario selected after changes to the

task.json
: