使用命令

时间:2017-08-29 16:36:29

标签: node.js debugging command visual-studio-code

我尝试使用VS Code运行简单的CRUD节点模块。

该模块的简化版本如下所示:

const getAll = () => {
  // return all elements
}

const saveElement = element => {
  // takes an object and goes through it and saves it
}

const removeElement = id => {
  // deletes the element with the passed id or returns false
}

const readElement = id => {
  // returns the element from the data
}

我使用yargs获取应用程序的参数,但我也使用命令调用每个方法,比如

node app.js remove --id="123456789"

VS Code中的launch.json如下所示:

{
  "version": "0.2.0",
  "configurations": [       
    {
      "type": "node",
      "request": "launch",
      "name": "Test Remove",
      "program": "${workspaceRoot}/app.js",
      "args": [
        "--id='123456789'"
      ]
    }
  ]
}

我无法做的是将特定的removeaddlistread命令添加到调试器中以检查这些方法,因为没有这些应用程序只运行参数,它返回一个我添加的日志,表明传递的命令无法识别。

我查看了VS Code文档,但我没有发现任何与我尝试做的事情有关的内容。

1 个答案:

答案 0 :(得分:1)

好的,明白了。实际上这很简单。只需在args数组中传递模块的特定命令即可。唯一需要注意的是,数组中的顺序必须与CLI中使用的顺序相同。所以,如果想要运行这个:

node app.js remove --id="123456789"

launch.json对象应如下所示:

{
  "version": "0.2.0",
  "configurations": [       
    {
      "type": "node",
      "request": "launch",
      "name": "Test Remove",
      "program": "${workspaceRoot}/app.js",
      "args": [
        "remove",
        "--id=123456789"
      ]
    }
  ]
}

更改args数组中的顺序将导致不必要的行为。

相关问题