52

I want to specify arguments in my launch.json file for debugging. I know I can do the following:

"args": ["--arg1", "value", "--arg2"]

However, I have a very long list of arguments that is formatted as a space-delimited string. Something like this: "--arg1 value --arg2". I tried specifying:

"args": ["--arg1 value --arg2"]

But that didn't work. Right now my workflow is to take the string of arguments, run it through a Python script that changes the string into a list and copy paste it in my launch.json file. Is there a better way to do this?

1
  • Why can't you feed the arguments to CS Code in the proper form? How much work would that be? Commented Sep 22, 2017 at 0:23

6 Answers 6

76

Mind that providing arguments in launch.json works like described until you need key/value pair args.

For example, the command

$ python main.py --verbose --name Test

has to be coded inside the launch.json arguments line like this:

"args": ["--verbose", "--name=Test"],

Find a nearly hidden hint in the "Watson" example in Python debug configurations in Visual Studio Code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! For everyone wondering, if you need to add paths you might have to change "\" to "/" otherwise VSCode might not be able to read it properly.
23

AAargh.. I just wasted 30 minutes of my life trying to figure out how to clearly define arguments with values. Sometimes things worked and sometimes total fail. I was just looking at the last error message: // Use IntelliSense to learn about possible attributes.

Turns out I had both my python program and the file launch.json active in the VScode open editor. I was making changes to launch.json, but FAILING to click on my python file before starting up the debugger.

Doh! Its not a surprise that a python interpreter fails when trying to run a .json file. Need to carefully read the complete error message. (The error message should say... hey you big dummy.. you should be using a .py file when executing python!)

Info shared here in case anybody else makes the same dumb mistake.

3 Comments

This post was actually very helpful for me. You need to focus the file you would like to debug, then hit F5. Thanks!
Peter Mortensen, Really? Your edit, two years after posting this answer was just inappropriate, and just not helpful. Thanks anyway.
Technically, this doesn't answer the original question at all. However, it was exactly the piece of information I actually needed. Thank you for sharing!
14

Unfortunately, there is no way to do what you want. Arguments can only be passed as an array, but not as a string with spaces. The argument with spaces is wrapped in quotation marks and passed as one whole argument.

Quote from the documentation:

args - arguments passed to the program to debug. This attribute is of type array and expects individual arguments as array elements.

Answer from one of the developers on github:

The rule to translate a command line to the "args" is simple: every command line argument separated by whitespace needs to become a separate item of the "args" attribute.

Examples:

Command:
tar -cvf test-14-09-12.tar /home/test/

Args:
"args": ["-cvf", "test-14-09-12.tar", "/home/test/"]
Command:
tar -c --file new.tar --include='*foo*' old.tgz

Args:
"args": ["-c", "--file", "new.tar", "--include='*foo*'", "old.tgz"]

Update:

I found solution, using snippets.

You can add snippet to global snippets or project snippets, as described here:

"args_transform": {
    "scope": "jsonc",
    "prefix": "args_transform",
    "body": "${1/((\"(.*?)\")|('(.*?)')|([^ ]+))( )?/\"$3$6$5\"${7:+, }/g}"
}

Write args_transform where you need to write args in launch.json (or any other json with comments file), press tab, insert your args and then press tab again

But it, probably, not works correctly with some kind of arguments, please write errors in comments if snippet fix needed

2 Comments

it'd be so nice to list some examples since this is the accepted answer :) (I understand these are listed in a separate answer below)
Thanks, it;s working with this command in Windows. "args": ["-cvf", "test-14-09-12.tar", "\\home\\test"]
2

Try this:

"args": [   "-arg1 value1",
            "-argname2 value2"],

It works with my PowerShell named arguments.

Comments

1

It isn't much, but it is honest work:

import json

input_args = "--arg1 value --arg2"

vscode_args = list(input_args.split(" "))
print(json.dumps(vscode_args))

This prints:

["--arg1", "value", "--arg2"]

which you can paste in your launch.json file.

I made this because I have a lot of arguments to convert. This comes in handy.

Comments

1

this worked for me:

I defined a variable ('workspace_path') for the debugger I'm using (launcher).

Settings -> Extensions -> Robot Framework -> Robot Variables Robot: Variables

Edit file 'settings.json' -> settings.json and add the variable (example: 'workspace_path')

Run -> Open Configurations -> Edit file 'launch.json' launch.json and add the argument workspace_path (without cuotes).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.