Is it possible to pass arguments to a task in Visual Studio Code

前端 未结 3 1261
臣服心动
臣服心动 2020-12-25 11:39

Here\'s an example of my tasks.json:

{
  \"version\": \"0.1.0\",
  \"tasks\": [
    {
      \"taskName\": \"test\",
      \"suppressTaskName\":          


        
相关标签:
3条回答
  • 2020-12-25 11:52

    Regarding Input variables, VSCode 1.43 (Feb. 2020) adds a new feature:

    promptString Password Input

    The "promptString" "input" type can have "password": true, which will cause the quick input that shows to obscure the typed content like a password.

    0 讨论(0)
  • 2020-12-25 12:01

    Here's what is working for me for now - using this to run a golang snippet with custom arguments. If you add a keyboard mapping to this, the process is very straightforward.

    So far tested this only under Windows - linux version is commented out for that reason

    {
            "label": "runwithargs",
            "type": "shell",
            "windows": {
                "options": {
                    "shell": {
                        "executable": "powershell.exe",
                        "args": [
                            "-NoProfile",
                            "-ExecutionPolicy",
                            "Bypass",
                            "-Command"
                        ]
                    }
                },
                "command": "",
                "args": [
                    { "value": "$cmdargs = read-host 'Enter command line arguments';", "quoting": "weak"},
                    { "value": "go run ${file} $cmdargs", "quoting": "weak"}
                ]
            },
            /*"linux": {
                "command": "echo 'Enter command line arguments: '; read cmdargs;",
                "args": [ "go run ${file} $cmdargs" ]                
            },*/          
            "presentation": {
                "panel": "dedicated",
                "focus": true
            }
        }
    
    0 讨论(0)
  • 2020-12-25 12:09

    I used the solution from this answer until now, but since Visual Studio Code has now an official support for task prompts I will add it as an answer here.

    In your tasks.json file, you add the key inputs next to your tasks. This key contains an array with all possible parameters. Note that not every task has to use all of these inputs.
    All of these inputs have an id, which you will use to reference the input in your task.
    Now, in the task you only need to add ${input:myInputId} whereever you need the parameter.

    Example:

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

    The task Echo param will open a prompt, which lets you input a string value and it will then print this value. The task Echo without param will simply print "Hello".

    0 讨论(0)
提交回复
热议问题