Merge branch 'idmontie-feature/issue-47-add-version-help-message-override' into release/3.0.0
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by John Gee · parents d060897, c61d724 · Browse files · 8 files·+248 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+248 added−17 removed
- tests:+133 added−0 removed
- other:+115 added−17 removed
Provenance
- Commit
afb8156- Tree
87f3a53- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
d060897- Parent 2
c61d724
8 files changed+248 added−17 removed
added:examples/custom-help-description+21 added
1+#!/usr/bin/env node2+3+/**4+ * Module dependencies.5+ */6+7+var program = require('../');8+9+program10+ .helpOption('-c, --HELP', 'custom help message')11+ .option('-s, --sessions', 'add session support')12+ .option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade');13+14+program15+ .command('child')16+ .option('--gender', 'specific gender of child')17+ .action((cmd) => {18+ console.log('Childsubcommand...');19+ });20+21+program.parse(process.argv);added:examples/custom-version+13 added
1+#!/usr/bin/env node2+3+/**4+ * Module dependencies.5+ */6+7+var program = require('../');8+9+program10+ .version('0.0.1', '-v, --VERSION', 'new version message')11+ .option('-s, --sessions', 'add session support')12+ .option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade')13+ .parse(process.argv);added:test/test.command.helpInformation.custom.js+46 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546var program = require('../') , sinon = require('sinon').sandbox.create() , should = require('should');
sinon.stub(process, 'exit');sinon.stub(process.stdout, 'write');
program.helpOption('-c, --HELP', 'custom help output');program.command('somecommand');program.command('anothercommand [options]');
var expectedHelpInformation = [ 'Usage: [options] [command]', '', 'Options:', ' -c, --HELP custom help output', '', 'Commands:', ' somecommand', ' anothercommand [options]', ''].join('\n');
program.helpInformation().should.equal(expectedHelpInformation);
// Test argumentsvar expectedCommandHelpInformation = [ 'Usage: test [options] [command]', '', 'Options:', ' -c, --HELP custom help output', '', 'Commands:', ' somecommand', ' anothercommand [options]', ''].join('\n');
program.parse(['node', 'test', '--HELP']);
process.stdout.write.called.should.equal(true);
var output = process.stdout.write.args[0];output[0].should.equal(expectedCommandHelpInformation);
sinon.restore();added:test/test.command.helpSubCommand.customFlags.js+65 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465var program = require('../'), sinon = require('sinon').sandbox.create(), should = require('should');
sinon.stub(process, 'exit');sinon.stub(process.stdout, 'write');
// Test that subcommands inherit the help flags// but can also override help flagsprogram .helpOption('-i, --ihelp', 'foo foo');
program .command('child') .option('--gender', 'specific gender of child') .action((cmd) => { console.log('Childsubcommand...'); });
program .command('family') .helpOption('-h, --help') .action((cmd) => { console.log('Familysubcommand...'); });
// Test argumentsvar expectedCommandHelpInformation = [ 'Usage: child [options]', '', 'Options:', ' --gender specific gender of child', ' -i, --ihelp foo foo', ''].join('\n');
program.parse(['node', 'test', 'child', '-i']);
process.stdout.write.called.should.equal(true);
var output = process.stdout.write.args[0];output[0].should.equal(expectedCommandHelpInformation);
// Test other commandsinon.restore();
sinon.stub(process, 'exit');sinon.stub(process.stdout, 'write');
var expectedFamilyCommandHelpInformation = [ 'Usage: family [options]', '', 'Options:', ' -h, --help foo foo', ''].join('\n');
program.parse(['node', 'test', 'family', '-h']);
process.stdout.write.called.should.equal(true);
var output2 = process.stdout.write.args[0];output2[0].should.equal(expectedFamilyCommandHelpInformation);
sinon.restore();added:test/test.options.version.customHelpDescription.js+22 added
12345678910111213141516171819202122var program = require('../') , sinon = require('sinon').sandbox.create() , should = require('should');
program.version('1.0.0', undefined, 'custom version output');program.command('somecommand');program.command('anothercommand [options]');
var expectedHelpInformation = [ 'Usage: [options] [command]', '', 'Options:', ' -V, --version custom version output', ' -h, --help output usage information', '', 'Commands:', ' somecommand', ' anothercommand [options]', ''].join('\n');
program.helpInformation().should.equal(expectedHelpInformation);modified:typings/index.d.ts+15 added-8 removed
36 unmodified lines37 */38 constructor(name?: string);39 40 /**41- * Set the program version to `str`.41+ * Set the program version to `str`. 42 *43 * This method auto-registers the "-V, --version" flag44 * which will print the version number when passed.45+ * 46+ * You can optionally supply the flags and description to override the defaults.47 *46- * @param {string} str47- * @param {string} [flags]48- * @returns {Command} for chaining48 */50- version(str: string, flags?: string): Command;49+ version(str: string, flags?: string, description?: string): Command;50 51 /**52 * Add command `name`.53 *214 unmodified lines268 269 /**270 * Output help information for this command.271 *272+ * When listener(s) are available for the helpLongFlag273+ * those callbacks are invoked.274+ * 275 * @param {(str: string) => string} [cb]276 */277 outputHelp(cb?: (str: string) => string): void;278 277- /** Output help information and exit.278- *279- * @param {(str: string) => string} [cb]279+ /**280+ * You can pass in flags and a description to override the help281+ * flags and help description for your command.282 */283+ helpOption(flags?: string, description?: string): Command;284+285+ /** 286+ * Output help information and exit.287+ */288 help(cb?: (str: string) => string): never;289 }290 291 }28 unmodified linesmodified:index.js+51 added-9 removed
101 unmodified lines102 this._execs = {};103 this._allowUnknownOption = false;104 this._args = [];105 this._name = name || '';106+107+ this._helpFlags = '-h, --help';108+ this._helpDescription = 'output usage information';109+ this._helpShortFlag = '-h';110+ this._helpLongFlag = '--help';111 }112 113 /**114 * Add command `name`.71 unmodified lines186 this._execs[cmd._name] = true;187 if (opts.isDefault) this.defaultExecutable = cmd._name;188 }189 cmd._noHelp = !!opts.noHelp;190+ cmd._helpFlags = this._helpFlags;191+ cmd._helpDescription = this._helpDescription;192+ cmd._helpShortFlag = this._helpShortFlag;193+ cmd._helpLongFlag = this._helpLongFlag;194 this.commands.push(cmd);195 cmd.parseExpectedArgs(args);196 cmd.parent = this;197 276 unmodified lines474 475 // github-style sub-commands with no sub-command476 if (this.executables && argv.length < 3 && !this.defaultExecutable) {477 // this user needs help469- argv.push('--help');478+ argv.push(this._helpLongFlag);479 }480 481 // process argv482 var normalized = this.normalize(argv.slice(2));45 unmodified lines528 529 // <cmd> --help530 if (args[0] === 'help') {531 args[0] = args[1];523- args[1] = '--help';532+ args[1] = this._helpLongFlag;533 }534 535 // executable536 var f = argv[1];329 unmodified lines866 *867 * This method auto-registers the "-V, --version" flag868 * which will print the version number when passed.869 *870+ * You can optionally supply the flags and description to override the defaults.871+ *872 * @param {String} str873 * @param {String} [flags]874+ * @param {String} [description]875 * @return {Command} for chaining876 * @api public877 */878 867-Command.prototype.version = function(str, flags) {879+Command.prototype.version = function(str, flags, description) {880 if (arguments.length === 0) return this._version;881 this._version = str;882 flags = flags || '-V, --version';883+ description = description || 'output the version number';871- var versionOption = new Option(flags, 'output the version number');884+ var versionOption = new Option(flags, description);885 this._versionOptionName = versionOption.long.substr(2) || 'version';886 this.options.push(versionOption);887 this.on('option:' + this._versionOptionName, function() {888 process.stdout.write(str + '\n');125 unmodified lines1014 1015 Command.prototype.largestOptionLength = function() {1016 var options = [].slice.call(this.options);1017 options.push({1005- flags: '-h, --help'1018+ flags: this._helpFlags1019 });1020+1021 return options.reduce(function(max, option) {1022 return Math.max(max, option.flags.length);1023 }, 0);1024 };48 unmodified lines1073 // Append the help information1074 return this.options.map(function(option) {1075 return pad(option.flags, width) + ' ' + option.description +1076 ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');1063- }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])1077+ }).concat([pad(this._helpFlags, width) + ' ' + this._helpDescription])1078 .join('\n');1079 };1080 1081 /**76 unmodified lines1158 .join('\n');1159 };1160 1161 /**1148- * Output help information for this command1162+ * Output help information for this command.1163 *1164+ * When listener(s) are available for the helpLongFlag1165+ * those callbacks are invoked.1166+ *1167 * @api public1168 */1169 1170 Command.prototype.outputHelp = function(cb) {6 unmodified lines1177 if (typeof cbOutput !== 'string' && !Buffer.isBuffer(cbOutput)) {1178 throw new Error('outputHelp callback must return a string or a Buffer');1179 }1180 process.stdout.write(cbOutput);1164- this.emit('--help');1181+ this.emit(this._helpLongFlag);1182 };1183 1184 /**1185+ * You can pass in flags and a description to override the help1186+ * flags and help description for your command.1187+ *1188+ * @param {String} [flags]1189+ * @param {String} [description]1190+ * @return {Command}1191+ * @api public1192+ */1193+1194+Command.prototype.helpOption = function(flags, description) {1195+ this._helpFlags = flags || this._helpFlags;1196+ this._helpDescription = description || this._helpDescription;1197+1198+ var splitFlags = this._helpFlags.split(/[ ,|]+/);1199+1200+ if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();1201+1202+ this._helpLongFlag = splitFlags.shift();1203+1204+ return this;1205+};1206+1207+/**1208 * Output help information and exit.1209 *1210+ * @param {Function} [cb]1211 * @api public1212 */1213 1214 Command.prototype.help = function(cb) {38 unmodified lines1253 */1254 1255 function outputHelpIfNecessary(cmd, options) {1256 options = options || [];1257+1258 for (var i = 0; i < options.length; i++) {1217- if (options[i] === '--help' || options[i] === '-h') {1259+ if (options[i] === cmd._helpLongFlag || options[i] === cmd._helpShortFlag) {1260 cmd.outputHelp();1261 process.exit(0);1262 }1263 }28 unmodified linesmodified:Readme.md+15 added
222 unmodified lines223 ```js224 program.version('0.0.1', '-v, --version');225 ```226 227+You can also override the help description for the version command.228 229+```js230+program.version('0.0.1', '-v, --version', 'output the current version');231+```232 233+234+235 ## Command-specific options236 237 You can attach options to a command.238 199 unmodified lines438 return colors.red(txt); //display the help text in red on the console439 }440 ```441 442+## .helpOption(flags, description)443+444+ Override the default help flags and description.445+446+```js447+program448+ .helpOption('-e, --HELP', 'read more information');449+```450+451 ## .help(cb)452 453 Output help information and exit immediately.454 Optional callback cb allows post-processing of help text before it is displayed.84 unmodified lines