VsCode:如何从命令生成自定义任务输入选项?

发布于 2025-01-19 08:09:56 字数 824 浏览 4 评论 0原文

假设我们有以下自定义任务:

{
    "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": ""
        }
    ]
}

但是我希望可用的选项是命令的结果,
喜欢lscat 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 技术交流群。

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

发布评论

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

评论(2

鲜血染红嫁衣 2025-01-26 08:09:56

您可以使用扩展名

noreferrer” pickString命名extension.commandvariable.pickstringremember的替换。

此命令可以从文件中读取选项,您可以用REGEXP确定“任务的问题匹配器”的格式。

一个示例:

  "inputs": [
    {
      "type": "command",
      "id": "option_name",
      "command": "extension.commandvariable.pickStringRemember",
      "args": {
        "description": "select an option :",
        "options": [
          ["always 1", "5000"],
          ["always 2", "5100"]
        ],
        "default": "",
        "fileName": "${workspaceFolder}/foo-bar.txt",
        "pattern": {
          "regexp": "^\\s*(?!#)([^=]+?)\\s*=\\s*(?:(\\{.+\\})|(.+))$",
          "label": "$1",
          "json": "$2",
          "value": "$3"
        }
      }
    }
  ]

如果您没有 label - value 行,则可以简化模式

        "pattern": {
          "regexp": "^\\s*(?!#)(.+)$"
        }

如果您没有静态选项(<代码>始终1/2 在示例中)您可以从args中删除选项属性。

如果grep-ping和文件重定向可能是一个问题,则使用命令或shell脚本创建文件(将输出文件名,使用变量作为参数传递到脚本)。

您可以构建一系列任务,一个复合任务,请参阅VSC Doc。

You can use the extension Command Variable v1.34.0

Use the replacement for pickString named extension.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:

  "inputs": [
    {
      "type": "command",
      "id": "option_name",
      "command": "extension.commandvariable.pickStringRemember",
      "args": {
        "description": "select an option :",
        "options": [
          ["always 1", "5000"],
          ["always 2", "5100"]
        ],
        "default": "",
        "fileName": "${workspaceFolder}/foo-bar.txt",
        "pattern": {
          "regexp": "^\\s*(?!#)([^=]+?)\\s*=\\s*(?:(\\{.+\\})|(.+))
quot;,
          "label": "$1",
          "json": "$2",
          "value": "$3"
        }
      }
    }
  ]

If you don't have label - value lines you can simplify the pattern to

        "pattern": {
          "regexp": "^\\s*(?!#)(.+)
quot;
        }

If you don't have static options (always 1/2 in example) you can remove the options property from args.

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.

菩提树下叶撕阳。 2025-01-26 08:09:56

这种粗略方法避免使用自定义或外部扩展。唯一的假设是系统上已经安装了 Python。使用 python 模块 json假设您的task.json),执行“预任务”以修改 inputs 键列表中的 options 键在task.json中。在此示例中,“主任务”将在“预任务”运行后使用终端中的 echo 向更新的输入选项中选定的用户说“嗨”。一个目录有一组遵循命名方案 USER_[insert_username] 的文件夹:

.vscode/
├─ task.json
USER_CindycinErean
USER_Link/
USER_Mario/
USER_Ness/
USER_ShihTzu/

使用 python,“前置任务”将扫描与此架构匹配的目录并更新选项 键作为输入中的列表。下面的块是代码在典型 python 脚本中的正常外观:

import json, os
base_folder = os.getcwd()
list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)]
path = base_folder + r'\\.vscode\\tasks.json'
path = r'{}'.format(path)
j = open(path, 'r');ex = json.load(j)
j.close()
j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users
ex['inputs'][0]['default'] = list_of_users[0]
json.dump(ex,j, indent=4);j.close()

这是单行代码:

import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()

“前置任务”是使用双引号转义后的单行代码和 Python 命令参数 ( -c)已被使用。

"command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""

现在定义完毕后,下面是 Windows、OSX 和 Linux 的 task.json 之前的状态:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Say Hi to selected user",
            "type": "shell",
            "dependsOn":["Update for User Selection"],
            "command": "echo Hi ${input:userSelect} !!!",
            "problemMatcher": []
        },
        {
            "label": "Update for User Selection",
            "type": "shell",
            "linux": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "osx": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "windows": {
                "options": {
                    "shell": {
                        "executable": "C:\\Windows\\system32\\cmd.exe",
                        "args": [
                            "/d",
                            "/c"
                        ]
                    },
                    "cwd": "${workspaceFolder}"
                },
                "command": "python -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "type": "pickString",
            "id": "userSelect",
            "description": "Choose a user from the directory to say Hi to!",
            "options": [
                "UserA",
                "UserB",
                "UserC",
                "UserD"
            ],
            "default": "UserA"
        }
    ]
}

这是之后的状态:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Say Hi to selected user",
            "type": "shell",
            "dependsOn": [
                "Update for User Selection"
            ],
            "command": "echo Hi ${input:userSelect} !!!",
            "problemMatcher": []
        },
        {
            "label": "Update for User Selection",
            "type": "shell",
            "linux": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "osx": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "windows": {
                "options": {
                    "shell": {
                        "executable": "C:\\Windows\\system32\\cmd.exe",
                        "args": [
                            "/d",
                            "/c"
                        ]
                    },
                    "cwd": "${workspaceFolder}"
                },
                "command": "python -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "type": "pickString",
            "id": "userSelect",
            "description": "Choose a user from the directory to say Hi to!",
            "options": [
                "CindycinErean",
                "Link",
                "Mario",
                "Ness",
                "ShihTzu"
            ],
            "default": "CindycinErean"
        }
    ]
}

下面是任务更新用户选择的演示 在更改 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 in task.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]:

.vscode/
├─ task.json
USER_CindycinErean
USER_Link/
USER_Mario/
USER_Ness/
USER_ShihTzu/

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:

import json, os
base_folder = os.getcwd()
list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)]
path = base_folder + r'\\.vscode\\tasks.json'
path = r'{}'.format(path)
j = open(path, 'r');ex = json.load(j)
j.close()
j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users
ex['inputs'][0]['default'] = list_of_users[0]
json.dump(ex,j, indent=4);j.close()

And here is the code in a single line:

import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()

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.

"command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""

Now with that defined, here is the before state of task.json for Windows, OSX, and Linux:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Say Hi to selected user",
            "type": "shell",
            "dependsOn":["Update for User Selection"],
            "command": "echo Hi ${input:userSelect} !!!",
            "problemMatcher": []
        },
        {
            "label": "Update for User Selection",
            "type": "shell",
            "linux": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "osx": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "windows": {
                "options": {
                    "shell": {
                        "executable": "C:\\Windows\\system32\\cmd.exe",
                        "args": [
                            "/d",
                            "/c"
                        ]
                    },
                    "cwd": "${workspaceFolder}"
                },
                "command": "python -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "type": "pickString",
            "id": "userSelect",
            "description": "Choose a user from the directory to say Hi to!",
            "options": [
                "UserA",
                "UserB",
                "UserC",
                "UserD"
            ],
            "default": "UserA"
        }
    ]
}

And this is the after state:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Say Hi to selected user",
            "type": "shell",
            "dependsOn": [
                "Update for User Selection"
            ],
            "command": "echo Hi ${input:userSelect} !!!",
            "problemMatcher": []
        },
        {
            "label": "Update for User Selection",
            "type": "shell",
            "linux": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "osx": {
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "command": "python3 -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "windows": {
                "options": {
                    "shell": {
                        "executable": "C:\\Windows\\system32\\cmd.exe",
                        "args": [
                            "/d",
                            "/c"
                        ]
                    },
                    "cwd": "${workspaceFolder}"
                },
                "command": "python -c \"import json, os;base_folder = os.getcwd();list_of_users = [f.name.split('_')[1] for f in os.scandir(base_folder) if (f.is_dir() and 'USER' in f.name)];path = base_folder + r'\\.vscode\\tasks.json';path = r'{}'.format(path);j = open(path, 'r');ex = json.load(j);j.close();j = open(path, 'w');ex['inputs'][0]['options'] = list_of_users;ex['inputs'][0]['default'] = list_of_users[0];json.dump(ex,j, indent=4);j.close()\""
            },
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "type": "pickString",
            "id": "userSelect",
            "description": "Choose a user from the directory to say Hi to!",
            "options": [
                "CindycinErean",
                "Link",
                "Mario",
                "Ness",
                "ShihTzu"
            ],
            "default": "CindycinErean"
        }
    ]
}

Below is a demonstration of the task Update for User Selection being executed with the user Mario selected after changes to the task.json:
enter image description here

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