print help for top-level commands and command sets

This commit is contained in:
AJ ONeal 2017-11-09 16:54:47 -07:00
parent 523f57944f
commit 83030fb416
1 changed files with 58 additions and 24 deletions

View File

@ -20,12 +20,12 @@ function parseArgs(argv, opts) {
var COMMAND = 'COMMAND';
var maxCmdLen = COMMAND.length;
var maxPairLen = 0;
var cmds;
var tlcs; // top-level commands
var arg1 = args[0];
// build commands list
var pairsMap = {};
cmds = opts.commands.filter(function (desc) {
tlcs = opts.commands.filter(function (desc) {
var pair = desc[0].split(/\s+/)[0];
var psub = pair.split(sep)[0];
pairsMap[pair] = true;
@ -36,13 +36,6 @@ function parseArgs(argv, opts) {
}
});
if (-1 === Object.keys(pairsMap).indexOf(cmd)) {
console.log('fail', cmd);
arg1 = cmd;
cmd = 'help';
help();
}
function rpad(str, len) {
while (str.length < len) {
str += ' ';
@ -53,42 +46,83 @@ function parseArgs(argv, opts) {
function help() {
var status = 0;
function helpMain() {
console.log('');
console.log('Here are all the top-level commands:');
console.log('');
function printCmd(desc) {
var pcmd = rpad(desc[0].split(/\s+/)[0], maxCmdLen);
var pdesc = desc[1];
console.info('\t' + defaults.main + ' ' + pcmd, ' # ' + pdesc);
}
console.log('\t' + defaults.main + ' ' + rpad(COMMAND, maxCmdLen), ' # description');
console.log('\t' + '------------------------------');
cmds.forEach(function (desc) {
var pcmd = rpad(desc[0].split(/\s+/)[0], maxCmdLen);
var pdesc = desc[1];
console.log('\t' + defaults.main + ' ' + pcmd, ' # ' + pdesc);
});
console.log('');
function printCmds(cmds) {
console.info('');
console.info('\t' + defaults.main + ' ' + rpad(COMMAND, maxCmdLen), ' # description');
console.info('\t' + '------------------------------');
cmds.forEach(printCmd);
console.info('');
}
function helpMain() {
console.info('');
console.info('Here are all the top-level commands:');
printCmds(tlcs);
}
if (arg1 && -1 === Object.keys(pairsMap).indexOf(arg1)) {
status = 1;
console.log('');
console.log(defaults.main + ": Unknown command '" + arg1 + "'");
console.info('');
console.info(defaults.main + ": Unknown command '" + arg1 + "'");
console.info('');
console.info("Try '" + defaults.main + " help'");
console.info('');
arg1 = null;
return;
}
// the case of "oauth3 help --something"
if (!arg1 || '-' === arg1[0]) {
helpMain();
process.exit(status);
return;
}
// the case of "oauth3 help help"
if ('help' === arg1) {
helpMain();
console.log("no more help available for 'help'");
console.info("no more help available for 'help'");
process.exit(status);
return;
}
console.info('');
console.info('Here are all the "' + command + '"-related commands:');
printCmds(
opts.commands.filter(function (desc) {
var pair = desc[0].split(/\s+/)[0];
var psub = pair.split(sep)[0];
maxPairLen = Math.max(maxPairLen, pair.length);
if (arg1 === psub || arg1 === pair) {
maxCmdLen = Math.max(maxCmdLen, pair.length);
return true;
}
})
);
console.info('');
}
if (-1 === Object.keys(pairsMap).indexOf(cmd)) {
arg1 = cmd;
cmd = 'help';
help();
return;
}
if (-1 !== [ 'help', '-h', '--help' ].indexOf(command) || -1 !== args.indexOf('-h') || -1 !== args.indexOf('--help')) {
help();
return;
}
console.log('RUN', cmd, sub || '(n/a)', arg1 || '(n/a)');
}
parseArgs(process.argv, {