0

When i run command node app.js --help , terminal shows only add command meanwhile i have defined three commands and all of them works. And when i use parse() method instead of argv why i get 'Removing a note' printed 2 times ?

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> 
Sanket
  • 11
  • 4

1 Answers1

0

You are calling .argv which is stopping the process before the others can be defined before help is invoked.

Remove .argv from all but the last command definition you create is the simplest fix for this.

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;
Andrew Nolan
  • 1,987
  • 2
  • 20
  • 23