Arguments description
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sergei Vizel · committed by Sergei Vizel · parent d359b75 · Browse files · 2 files·+101 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+101 added−27 removed
- tests:+1 added−1 removed
- other:+100 added−26 removed
Provenance
- Commit
f226259- Tree
0a12dab- Author
Sergei Vizel <sergei.vizel@modera.net>- Committer
Sergei Vizel <sergei.vizel@modera.net>- Parent
d359b75
2 files changed+101 added−27 removed
modified:test/test.command.help.js+1 added-1 removed
5 unmodified lines678910105 unmodified linesprogram.command('mycommand [options]');
program.parse(['node', 'test']);
program.commandHelp().should.equal('\n Commands:\n\n mycommand [options]\n');program.commandHelp().should.equal(' Commands:\n\n mycommand [options]\n');modified:index.js+100 added-26 removed
855 unmodified lines856 /**857 * Set the description to `str`.858 *859 * @param {String} str860+ * @param {Object} argsDescription861 * @return {String|Command}862 * @api public863 */864 864-Command.prototype.description = function(str) {865+Command.prototype.description = function(str, argsDescription) {866 if (0 === arguments.length) return this._description;867 this._description = str;868+ this._argsDescription = argsDescription;869 return this;870 };871 872 /**55 unmodified lines928 return this;929 };930 931 /**932+ * Return prepared commands.933+ *934+ * @return {Array}935+ * @api private936+ */937+938+Command.prototype.prepareCommands = function() {939+ return this.commands.filter(function(cmd) {940+ return !cmd._noHelp;941+ }).map(function(cmd) {942+ var args = cmd._args.map(function(arg) {943+ return humanReadableArgName(arg);944+ }).join(' ');945+946+ return [947+ cmd._name948+ + (cmd._alias ? '|' + cmd._alias : '')949+ + (cmd.options.length ? ' [options]' : '')950+ + ' ' + args951+ , cmd._description952+ ];953+ });954+};955+956+/**957+ * Return the largest command length.958+ *959+ * @return {Number}960+ * @api private961+ */962+963+Command.prototype.largestCommandLength = function() {964+ var commands = this.prepareCommands();965+ return commands.reduce(function(max, command) {966+ return Math.max(max, command[0].length);967+ }, 0);968+};969+970+/**971 * Return the largest option length.972 *973 * @return {Number}974 * @api private975 */976 977 Command.prototype.largestOptionLength = function() {978+ var options = [].slice.call(this.options);979+ options.push({980+ flags: '-h, --help'981+ });937- return this.options.reduce(function(max, option) {982+ return options.reduce(function(max, option) {983 return Math.max(max, option.flags.length);984 }, 0);985 };986 987 /**988+ * Return the largest arg length.989+ *990+ * @return {Number}991+ * @api private992+ */993+994+Command.prototype.largestArgLength = function() {995+ return this._args.reduce(function(max, arg) {996+ return Math.max(max, arg.name.length);997+ }, 0);998+};999+1000+/**1001+ * Return the pad width.1002+ *1003+ * @return {Number}1004+ * @api private1005+ */1006+1007+Command.prototype.padWidth = function() {1008+ var width = this.largestOptionLength();1009+ if (this._argsDescription && this._args.length) {1010+ if (this.largestArgLength() > width) {1011+ width = this.largestArgLength();1012+ }1013+ }1014+1015+ if (this.commands && this.commands.length) {1016+ if (this.largestCommandLength() > width) {1017+ width = this.largestCommandLength();1018+ }1019+ }1020+1021+ return width;1022+};1023+1024+/**1025 * Return help for options.1026 *1027 * @return {String}1028 * @api private1029 */1030 1031 Command.prototype.optionHelp = function() {950- var width = this.largestOptionLength();1032+ var width = this.padWidth();1033 1034 // Append the help information1035 return this.options.map(function(option) {1036 return pad(option.flags, width) + ' ' + option.description11 unmodified lines1048 1049 Command.prototype.commandHelp = function() {1050 if (!this.commands.length) return '';1051 970- var commands = this.commands.filter(function(cmd) {971- return !cmd._noHelp;972- }).map(function(cmd) {973- var args = cmd._args.map(function(arg) {974- return humanReadableArgName(arg);975- }).join(' ');1052+ var commands = this.prepareCommands();1053+ var width = this.padWidth();1054 977- return [978- cmd._name979- + (cmd._alias ? '|' + cmd._alias : '')980- + (cmd.options.length ? ' [options]' : '')981- + ' ' + args982- , cmd._description983- ];984- });985-986- var width = commands.reduce(function(max, command) {987- return Math.max(max, command[0].length);988- }, 0);989-1055 return [991- ''992- , ' Commands:'1056+ ' Commands:'1057 , ''1058 , commands.map(function(cmd) {1059 var desc = cmd[1] ? ' ' + cmd[1] : '';1060 return pad(cmd[0], width) + desc;15 unmodified lines1076 desc = [1077 ' ' + this._description1078 , ''1079 ];1080+1081+ var argsDescription = this._argsDescription;1082+ if (argsDescription && this._args.length) {1083+ var width = this.padWidth();1084+ desc.push(' Arguments:');1085+ desc.push('');1086+ this._args.forEach(function(arg) {1087+ desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);1088+ });1089+ desc.push('');1090+ }1091 }1092 1093 var cmdName = this._name;1094 if (this._alias) {9 unmodified lines1104 var commandHelp = this.commandHelp();1105 if (commandHelp) cmds = [commandHelp];1106 1107 var options = [1033- ''1034- , ' Options:'1108+ ' Options:'1109 , ''1110 , '' + this.optionHelp().replace(/^/gm, ' ')1111 , ''1112 ];108 unmodified lines