Skip to content

Commit

Permalink
Types for version getter (#1982)
Browse files Browse the repository at this point in the history
* Update types since .version() returns version.

* Remove extra space
  • Loading branch information
shadowspawn authored Aug 26, 2023
1 parent 4c095d1 commit f5413db
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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`);
Expand Down
4 changes: 4 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ expectType<commander.Command | null>(program.parent);
expectType<commander.Command>(program.version('1.2.3'));
expectType<commander.Command>(program.version('1.2.3', '-r,--revision'));
expectType<commander.Command>(program.version('1.2.3', '-r,--revision', 'show revision information'));
expectType<string | undefined>(program.version());

// command (and CommandOptions)
expectType<commander.Command>(program.command('action'));
Expand Down

0 comments on commit f5413db

Please sign in to comment.