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
Provenance
- Commit
c92ca1d- Tree
4ffc145- Author
Tomoaki Abe <abe@enzou.tokyo>- Committer
GitHub <noreply@github.com>- Parent 1
6b026a5- Parent 2
1d52048
2 files changed+100 added−26 removed
modified:test/test.command.help.js+2 added-2 removed
3 unmodified lines4567889101112123 unmodified lines
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');No newline at end of filemodified:index.js+98 added-24 removed
867 unmodified lines868 /**869 * Set the description to `str`.870 *871 * @param {String} str872+ * @param {Object} argsDescription873 * @return {String|Command}874 * @api public875 */876 876-Command.prototype.description = function(str) {877+Command.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 };883 884 /**55 unmodified lines940 return this;941 };942 943 /**944+ * Return prepared commands.945+ *946+ * @return {Array}947+ * @api private948+ */949+950+Command.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(' ');957+958+ return [959+ cmd._name +960+ (cmd._alias ? '|' + cmd._alias : '') +961+ (cmd.options.length ? ' [options]' : '') +962+ (args ? ' ' + args : ''),963+ cmd._description964+ ];965+ });966+};967+968+/**969+ * Return the largest command length.970+ *971+ * @return {Number}972+ * @api private973+ */974+975+Command.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+};981+982+/**983 * Return the largest option length.984 *985 * @return {Number}986 * @api private987 */988 989 Command.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 };998 999 /**1000+ * Return the largest arg length.1001+ *1002+ * @return {Number}1003+ * @api private1004+ */1005+1006+Command.prototype.largestArgLength = function() {1007+ return this._args.reduce(function(max, arg) {1008+ return Math.max(max, arg.name.length);1009+ }, 0);1010+};1011+1012+/**1013+ * Return the pad width.1014+ *1015+ * @return {Number}1016+ * @api private1017+ */1018+1019+Command.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+ }1026+1027+ if (this.commands && this.commands.length) {1028+ if (this.largestCommandLength() > width) {1029+ width = this.largestCommandLength();1030+ }1031+ }1032+1033+ return width;1034+};1035+1036+/**1037 * Return help for options.1038 *1039 * @return {String}1040 * @api private1041 */1042 1043 Command.prototype.optionHelp = function() {962- var width = this.largestOptionLength();1044+ var width = this.padWidth();1045 1046 // Append the help information1047 return this.options.map(function(option) {1048 return pad(option.flags, width) + ' ' + option.description +11 unmodified lines1060 1061 Command.prototype.commandHelp = function() {1062 if (!this.commands.length) return '';1063 982- 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();1066 989- return [990- cmd._name +991- (cmd._alias ? '|' + cmd._alias : '') +992- (cmd.options.length ? ' [options]' : '') +993- (args ? ' ' + args : ''),994- cmd._description995- ];996- });997-998- var width = commands.reduce(function(max, command) {999- return Math.max(max, command[0].length);1000- }, 0);1001-1067 return [1003- '',1068 ' Commands:',1069 '',1070 commands.map(function(cmd) {1071 var desc = cmd[1] ? ' ' + cmd[1] : '';16 unmodified lines1088 desc = [1089 ' ' + this._description,1090 ''1091 ];1092+1093+ 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 }1104 1105 var cmdName = this._name;1106 if (this._alias) {9 unmodified lines1116 var commandHelp = this.commandHelp();1117 if (commandHelp) cmds = [commandHelp];1118 1119 var options = [1045- '',1120 ' Options:',1121 '',1122 '' + this.optionHelp().replace(/^/gm, ' '),1123 ''108 unmodified lines