Specifying arguments in launch.json for Python

Viewed 51422

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?

6 Answers

Mind that providing arguments in launch.json works like described until you need key value 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.

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.

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

Try this:

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

It works with my PowerShell named arguments.

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.

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

Related