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

4

77

Here's an example of my tasks.json:

{
  "version": "0.1.0",
  "tasks": [
    {
      "taskName": "test",
      "suppressTaskName": true,
      "command": "python",
      "args": [
        "tests/brewer_tests.py"
      ],
      "isTestCommand": true
    }
  ]
}

I can run this with shift+cmd+alt+b. I can also run it with alt+t, and choose it from the menu. Is it possible to pass additional arguments in that menu? e.g. enter image description here

And you could build it into your task like so:

{
  "version": "0.1.0",
  "tasks": [
    {
      "taskName": "test",
      "suppressTaskName": true,
      "command": "python",
      "args": [
        "tests/brewer_tests.py",
        $arg1                        # would resolve to "ARG1"
      ],
      "isTestCommand": true
    }
  ]
}

Or something similar?

Lontson answered 9/8, 2017 at 6:0 Comment(2)
Looks like no, but there's a Github issue for it, so vote on it: github.com/Microsoft/vscode/issues/1574Slumber
Possible duplicate of Visual Studio Code User Defined ArgumentSlumber
S
109

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".

Sabotage answered 19/12, 2018 at 7:38 Comment(0)
S
9

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
    }
}
Shirline answered 20/6, 2018 at 15:39 Comment(3)
I have been looking for this for a while now. This solution seems to work pretty good. You can also prompt for multiple arguments, by simply chaining them like this: $arg1 = read-host 'Arg 1:';$arg2 = read-host 'Arg 2:' Only drawback I found so far is, that vs code does not like the {"value": "", "quoting": "weak" -syntax.Sabotage
@Springrbua This should be added to the base task configuration, right? But still "value" fields inside "args" are incorrect type. It is quite unclear how to apply this answer. I have VS code 1.25.1Grower
@BenceKaulics you can realy just paste this into your tasks. Json File. Then replace the value of the 2nd "value" field with the command you want to execute. As I said, VS code gives a warning, but it seems to work.Sabotage
L
5

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.

Liaoyang answered 28/2, 2020 at 18:23 Comment(0)
P
0

Create a VsCode task to Git Clone with LF end of line characters

Open the directory (File/Open Folder) in VsCode where you want to clone a repository. Use Ctrl+P to open the user task.json file, copy the JSON below and paste it into the task.json file.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "CloneGitRepoLF",
            "type": "shell",
            "windows": {
                "options": {
                    "shell": {
                        "executable": "powershell.exe",
                        "args": [
                            "-NoProfile",
                            "-ExecutionPolicy",
                            "Bypass",
                            "-Command"
                        ]
                    }
                },
                "command": "",
                "args": [
                    { "value": "$cmdargs = read-host 'Enter the Git repo to clone';", "quoting": "weak"},
                    { "value": "wsl git clone --config core.autocrlf=false $cmdargs", "quoting": "weak"}
                ]
            },
            "presentation": {
                "panel": "dedicated",
                "focus": true
            }
        }
    ]
}

You can then run the task named CloneGitRepoLF.

Getting fancy

Install the Task Buttons extension. Search VsCode for settings.json. Add the following to the JSON:

"VsCodeTaskButtons.tasks": [
    {
        "label": "$(github) Clone Git Repo LF",
        "task": "CloneGitRepoLF",
        "tooltip": "This is task ensures Git clones the repo with LF EOL."
    }
],

You should now have a button at the very bottom of the VsCode window, named Clone Git Repo LF that you can use as a shortcut to clone a repository.

Postal answered 1/12, 2023 at 17:5 Comment(1)
what if i dont want to manully ad any input and have default input that will automatically used?Dibble

© 2022 - 2025 — McMap. All rights reserved.