当我运行node app.js --help命令时,终端只显示add命令,同时我定义了三个命令,它们都可以工作。当我使用parse()方法而不是argv时,为什么我会打印两次“删除注释”?
const yargs = require('yargs');
yargs.command({
    command:'add',
    describe:'Adds new note',
    handler:function(){
    console.log('Adding a note')
    }
}).argv;
yargs.command({
    command:'remove',
    describe:'Removes a note',
    handler:function(){
    console.log('Removing a note')
    }
 }).argv;
yargs.command({
    command:'list',
    describe:'Fetches a list of notes',
    handler:function(){
    console.log('Fetching a list')
    }
 }).argv;
Commands:
  app.js add  Adds new note
Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
 PS C:\Users\Sanket\Documents\node js\notes-app> 
PS C:\Users\Sanket\Documents\node js\notes-app> node app.js add   
Adding a note
PS C:\Users\Sanket\Documents\node js\notes-app> node app.js remove
Removing a note
Removing a note
PS C:\Users\Sanket\Documents\node js\notes-app> node app.js list  
Fetching a list
PS C:\Users\Sanket\Documents\node js\notes-app> 发布于 2020-04-19 05:02:20
您调用的是.argv,它在调用help之前先停止进程,然后才能定义其他进程。
从您创建的最后一个命令定义中删除.argv是解决此问题的最简单的方法。
const yargs = require('yargs');
yargs.command({
    command:'add',
    describe:'Adds new note',
    handler:function(){
    console.log('Adding a note')
    }
});
yargs.command({
    command:'remove',
    describe:'Removes a note',
    handler:function(){
    console.log('Removing a note')
    }
 });
yargs.command({
    command:'list',
    describe:'Fetches a list of notes',
    handler:function(){
    console.log('Fetching a list')
    }
 }).argv;https://stackoverflow.com/questions/61295304
复制相似问题