Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 365
Luke Sweeney Avatar asked Aug 09 '17 06:08

Luke Sweeney


People also ask

How do you pass arguments in Visual Studio?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

How do I run VS Code tasks?

Tip: You can run your task through Quick Open (Ctrl+P) by typing 'task', Space and the command name. In this case, 'task lint'.


2 Answers

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

like image 177
Robert P Avatar answered Sep 18 '22 03:09

Robert P


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         }     } 
like image 40
bushed Avatar answered Sep 19 '22 03:09

bushed