`program.parse(process.argv)` 在commander.js 中有什么作用? [英] what does `program.parse(process.argv)` do in commander.js?

查看:45
本文介绍了`program.parse(process.argv)` 在commander.js 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户 commander.jsinquirer.js 提出问题并收集答案以创建一个 User 实例:

I want to user commander.js and inquirer.js to ask questions and collect the answer to create a User instance:

// index.js
const mongoose = require('mongoose');
const User = require('./model/user')
const {addUser,listAllUsers,findUserByEmail,updateUser,deleteUser} = require('./model_methods/user_methods')
const { program } = require('commander');
var inquirer = require('inquirer');

// connect to DB
const db = mongoose.connect('mongodb://localhost:27017/myImportantDates', {
    useNewUrlParser: true, 
    useUnifiedTopology: true, 
});

const questions = [
    {
      type: 'input',
      name: 'email',
      message: 'user email'
    },
    {
      type: 'input',
      name: 'name',
      message: 'user name'
    },
    {
      type: 'input',
      name: 'password',
      message: 'user password'
    },
  ];



program
   .version('0.0.1')
   .description('The important dates app');

program
   .command('add')
   .alias('a')
   .description('Add a user')
   .action(
       inquirer
         .prompt(questions)
         .then( answers => {
            addUser(answers)
          })
          .catch(err =>{
            console.log(error) 
          })
   )
program.parse(process.argv);

当我使用 node index.js add 运行它时,问题数组提出一个问题并退出:

When I run it with node index.js add, the questions array ask one question and quit:

@DESKTOP-5920U38:/mnt/c/Users/myApp$ node index.js add
? user email 
@DESKTOP-5920U38:/mnt/c/Users/myApp$ 

当我删除 program.parse(process.argv) 时,一切正常,它可以返回新的 User 实例.

When I delete program.parse(process.argv), however, everything works fine, it can return me the new User instance.

我查看文档:https://github.com/tj/commander.js/仍然不知道发生了什么.有人知道更多吗??

I check the documents: https://github.com/tj/commander.js/ Still have no idea what happened. Does anybody know more about this??

我刚刚发现,如果我在 program 实例的第一次设置中像这样将 program.parse(process.argv) 放在开头:

What I found just now is that if I put program.parse(process.argv) in the beginning like this in the first set up of the program instance:

program
   .version('0.0.1')
   .description('The important dates app')
   .parse(process.argv);

它也有效.但我仍然不知道为什么顺序很重要.

It works too. But I still don't know why the order matters.

推荐答案

在 Node.js 中,process.argv一个包含在 Node.js 进程启动时传递的命令行参数的数组.因此,program.parse(process.argv) 解析参数的命令行选项,这绕过了您的 inquierer.js 提示.你可以不用管它.

In Node.js, process.argv is an array containing the command line arguments passed when the Node.js process was launched. So, program.parse(process.argv) parses the the command line options for arguments, which is bypassing your inquierer.js prompt. You can leave it out.

这篇关于`program.parse(process.argv)` 在commander.js 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆