how to debug golang cobra cli app in vscode

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

how to debug golang cobra cli app in vscode

问题

我有一个使用golang cobra cli的应用程序。我已经配置了我的vscode进行调试。我想使用vscode调试我的应用程序中的特定命令。

我正在使用以下的launch.json配置:

        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${fileDirname}"
        }

如果我选择main.go并开始调试,它只会打印出我的命令的帮助信息。我如何在vscode中调试特定的cli子命令?比如说abc create或abc version。

如果我选择一个子命令package并进行调试,它会显示:启动失败:无法启动进程:不是可执行文件。

英文:

I have a golang cobra cli app. have configured my vscode for debugging. I want to debug a specific command using vscode for my application.

I am using this launch.json

        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${fileDirname}"
        }

If I select main.go and start debugging it just prints the help for my command. How can I debug a particular cli subcommand in vscode? Like say abc create or abc version

If I select a subcommand package and then debug it says : Failed to launch :could not launch process:not an executalbe file

答案1

得分: 4

你可以配置VSCode的启动配置来传递参数,示例如下:

{
    "name": "Launch Package",
    "type": "go",
    "request": "launch",
    "mode": "debug",
    "program": "${fileDirname}",
    "args": ["arg1", ...]
}

请注意,你可能更喜欢在program字段中写入你的主要Go文件的路径,这样无论你当前在哪个文件上,都可以运行/调试。

参考链接:

英文:

You can configure the VSCode launch configuration to pass arguments using:

        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${fileDirname}"
            "args": ["arg1", ...]
        }

Note that you may prefer to write the path to your main go file in the program field so that you can run/debug no matter what file you are currently on.

Reference:

huangapple
  • 本文由 发表于 2021年10月21日 17:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/69659223.html
匿名

发表评论

匿名网友

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

确定