Skip to content

Commit

Permalink
[feat] add help command
Browse files Browse the repository at this point in the history
  • Loading branch information
icfr committed Sep 26, 2016
1 parent 3fd085e commit 1585abf
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions lib/shell/lib/plugins/help.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var pad;

pad = require('pad');
var pad = require('pad');
var each = require('lodash/each');
var console;

/*
Expand Down Expand Up @@ -29,24 +30,81 @@ Usage

module.exports = function help(settings) {
var shell, text;
shell = settings.shell;
console = shell.logger(shell.config('core.log.prefix') + ':shell:help');
console.log('start helper');

var introduction = 'Type "help" or press enter for a list of commands';
var padding = 35;
shell = settings.shell;
var argPadding = 20;

if (!settings.introduction) {
settings.introduction = introduction;
}
shell.help = function shellHelp(req, res) {
var route, routes, routeText, _i, _len;
var routes = shell.routes;
var command = req.params.command;
if (command) {

var route = routes[shell.routeName[command]];
if (!route) {
res.red('command ' + command + ' not found');
return res.prompt();
}
res.yellow('Help:').ln();
res.print(pad('', 2)).white(route.description).ln().ln();
res.yellow('Usage:').ln();
res.print(pad('', 2)).white(route.name);
if (route.keys.options.length > 0) {
res.white('[options] [--] ');
}
var args = '';
each(route.keys.params, function eachParam(param) {
var name = param.optional ? '[<' + param.key + '>]' : '<' + param.key + '>';

args += pad('', 2) + pad('\x1b[32m' + param.key, argPadding);
if (param.help) {
args += '\x1b[37m' + param.help + ' ';
}
if (param.def) {
args += '\x1b[33m[default: "' + param.def + '"]';
}
args += '\n';
res.white(name + ' ');
});

res.ln().ln();
if (route.keys.params.length > 0) {
res.yellow('Arguments:').ln();
}
res.green(args).ln();
if (route.keys.options.length > 0) {
res.yellow('Options:').ln();
}
each(route.keys.options, function eachOption(option) {
res.print(pad('', 2)).green(pad(option.key, argPadding));
if (option.help) {
res.white(option.help);
}
res.ln();
});

return res.prompt();
}
var routeText;
res.yellow('Available commands:');
res.ln();
routes = shell.routes;
for (_i = 0, _len = routes.length; _i < _len; _i++) {
route = routes[_i];
routeText = pad(route.command.replace(/(\{.*\})/, ''), padding);
if (route.description) {
res.print(pad('', 2)).green(routeText).white(route.description).ln();

each(routes, function eachRoute(rte) {
routeText = pad(rte.name, padding);
if (rte.description) {
res.print(pad('', 2)).green(routeText).white(rte.description).ln();
}
}
});
return res.prompt();
};
shell.cmd('help', 'Show this message', shell.help.bind(shell));
console.log('add routes');
shell.cmd('help {command(([\\w\\:])*)?}', 'Show this message', shell.help.bind(shell));
shell.cmd('', shell.help.bind(shell));
if (shell.isShell && settings.introduction) {
text = typeof settings.introduction === 'string' ? settings.introduction : introduction;
Expand Down

0 comments on commit 1585abf

Please sign in to comment.