在 Python 的 launch.json 中指定参数 [英] Specifying arguments in launch.json for Python

查看:65
本文介绍了在 Python 的 launch.json 中指定参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 launch.json 文件中指定参数以进行调试.我知道我可以执行以下操作:

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

但是,我有很长的参数列表,格式为空格分隔的字符串.像这样:"--arg1 value --arg2".我尝试指定:

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

但这没有用.现在我的工作流程是获取参数字符串,通过 Python 脚本运行它,该脚本将字符串更改为列表并将其复制粘贴到我的 launch.json 文件中.有没有更好的方法来做到这一点?

解决方案

不幸的是,没有办法做你想做的事.参数只能作为数组传递,而不能作为带空格的字符串传递.带空格的参数用引号括起来并作为一个完整的参数传递.

引用自文档:><块引用>

args - 传递给程序进行调试的参数.这个属性是输入数组并期望将单个参数作为数组元素.

一位开发者在 github 上的回答:><块引用>

将命令行转换为args"的规则很简单:每个由空格分隔的命令行参数都需要成为args"的一个单独项目.属性.

示例:

命令:tar -cvf test-14-09-12.tar/home/test/参数:args":[-cvf"、test-14-09-12.tar"、/home/test/"]

命令:tar -c --file new.tar --include='*foo*' old.tgz参数:args":[-c"、--file"、new.tar"、--include='*foo*'"、old.tgz"]

更新:

我找到了解决方案,使用片段.

您可以将代码段添加到全局代码段或项目代码段,如此处:

args_transform":{范围":jsonc",前缀":args_transform","正文": "${1/((\"(.*?)\")|('(.*?)')|([^ ]+))( )?/\";$3$6$5\${7:+, }/g}"}

在需要在launch.json(或任何其他带有注释文件的json)中写入args的地方写入args_transform,按Tab键,插入您的args,然后再次按Tab键

但它可能无法与某些参数一起正常工作,如果需要修复代码片段,请在评论中写下错误

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?

解决方案

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

这篇关于在 Python 的 launch.json 中指定参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆