Home > front end >  VS Code debugging prompting for arguments and also setting the working directory
VS Code debugging prompting for arguments and also setting the working directory

Time:06-01

I know how to pass fixed arguments in the launch.json, e.g. In Visual Studio Code, how to pass arguments in launch.json . What I really need is a prompt where I can give a value for an argument that changes.

In addition, my argument is a (data) directory for which there is a very ugly long absolute path. I'd really like to be able to set the working directory to a path which contains each of my individual data directories so I only need to provide a relative directory path, i.e. just the directory name.

I'm working with Python, on Windows (not my choice) using VS Code 1.55.2 (not my choice, either).

CodePudding user response:

You can use input variables

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File with arguments",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "args": [
        "--dir",
        "/some/fixed/dir/${input:enterDir}"
      ]
    }
  ],
  "inputs": [
    {
      "id": "enterDir",
      "type": "promptString",
      "description": "Subdirectory to process",
      "default": "data-0034"
    }
  ]
}

You can place the ${input:enterDir} in any string in the task "configurations" like the "cwd" property.

If you like to pick a directory from a list because it is dynamic you can use the extension Command Variable that has the command pickFile

Command Variable v1.36.0 supports the fixed folder specification.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File with arguments",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "args": [
        "--dir",
        "${input:pickDir}"
      ]
    }
  ],
  "inputs": [
    {
      "id": "pickDir",
      "type": "command",
      "command": "extension.commandvariable.file.pickFile",
      "args": {
        "include": "**/*",
        "display": "fileName",
        "description": "Subdirectory to process",
        "showDirs": true,
        "fromFolder": { "fixed": "/some/fixed/dir" }
      }
    }
  ]
}

On Unix like systems you can include the folder in the include glob pattern. On Windows you have to use the fromFolder to convert the directory path to a usable glob pattern. If you have multiple folders you can use the predefined property.

  • Related