From ea25fadea6b2f10f46949d11ac0e268c93fcdb1d Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Wed, 9 Dec 2015 07:04:23 -0500 Subject: [PATCH] CLI fail gracefully on unknown command --- bin/documentation.js | 7 +++++++ test/bin.js | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/bin/documentation.js b/bin/documentation.js index 78ddee0..c0b74a0 100755 --- a/bin/documentation.js +++ b/bin/documentation.js @@ -67,6 +67,13 @@ function parseArgs() { var command = argv._[0], inputs = argv._.slice(1); + if (!commands[command]) { + yargs.showHelp(); + var suggestion = [argv['$0'], 'build'].concat(process.argv.slice(2)).join(' '); + process.stderr.write('Unknown command: ' + command + '. Did you mean "' + suggestion + '"?\n'); + process.exit(1); + } + if (inputs.length == 0) { try { var p = require(path.resolve('package.json')); diff --git a/test/bin.js b/test/bin.js index c4273df..82b60c4 100644 --- a/test/bin.js +++ b/test/bin.js @@ -122,6 +122,14 @@ test('invalid arguments', function (group) { }); }, options); + group.test('bad command', function (t) { + documentation(['-f html fixture/internal.input.js'], function (err, stdout, stderr) { + t.ok(err.code, 'exits nonzero'); + t.ok(stderr.match(/Unknown command/), 'reports unknown command'); + t.end(); + }); + }); + group.end(); });