yargs: exception thrown when yargs.parsed.newAliases is undefined

Found something of an issue when using yargs.showHelp() L761

it seems that when self._parseArgs(processArgs) is called, it is not saving the results to self.parsed

if you put a breakpoint on this line: L1012, you will see that the self.parsed property contains the appropriate items.

however, after this function is done, on L762, self.parsed is set back to false instead of an object. this leads to their being a parse error:

TypeError: Cannot convert undefined or null to object .concat(Object.keys(yargs.parsed.newAliases) || [])

under function: usage.showHelp( L240

Thanks! 👍

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 17 (16 by maintainers)

Most upvoted comments

I cannot replicate it in a single file, but with commands it is easy:

// ./bin.js
require('yargs')
  .commandDir('commands')
  .parse();

// ./commands/foo.js
module.exports = {
  command: 'foo',
  desc: '',
  builder: () => {},
  handler: async () => {
    throw new Error('test');
  }
};

(node:23038) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Object.help (/Users/gajus/Documents/dev/temp3/node_modules/yargs/lib/usage.js:240:22)
    at Object.self.showHelp (/Users/gajus/Documents/dev/temp3/node_modules/yargs/lib/usage.js:432:15)
    at Object.Yargs.self.showHelp (/Users/gajus/Documents/dev/temp3/node_modules/yargs/yargs.js:790:11)
    at Object.fail (/Users/gajus/Documents/dev/temp3/node_modules/yargs/lib/usage.js:50:17)
    at /Users/gajus/Documents/dev/temp3/node_modules/yargs/lib/command.js:239:36
(node:23038) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:23038) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.