VS-Code - How to Debug and pass Command Line Arguments via Launch Config

February 16, 2021

Introduction

In this post, I will take example for Python project. And how we can start debugging or launch a code file by passing command line arguments.

For this, you would need launch.json. If you have, just edit it as I will discuss in this post.

To create a new launch.json, click on Run -> Open Configuratio or Run -> Add Configuration

Command Line Arguments

Lets take a look at launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: <Any_Name>",
      "type": "python",
      "request": "launch",
      "program":"${file}"

      "args": [
        "-c", "/Users/xyz/config",
        "-p", "2012"
      ]
    }
  ]
}

In above configuration, I’m passing following command line parameters:

-c /Users/xyz/config -p 2012

Note: In above configuration, it will always launch current code file and tries to execute it or debug it. In this scenario, you will always open your main code file, and start debugging from there.

Or, if you don’t want to open main file again and again, see next section.

Provide Starting point code file for Debug

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Any_Name",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/app.py",

      "args": [
        "-c", "/Users/xyz/config",
        "-p", "2012"
      ]
    }
  ]
}

In above configuration, I’m telling visual studio code to execute app.py file in my workspace folder.


Similar Posts

Latest Posts