12

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

2
  • input-variables or pick-file Commented May 23, 2022 at 16:09
  • Thanks, @rioV8 . I gave input-variables a spin and that seems a workable solution. I can make this work without having to set the working directory if I just use the input variable with a constant directory string prefixed to it. Then I only need to enter the leaf. If you would like credit for the answer, feel free to submit an answer. Commented May 23, 2022 at 21:23

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

Comments

6

While the previous answer requires specifying each command line argument by name, there is a way to get VS Code to prompt for the entire command line. This makes the solution work with any PowerShell script rather than requiring launch.json to be tailored to each script you are debugging. In the configurations block of launch.json, add:

        {
            "type": "PowerShell",
            "request": "launch",
            "name": "PowerShell Launch Current File w/Args Prompt",
            "script": "${file}",
            "args": [
                "${command:SpecifyScriptArgs}"
            ],
            "cwd": "${file}"
        }

The secret sauce here is the ${command:SpecifyScriptArgs} prompt.

1 Comment

Thanks, Justin. Nice. This could be only valid for Powershell - and is suited to it because PowerShell does almost no command line processing in the shell and passes all the arguments to the script in $args.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.