joyent/node-triton#5 show usage info on usage errors

This commit is contained in:
Trent Mick 2015-09-02 01:04:20 -07:00
parent 58a7c9977b
commit 4064b93c8e
6 changed files with 50 additions and 8 deletions

View File

@ -5,13 +5,16 @@
var p = console.log;
var cmdln = require('cmdln');
var CLI = require('../lib/cli');
var main = require('../lib/cli').main;
if (require.main === module) {
main();
/*
var cli = new CLI();
cmdln.main(cli, {
argv: process.argv,
showCode: true,
showNoCommandErr: false
});
*/
}

View File

@ -271,11 +271,50 @@ CLI.prototype.do_badger = require('./do_badger');
//---- mainline
if (require.main === module) {
function main(argv) {
if (!argv) {
argv = process.argv;
}
var cli = new CLI();
cmdln.main(cli, {showNoCommandErr: false});
cli.main(argv, function (err, subcmd) {
var exitStatus = (err ? err.exitStatus || 1 : 0);
var showErr = (cli.showErr !== undefined ? cli.showErr : true);
if (err && showErr) {
var code = (err.body ? err.body.code : err.code);
if (code === 'NoCommand') {
/* jsl:pass */
} else if (err.message !== undefined) {
console.error('%s%s: error%s: %s',
cli.name,
(subcmd ? ' ' + subcmd : ''),
(code ? format(' (%s)', code) : ''),
(cli.showErrStack ? err.stack : err.message));
// If this is a usage error, attempt to show some usage info.
if (['Usage', 'Option'].indexOf(code) !== -1 && subcmd) {
var help = cli.helpFromSubcmd(subcmd);
if (help) {
// Would like a shorter synopsis. Attempt to
// parse it down, somewhat generally.
var usageIdx = help.indexOf('\nUsage:');
if (usageIdx !== -1) {
help = help.slice(usageIdx);
}
console.error(help);
}
}
}
}
process.exit(exitStatus);
});
}
//---- exports
module.exports = CLI;
module.exports = {
CLI: CLI,
main: main
};

View File

@ -20,7 +20,7 @@ function do_create_instance(subcmd, opts, args, callback) {
return;
} else if (args.length < 1 || args.length > 2) {
return callback(new errors.UsageError(format(
'incorrect number of args (%d): %s', args.length, args.join(' '))));
'incorrect number of args (%d)', args.length)));
}
var log = this.triton.log;

View File

@ -15,7 +15,7 @@ function do_image(subcmd, opts, args, callback) {
return;
} else if (args.length !== 1) {
return callback(new errors.UsageError(format(
'incorrect number of args (%d): %s', args.length, args.join(' '))));
'incorrect number of args (%d)', args.length)));
}
this.triton.getImage(args[0], function onRes(err, img) {

View File

@ -16,7 +16,7 @@ function do_network(subcmd, opts, args, cb) {
return;
} else if (args.length !== 1) {
return cb(new errors.UsageError(format(
'incorrect number of args (%d): %s', args.length, args.join(' '))));
'incorrect number of args (%d)', args.length)));
}
this.triton.getNetwork(args[0], function (err, net) {

View File

@ -15,7 +15,7 @@ function do_package(subcmd, opts, args, callback) {
return;
} else if (args.length !== 1) {
return callback(new errors.UsageError(format(
'incorrect number of args (%d): %s', args.length, args.join(' '))));
'incorrect number of args (%d)', args.length)));
}
this.triton.getPackage(args[0], function onRes(err, pkg) {