Skip to content
Myr

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

8 files+248 added−17 removed

added:examples/custom-help-description+21 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program10 .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');1314program15 .command('child')16 .option('--gender', 'specific gender of child')17 .action((cmd) => {18 console.log('Childsubcommand...');19 });2021program.parse(process.argv);
added:examples/custom-version+13 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program10 .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
added:test/test.command.helpSubCommand.customFlags.js+65 added
added:test/test.options.version.customHelpDescription.js+22 added
modified:typings/index.d.ts+15 added-8 removed
36 unmodified lines
37 */38 constructor(name?: string);3940 /**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;5051 /**52 * Add command `name`.53 *
214 unmodified lines
268269 /**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;278277 /** 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;284285 /** 286 * Output help information and exit.287 */288 help(cb?: (str: string) => string): never;289 }290291}
28 unmodified lines
modified:index.js+51 added-9 removed
101 unmodified lines
102 this._execs = {};103 this._allowUnknownOption = false;104 this._args = [];105 this._name = name || '';106107 this._helpFlags = '-h, --help';108 this._helpDescription = 'output usage information';109 this._helpShortFlag = '-h';110 this._helpLongFlag = '--help';111}112113/**114 * Add command `name`.
71 unmodified lines
186 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 lines
474475 // 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 }480481 // process argv482 var normalized = this.normalize(argv.slice(2));
45 unmodified lines
528529 // <cmd> --help530 if (args[0] === 'help') {531 args[0] = args[1];523 args[1] = '--help';532 args[1] = this._helpLongFlag;533 }534535 // executable536 var f = argv[1];
329 unmodified lines
866 *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 */878867Command.prototype.version = function(str, flags) {879Command.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 lines
10141015Command.prototype.largestOptionLength = function() {1016 var options = [].slice.call(this.options);1017 options.push({1005 flags: '-h, --help'1018 flags: this._helpFlags1019 });10201021 return options.reduce(function(max, option) {1022 return Math.max(max, option.flags.length);1023 }, 0);1024};
48 unmodified lines
1073 // 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};10801081/**
76 unmodified lines
1158 .join('\n');1159};11601161/**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 */11691170Command.prototype.outputHelp = function(cb) {
6 unmodified lines
1177 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};11831184/**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 */11931194Command.prototype.helpOption = function(flags, description) {1195 this._helpFlags = flags || this._helpFlags;1196 this._helpDescription = description || this._helpDescription;11971198 var splitFlags = this._helpFlags.split(/[ ,|]+/);11991200 if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();12011202 this._helpLongFlag = splitFlags.shift();12031204 return this;1205};12061207/**1208 * Output help information and exit.1209 *1210 * @param {Function} [cb]1211 * @api public1212 */12131214Command.prototype.help = function(cb) {
38 unmodified lines
1253 */12541255function outputHelpIfNecessary(cmd, options) {1256 options = options || [];12571258 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 lines
modified:Readme.md+15 added
222 unmodified lines
223```js224program.version('0.0.1', '-v, --version');225```226227You can also override the help description for the version command.228229```js230program.version('0.0.1', '-v, --version', 'output the current version');231```232233234235## Command-specific options236237You can attach options to a command.238
199 unmodified lines
438 return colors.red(txt); //display the help text in red on the console439}440```441442## .helpOption(flags, description)443444 Override the default help flags and description.445446```js447program448 .helpOption('-e, --HELP', 'read more information');449```450451## .help(cb)452453 Output help information and exit immediately.454 Optional callback cb allows post-processing of help text before it is displayed.
84 unmodified lines