Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types for version getter #1982

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1818,17 +1818,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 @@ -1837,7 +1836,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