diff --git a/lib/command.js b/lib/command.js index 2fc93fd3f..1ade8881f 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1825,17 +1825,16 @@ Expecting one of '${allowedValues.join("', '")}'`); } /** - * Set the program version to `str`. + * Get or set the program version. * - * This method auto-registers the "-V, --version" flag - * which will print the version number when passed. + * This method auto-registers the "-V, --version" option which will print the version number. * - * You can optionally supply the flags and description to override the defaults. + * You can optionally supply the flags and description to override the defaults. * - * @param {string} str + * @param {string} [str] * @param {string} [flags] * @param {string} [description] - * @return {this | string} `this` command for chaining, or version string if no arguments + * @return {this | string | undefined} `this` command for chaining, or version string if no arguments */ version(str, flags, description) { @@ -1844,7 +1843,7 @@ Expecting one of '${allowedValues.join("', '")}'`); flags = flags || '-V, --version'; description = description || 'output the version number'; const versionOption = this.createOption(flags, description); - this._versionOptionName = versionOption.attributeName(); + this._versionOptionName = versionOption.attributeName(); // [sic] not defined in constructor, partly legacy, partly only needed at root this.options.push(versionOption); this.on('option:' + versionOption.name(), () => { this._outputConfiguration.writeOut(`${str}\n`); diff --git a/typings/index.d.ts b/typings/index.d.ts index c9d3c314c..8497fd214 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -294,6 +294,10 @@ export class Command { * You can optionally supply the flags and description to override the defaults. */ version(str: string, flags?: string, description?: string): this; + /** + * Get the program version. + */ + version(): string | undefined; /** * Define a command, implemented using an action handler. diff --git a/typings/index.test-d.ts b/typings/index.test-d.ts index 5d17dd17a..7296e4cf2 100644 --- a/typings/index.test-d.ts +++ b/typings/index.test-d.ts @@ -36,6 +36,7 @@ expectType(program.parent); expectType(program.version('1.2.3')); expectType(program.version('1.2.3', '-r,--revision')); expectType(program.version('1.2.3', '-r,--revision', 'show revision information')); +expectType(program.version()); // command (and CommandOptions) expectType(program.command('action'));