Skip to content
Myr

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

2 files+101 added−27 removed

modified:test/test.command.help.js+1 added-1 removed
modified:index.js+100 added-26 removed
855 unmodified lines
856/**857 * Set the description to `str`.858 *859 * @param {String} str860 * @param {Object} argsDescription861 * @return {String|Command}862 * @api public863 */864864Command.prototype.description = function(str) {865Command.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};871872/**
55 unmodified lines
928 return this;929};930931/**932 * Return prepared commands.933 *934 * @return {Array}935 * @api private936 */937938Command.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(' ');945946 return [947 cmd._name948 + (cmd._alias ? '|' + cmd._alias : '')949 + (cmd.options.length ? ' [options]' : '')950 + ' ' + args951 , cmd._description952 ];953 });954};955956/**957 * Return the largest command length.958 *959 * @return {Number}960 * @api private961 */962963Command.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};969970/**971 * Return the largest option length.972 *973 * @return {Number}974 * @api private975 */976977Command.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};986987/**988 * Return the largest arg length.989 *990 * @return {Number}991 * @api private992 */993994Command.prototype.largestArgLength = function() {995 return this._args.reduce(function(max, arg) {996 return Math.max(max, arg.name.length);997 }, 0);998};9991000/**1001 * Return the pad width.1002 *1003 * @return {Number}1004 * @api private1005 */10061007Command.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 }10141015 if (this.commands && this.commands.length) {1016 if (this.largestCommandLength() > width) {1017 width = this.largestCommandLength();1018 }1019 }10201021 return width;1022};10231024/**1025 * Return help for options.1026 *1027 * @return {String}1028 * @api private1029 */10301031Command.prototype.optionHelp = function() {950 var width = this.largestOptionLength();1032 var width = this.padWidth();10331034 // Append the help information1035 return this.options.map(function(option) {1036 return pad(option.flags, width) + ' ' + option.description
11 unmodified lines
10481049Command.prototype.commandHelp = function() {1050 if (!this.commands.length) return '';1051970 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();1054977 return [978 cmd._name979 + (cmd._alias ? '|' + cmd._alias : '')980 + (cmd.options.length ? ' [options]' : '')981 + ' ' + args982 , cmd._description983 ];984 });985986 var width = commands.reduce(function(max, command) {987 return Math.max(max, command[0].length);988 }, 0);9891055 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 lines
1076 desc = [1077 ' ' + this._description1078 , ''1079 ];10801081 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 }10921093 var cmdName = this._name;1094 if (this._alias) {
9 unmodified lines
1104 var commandHelp = this.commandHelp();1105 if (commandHelp) cmds = [commandHelp];11061107 var options = [1033 ''1034 , ' Options:'1108 ' Options:'1109 , ''1110 , '' + this.optionHelp().replace(/^/gm, ' ')1111 , ''1112 ];
108 unmodified lines