title: Calling one command in Yargs calls other commands as well
body: <p>I am learning nodejs and yargs and tried to implement it in my code using command function.</p>
  <p>I am trying to make a <code>CLI</code> based note taking app.</p>
  <p>I have two files <code>app.js</code> and <code>utils.js</code>, I run <code>app.js</code> and <code>utils.js</code> is imported in <code>app.js</code> to use functions in it.</p>
  <p>There is a problem that I cannot debug, when calling <code>app.js</code> with <code>remove</code> option it is automatically also calling <code>add</code> command even when it is not being called explicitly by remove command.</p>
  <p><strong>Input:</strong></p>
  <pre class="lang-js prettyprint-override"><code>node app.js remove --title=&quot;hello&quot;
  </code></pre>
  <p><strong>Output:</strong></p>
  <pre class="lang-js prettyprint-override"><code>{ _: [ 'remove' ], title: 'hello', '$0': 'app.js' }
  Already exists!
  Operation successful!
  </code></pre>
  <p><strong>This is my <code>app.js</code>:</strong></p>
  <pre class="lang-js prettyprint-override"><code>// import modules
  const validator = require('validator');
  const yargs = require('yargs');
  // const chalk = require('chalk');
  const utils = require('./utils.js');
  
  // version
  
  yargs.version('1.0.0');
  
  const argv = yargs.argv;
  console.log(argv);
  const command = argv._[0];
  
  // commands
  yargs.command({
      command: 'add',
      describe: 'Add a new note',
      builder: {
          overwrite: {
              describe: 'Overwrite the existing file',
              demandOption: true,
              type: 'boolean'
          },
          title: {
              describe: 'Title of the note',
              demandOption: true,
              type: 'string'
          },
          body: {
              body: 'Body of the note',
              demandOption: true,
              type: 'string'
          }
      },
      handler: utils.addNote(argv.overwrite, argv.title, argv.body)
  });
  
  yargs.command({
      command: 'remove',
      describe: 'Remove a note by its title',
      builder: {
          title: {
              describe: 'Title to search for',
              demandOption: true,
              type: 'string'
          }
      },
      handler: utils.removeNote(argv.title)
  });
  
  // eof
  yargs.parse()
  </code></pre>
  <p><strong>This is my <code>utils.js</code>:</strong></p>
  <pre class="lang-js prettyprint-override"><code>// import
  const fs = require('fs');
  
  // load notes
  function loadNote() {
      try {
          const dataBuffer = fs.readFileSync('notes.json');
          const stringData = dataBuffer.toString();
          const dataJson = JSON.parse(stringData);
          return dataJson;
      } catch (e) {
          return [];
      }
  }
  
  // add note
  function addNote(overwrite, title, body) {
      const newNote = {
          &quot;title&quot;: title,
          &quot;body&quot;: body
      };
  
      const dataJson = loadNote();
      if (overwrite) {
          fs.writeFileSync('notes.json', JSON.stringify([newNote]));
          console.log(&quot;Operation successful!&quot;);
      } else {
          let flag = true;
          dataJson.forEach(function (object) {
              if (object.title === title) {
                  flag = false;
              }
          });
          if (flag) {
              dataJson.push(newNote);
              fs.writeFileSync('notes.json', JSON.stringify(dataJson));
              console.log(&quot;Operation successful!&quot;);
          } else {
              console.log(&quot;Already exists!&quot;);
          }
      }
  }
  
  // remove notes
  function removeNote(title) {
      const dataJson = loadNote();
      dataJson.filter((object) =&gt; object.title !== title);
      fs.writeFileSync('notes.json', JSON.stringify(dataJson));
      console.log(&quot;Operation successful!&quot;);
  }
  
  // export
  module.exports = {
      addNote: addNote,
      removeNote: removeNote,
  };
  </code></pre>