Skip to content
Myr

Merge pull request #711 from cravler/args_description

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Arguments description

authored by Tomoaki Abe · committed by GitHub · parents 6b026a5, 1d52048 · Browse files · 2 files+100 added−26 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+100 added−26 removed

  • tests:+2 added−2 removed
  • other:+98 added−24 removed

2 files+100 added−26 removed

modified:test/test.command.help.js+2 added-2 removed
modified:index.js+98 added-24 removed
867 unmodified lines
868/**869 * Set the description to `str`.870 *871 * @param {String} str872 * @param {Object} argsDescription873 * @return {String|Command}874 * @api public875 */876876Command.prototype.description = function(str) {877Command.prototype.description = function(str, argsDescription) {878 if (arguments.length === 0) return this._description;879 this._description = str;880 this._argsDescription = argsDescription;881 return this;882};883884/**
55 unmodified lines
940 return this;941};942943/**944 * Return prepared commands.945 *946 * @return {Array}947 * @api private948 */949950Command.prototype.prepareCommands = function() {951 return this.commands.filter(function(cmd) {952 return !cmd._noHelp;953 }).map(function(cmd) {954 var args = cmd._args.map(function(arg) {955 return humanReadableArgName(arg);956 }).join(' ');957958 return [959 cmd._name +960 (cmd._alias ? '|' + cmd._alias : '') +961 (cmd.options.length ? ' [options]' : '') +962 (args ? ' ' + args : ''),963 cmd._description964 ];965 });966};967968/**969 * Return the largest command length.970 *971 * @return {Number}972 * @api private973 */974975Command.prototype.largestCommandLength = function() {976 var commands = this.prepareCommands();977 return commands.reduce(function(max, command) {978 return Math.max(max, command[0].length);979 }, 0);980};981982/**983 * Return the largest option length.984 *985 * @return {Number}986 * @api private987 */988989Command.prototype.largestOptionLength = function() {990 var options = [].slice.call(this.options);991 options.push({992 flags: '-h, --help'993 });949 return this.options.reduce(function(max, option) {994 return options.reduce(function(max, option) {995 return Math.max(max, option.flags.length);996 }, 0);997};998999/**1000 * Return the largest arg length.1001 *1002 * @return {Number}1003 * @api private1004 */10051006Command.prototype.largestArgLength = function() {1007 return this._args.reduce(function(max, arg) {1008 return Math.max(max, arg.name.length);1009 }, 0);1010};10111012/**1013 * Return the pad width.1014 *1015 * @return {Number}1016 * @api private1017 */10181019Command.prototype.padWidth = function() {1020 var width = this.largestOptionLength();1021 if (this._argsDescription && this._args.length) {1022 if (this.largestArgLength() > width) {1023 width = this.largestArgLength();1024 }1025 }10261027 if (this.commands && this.commands.length) {1028 if (this.largestCommandLength() > width) {1029 width = this.largestCommandLength();1030 }1031 }10321033 return width;1034};10351036/**1037 * Return help for options.1038 *1039 * @return {String}1040 * @api private1041 */10421043Command.prototype.optionHelp = function() {962 var width = this.largestOptionLength();1044 var width = this.padWidth();10451046 // Append the help information1047 return this.options.map(function(option) {1048 return pad(option.flags, width) + ' ' + option.description +
11 unmodified lines
10601061Command.prototype.commandHelp = function() {1062 if (!this.commands.length) return '';1063982 var commands = this.commands.filter(function(cmd) {983 return !cmd._noHelp;984 }).map(function(cmd) {985 var args = cmd._args.map(function(arg) {986 return humanReadableArgName(arg);987 }).join(' ');1064 var commands = this.prepareCommands();1065 var width = this.padWidth();1066989 return [990 cmd._name +991 (cmd._alias ? '|' + cmd._alias : '') +992 (cmd.options.length ? ' [options]' : '') +993 (args ? ' ' + args : ''),994 cmd._description995 ];996 });997998 var width = commands.reduce(function(max, command) {999 return Math.max(max, command[0].length);1000 }, 0);10011067 return [1003 '',1068 ' Commands:',1069 '',1070 commands.map(function(cmd) {1071 var desc = cmd[1] ? ' ' + cmd[1] : '';
16 unmodified lines
1088 desc = [1089 ' ' + this._description,1090 ''1091 ];10921093 var argsDescription = this._argsDescription;1094 if (argsDescription && this._args.length) {1095 var width = this.padWidth();1096 desc.push(' Arguments:');1097 desc.push('');1098 this._args.forEach(function(arg) {1099 desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);1100 });1101 desc.push('');1102 }1103 }11041105 var cmdName = this._name;1106 if (this._alias) {
9 unmodified lines
1116 var commandHelp = this.commandHelp();1117 if (commandHelp) cmds = [commandHelp];11181119 var options = [1045 '',1120 ' Options:',1121 '',1122 '' + this.optionHelp().replace(/^/gm, ' '),1123 ''
108 unmodified lines