Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug a CLI with VSCode?

I have a simple CLI written with Node.js. I want to debug that CLI with VSCode's built in debugging system but, I don't know how to attach the necessary commands to the debugger, whether it be parameters in the package.json or the launch.json, or both.

Let's say the CLI functions has the following command syntax:

> my_cool_cli <command>

In application, I'd do this:

> my_cool_cli start

And it would print:

Hello world!

Assume that the CLI is built using the commander library. It has been linked with npm, installed, it's globally accessible, and I can run it with no issues (other than all the unseen bugs) from the standard terminal.

I find that when I enter my_cool_cli start, it does not return 'Hello world!' as it should, because there is a bug. How can I debug this CLI with VSCode?

like image 823
glotchimo Avatar asked Jun 17 '18 18:06

glotchimo


People also ask

How do I Debug a command-line?

To activate the debugger at the command prompt In the Session List window, do one of the following: Choose Debug Next. The debugger is now active and is waiting to attach to a session. Select a session, and then choose Debug.

How do I Debug command-line arguments in Visual Studio?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.


1 Answers

In order to debug with the console commands, the commands have to be passed as arguments in launch.json within the given launch configuration.

{
    "type": "node",
    "request": "launch",
    "name": "Launch My Cool CLI",
    "program": "${workspaceFolder}//index.js",
    "args": [
        "start"
    ]
}

There is no need to provide the application name my_cool_cli in the arguments.

like image 185
glotchimo Avatar answered Oct 23 '22 15:10

glotchimo