Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: error when command is unknown (#2916)
Browse files Browse the repository at this point in the history
Since yargs v14, using `.parse(command)` with an unknown command
causes an [error to be thrown](yargs/yargs#1419)
instead of the help text being printed.

One solution is to use `.argv` instead of `.parse(command)` but we
use the `.parse(command)` variant to enable testing as the passed
command is evaluated rather than `process.argv`, which is what happens
when you call `.argv`.

Instead if we detect a error caused by an unknown command, use the parser
to show help instead of version of yargs that had `.parse(command)`
called on it.
  • Loading branch information
achingbrain authored Mar 13, 2020
1 parent 3207e3b commit 743a7fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/ipfs/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ module.exports = (command, ctxMiddleware) => {
.fail((msg, err, yargs) => {
// Handle yargs errors
if (msg) {
// if the error was caused by an unknown command, the use of `.parse(command)`
// below causes printing help to fail: https://github.com/yargs/yargs/issues/1419#issuecomment-527234789
// so pass the unadulterated parser in as `yargs` in order to print help successfully
if (msg.includes('Unknown argument')) {
yargs = parser
}

return reject(errCode(new Error(msg), 'ERR_YARGS', { yargs }))
}

Expand Down
9 changes: 9 additions & 0 deletions packages/ipfs/test/cli/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,13 @@ describe('daemon', () => {
}
})
})

it('should print help when command is unknown', async function () {
this.timeout(100 * 1000)

const err = await ipfs.fail('derp')

expect(err.stderr).to.include('Commands:')
expect(err.stderr).to.include('Unknown argument: derp')
})
})

0 comments on commit 743a7fc

Please sign in to comment.