'VsCode : How to generate custom task input options from a command?

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 ?



Solution 1:[1]

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*(?:(\\{.+\\})|(.+))$",
          "label": "$1",
          "json": "$2",
          "value": "$3"
        }
      }
    }
  ]

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

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

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rioV8