Skip to content

Commit

Permalink
Merge pull request tj#711 from cravler/args_description
Browse files Browse the repository at this point in the history
Arguments description
  • Loading branch information
abetomo committed Feb 23, 2018
2 parents 6b026a5 + 1d52048 commit c92ca1d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 26 deletions.
122 changes: 98 additions & 24 deletions index.js
Expand Up @@ -869,13 +869,15 @@ Command.prototype.version = function(str, flags) {
* Set the description to `str`.
*
* @param {String} str
* @param {Object} argsDescription
* @return {String|Command}
* @api public
*/

Command.prototype.description = function(str) {
Command.prototype.description = function(str, argsDescription) {
if (arguments.length === 0) return this._description;
this._description = str;
this._argsDescription = argsDescription;
return this;
};

Expand Down Expand Up @@ -938,6 +940,45 @@ Command.prototype.name = function(str) {
return this;
};

/**
* Return prepared commands.
*
* @return {Array}
* @api private
*/

Command.prototype.prepareCommands = function() {
return this.commands.filter(function(cmd) {
return !cmd._noHelp;
}).map(function(cmd) {
var args = cmd._args.map(function(arg) {
return humanReadableArgName(arg);
}).join(' ');

return [
cmd._name +
(cmd._alias ? '|' + cmd._alias : '') +
(cmd.options.length ? ' [options]' : '') +
(args ? ' ' + args : ''),
cmd._description
];
});
};

/**
* Return the largest command length.
*
* @return {Number}
* @api private
*/

Command.prototype.largestCommandLength = function() {
var commands = this.prepareCommands();
return commands.reduce(function(max, command) {
return Math.max(max, command[0].length);
}, 0);
};

/**
* Return the largest option length.
*
Expand All @@ -946,11 +987,52 @@ Command.prototype.name = function(str) {
*/

Command.prototype.largestOptionLength = function() {
return this.options.reduce(function(max, option) {
var options = [].slice.call(this.options);
options.push({
flags: '-h, --help'
});
return options.reduce(function(max, option) {
return Math.max(max, option.flags.length);
}, 0);
};

/**
* Return the largest arg length.
*
* @return {Number}
* @api private
*/

Command.prototype.largestArgLength = function() {
return this._args.reduce(function(max, arg) {
return Math.max(max, arg.name.length);
}, 0);
};

/**
* Return the pad width.
*
* @return {Number}
* @api private
*/

Command.prototype.padWidth = function() {
var width = this.largestOptionLength();
if (this._argsDescription && this._args.length) {
if (this.largestArgLength() > width) {
width = this.largestArgLength();
}
}

if (this.commands && this.commands.length) {
if (this.largestCommandLength() > width) {
width = this.largestCommandLength();
}
}

return width;
};

/**
* Return help for options.
*
Expand All @@ -959,7 +1041,7 @@ Command.prototype.largestOptionLength = function() {
*/

Command.prototype.optionHelp = function() {
var width = this.largestOptionLength();
var width = this.padWidth();

// Append the help information
return this.options.map(function(option) {
Expand All @@ -979,28 +1061,10 @@ Command.prototype.optionHelp = function() {
Command.prototype.commandHelp = function() {
if (!this.commands.length) return '';

var commands = this.commands.filter(function(cmd) {
return !cmd._noHelp;
}).map(function(cmd) {
var args = cmd._args.map(function(arg) {
return humanReadableArgName(arg);
}).join(' ');

return [
cmd._name +
(cmd._alias ? '|' + cmd._alias : '') +
(cmd.options.length ? ' [options]' : '') +
(args ? ' ' + args : ''),
cmd._description
];
});

var width = commands.reduce(function(max, command) {
return Math.max(max, command[0].length);
}, 0);
var commands = this.prepareCommands();
var width = this.padWidth();

return [
'',
' Commands:',
'',
commands.map(function(cmd) {
Expand All @@ -1025,6 +1089,17 @@ Command.prototype.helpInformation = function() {
' ' + this._description,
''
];

var argsDescription = this._argsDescription;
if (argsDescription && this._args.length) {
var width = this.padWidth();
desc.push(' Arguments:');
desc.push('');
this._args.forEach(function(arg) {
desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
});
desc.push('');
}
}

var cmdName = this._name;
Expand All @@ -1042,7 +1117,6 @@ Command.prototype.helpInformation = function() {
if (commandHelp) cmds = [commandHelp];

var options = [
'',
' Options:',
'',
'' + this.optionHelp().replace(/^/gm, ' '),
Expand Down
4 changes: 2 additions & 2 deletions test/test.command.help.js
Expand Up @@ -5,8 +5,8 @@ var program = require('../')

program.command('bare');

program.commandHelp().should.equal('\n Commands:\n\n bare\n');
program.commandHelp().should.equal(' Commands:\n\n bare\n');

program.command('mycommand [options]');

program.commandHelp().should.equal('\n Commands:\n\n bare\n mycommand [options]\n');
program.commandHelp().should.equal(' Commands:\n\n bare\n mycommand [options]\n');

0 comments on commit c92ca1d

Please sign in to comment.