Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

python - launch.json - different arguments for different programs by default?

I am using Visual Studio Code to edit and test python. I typically open the entire folder in Code and I have multiple python files in the folder. When I have a particular Python file open in the editor and I hit F5 to start debugging, I would like default arguments passed to the program I am debugging vary depending on which file I am debugging. E.g. one set of arguments for p1.py and a different set of arguments for p2.py, but without having to switch debugging configurations. Is that possible? It seems related to How to set multiple launch.json or different arguments in same project folder at Visual Studio Code? but not quite what I am looking for. I also looked at https://code.visualstudio.com/docs/editor/debugging#_launch-configurations and if I am reading it right, I am out of luck. (I could create two configurations, one for p1.py and another for p2.py, but I would have to manually switch between those configurations when I switch between those files in the editor.)

question from:https://stackoverflow.com/questions/65923979/launch-json-different-arguments-for-different-programs-by-default

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With extension Command Variable you can select arguments based on the file path.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "args" : ["${input:arg1}","${input:arg2}"],
      "console": "integratedTerminal"
    }
  ],
  "inputs": [
    {
      "id": "arg1",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "/p1.py": "foobar",
        "/p2.py": "blabla"
      }
    },
    {
      "id": "arg1",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "/p1.py": "tralala",
        "/p2.py": ""
      }
    }
  ]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...