yargs 无法正确读取输入

问题描述 投票:0回答:2

在我的 apps.js 文件中,我有以下代码:

const yargs=require('yargs');
const argv=yargs.argv;
console.log('yargs',argv);

当我正常运行命令时,我得到:

> node app.js read --title="to by"
yargs { _: [ 'read' ],
  help: false,
  version: false,
  title: 'to by',
  '$0': 'app.js' }

但是,当我使用nodemon时,如果标题中有空格,我会得到错误的参数:

> nodemon app.js read --title="to by"
yargs { _: [ 'read', '"--title=to', 'by"' ],
  help: false,
  version: false,
  '$0': 'app.js' }

所以应用程序无法正常运行。这里发生了什么?谢谢

javascript nodemon yargs
2个回答
0
投票

我可能会错得很厉害,但在像这样在终端中传递参数时尝试不使用 equals = 。

    nodemon app.js read --title "to by"

0
投票

出于某种原因,如果我在使用 nodemon 时在初始化命令下方的页面中的某处添加 yargs.argv ,控制台只会记录命令结果

const yargs = require("yargs");

yargs.command({
  command: "my_command",
  describe: "Some Thing is Happening!",
  handler: () => console.log("Command is Running."),
});

yargs.argv; // not storing value just mentioning.

在脚本中使用nodemon

"scripts": {
    "start": "nodemon index.js"
  },

您可以使用

npm start my_command

© www.soinside.com 2019 - 2024. All rights reserved.