Launch a program in vscode only if a certain condition is satisfied, e.g. the filename matches. (using launch.json)

huangapple go评论86阅读模式
英文:

Launch a program in vscode only if a certain condition is satisfied, e.g. the filename matches. (using launch.json)

问题

我正在尝试编写一个launch.json配置,用于运行名为myProg的Python程序。按设计,myProg需要从包含名为myInput.py的文件的文件夹中运行;我的工作流程是打开myInput.py,在那里设置一些断点,然后通过以下启动配置启动myProg

{
  "name": "my launcher",
  "type": "python",
  "request": "launch",
  "program": "/home/user/.local/bin/myProg",
  "args": [
      "install",
      "--someOption=myOption",
      "--someOtherOption=myOtherOption"
  ],
  "cwd": "${fileDirname}",
  "console": "integratedTerminal",
  "justMyCode": false
}

只要我在单击“开始调试”时将myInput.py(或包含myInput.py的文件夹中的任何其他文件)打开并置于编辑器的焦点中,此配置就能正常工作,因为我已将"cwd"设置为"${fileDirname}",即当前文件所在的文件夹。

这是我想要实现的目标:当单击“开始调试”时焦点在除myInput.py之外的任何文件上时,我希望会出现错误。

这是我想要更改的行为部分:

1- 目前,如果选择来自不同文件夹的文件,代码会运行,直到需要加载myInput.py部分,然后引发异常并失败。在这种情况下,我想要的是“根本不运行”,而不是给我一个错误、消息或类似的内容。

2- 目前,如果选择来自myInput.py所在文件夹的文件,代码会正常运行。我更希望它“不运行”,而是给我一个错误/消息,指出未选择正确的文件。

是否有实现我想要的方式(项目1更为重要)?简而言之,我希望根据条件来决定是否运行程序。

请假设我不能更改“myProg”或其工作方式。

英文:

I am trying to write a launch.json config to run a python program called myProg. By design, myProg needs to be run from a folder containing a file named myInput.py; My workflow is that I open myInput.py, put some breakpoints there, and launch myProg through the following launch configuration:

{
  "name": "my launcher",
  "type": "python",
  "request": "launch",
  "program": "/home/user/.local/bin/myProg",
  "args": [
      "install",
      "--someOption=myOption",
      "--someOtherOption=myOtherOption"
  ],
  "cwd": "${fileDirname}",
  "console": "integratedTerminal",
  "justMyCode": false
}

This config works as long as I have myInput.py (or any other file from the folder containing myInput.py) open and in focus in the editor when I click "start debugging", because I have set "cwd" to be "${fileDirname}", i.e. the folder containing the current file.

This is what I want to achieve: I want to get an error when any file other than myInput.py is in focus when I "start debugging".

These are the parts of the behavior that I want to change:

1- Currently when a file from a different folder is selected, the code runs until it reaches the part when it needs to load myInput.py, then it raises an exception and fails. What I want in this case is "not running at all", instead giving me an error, message or similar.

2- Currently when a file from the same folder as myInput.py is selected, the code runs fine. I would prefer it to "not run" and instead give me an error/message that the correct file is not selected.

Is there a way to achieve what I want (item 1 is more important)? In short, I want to have a condition based on which it is decided whether to run the program or not.

Assume that I cannot change "myProg" or how it works.

答案1

得分: 1

您可以使用扩展Command Variable和命令extension.commandvariable.file.fileAsKey

如果当前文件不包含/myInput.py,选择一个不存在的命令DoNotRun

{
  "version": "2.0.0",
  "tasks": [
    {
      "name": "my launcher",
      "type": "python",
      "request": "launch",
      "program": "${input:chooseCmd}",
      "args": [
          "install",
          "--someOption=myOption",
          "--someOtherOption=myOtherOption"
      ],
      "cwd": "${fileDirname}",
      "console": "integratedTerminal",
      "justMyCode": false
    }
  ],
  "inputs": [
    {
      "id": "chooseCmd",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "/myInput.py": "/home/user/.local/bin/myProg",
        "@default": "/home/user/.local/bin/DoNotRun"
      }
    }
  ]
}
英文:

You can use the extension Command Variable and the command extension.commandvariable.file.fileAsKey

If the current file does not contain /myInput.py choose a non existing command DoNotRun

{
  "version": "2.0.0",
  "tasks": [
    {
      "name": "my launcher",
      "type": "python",
      "request": "launch",
      "program": "${input:chooseCmd}",
      "args": [
          "install",
          "--someOption=myOption",
          "--someOtherOption=myOtherOption"
      ],
      "cwd": "${fileDirname}",
      "console": "integratedTerminal",
      "justMyCode": false
    }
  ],
  "inputs": [
    {
      "id": "chooseCmd",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "/myInput.py": "/home/user/.local/bin/myProg",
        "@default": "/home/user/.local/bin/DoNotRun"
      }
    }
  ]
}

答案2

得分: 0

这听起来像一个XY问题。只需在启动配置中将cwd设置为${workspaceFolder}/<包含myInput.py的目录路径>。甚至您的程序需要从该目录运行“出于设计目的”的事实可能也是一个XY问题。但没有足够的信息来判断。

英文:

This sounds like an XY problem. Just set cwd in the launch config to ${workspaceFolder}/&lt;path to directory containing myInput.py&gt;. Even the fact that it's "by design" that your program needs to be run from that directory might be an XY problem. But there's not enough info to tell.

答案3

得分: -1

如果我是你,我会尝试在launch.json中不同地指定cwd,这样它会从{workspaceFolder}而不是文件目录中寻找myInput.py文件。然后,它会从你在VSCode中打开的文件夹中寻找你的文件,而不是从你当前正在编辑的文件的路径中寻找。

{
  "name": "我的启动器",
  "type": "python",
  "request": "launch",
  "program": "/home/user/.local/bin/myProg",
  "args": [
      "install",
      "--someOption=myOption",
      "--someOtherOption=myOtherOption"
  ],
  "cwd": "${workspaceFolder}/path to input.py/myInput.py",
  "console": "integratedTerminal",
  "justMyCode": false
}
英文:

If i were you i would try to specify the cwd in launch.json differently, so it looks for myInput.py file from the {workspaceFolder} instead from file dirname. Then it would look for your file from the folder you have opened in vscode not from path of the file you are currently editing.

{
  &quot;name&quot;: &quot;my launcher&quot;,
  &quot;type&quot;: &quot;python&quot;,
  &quot;request&quot;: &quot;launch&quot;,
  &quot;program&quot;: &quot;/home/user/.local/bin/myProg&quot;,
  &quot;args&quot;: [
      &quot;install&quot;,
      &quot;--someOption=myOption&quot;,
      &quot;--someOtherOption=myOtherOption&quot;
  ],
  &quot;cwd&quot;: &quot;${workspaceFolder}/path to input.py/myInput.py&quot;,
  &quot;console&quot;: &quot;integratedTerminal&quot;,
  &quot;justMyCode&quot;: false
}

huangapple
  • 本文由 发表于 2023年3月7日 22:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定