Skip to content

Commit

Permalink
feat(command): allow method as version param
Browse files Browse the repository at this point in the history
fix command binding for version handler on child command
  • Loading branch information
c4spar committed Mar 16, 2021
1 parent fd647b8 commit e6c3ccc
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions command/command.ts
Expand Up @@ -927,7 +927,7 @@ export class Command<
});
}

if (this.ver && this._versionOption !== false) {
if (this._versionOption !== false && (this._versionOption || this.ver)) {
this.option(
this._versionOption?.flags || "-V, --version",
this._versionOption?.desc ||
Expand All @@ -936,9 +936,7 @@ export class Command<
standalone: true,
prepend: true,
action: async function () {
await Deno.stdout.write(
new TextEncoder().encode(this.getVersion() + "\n"),
);
this.showVersion();
Deno.exit(0);
},
...(this._versionOption?.opts ?? {}),
Expand Down Expand Up @@ -1305,7 +1303,12 @@ export class Command<

/** Get command version. */
public getVersion(): string | undefined {
return this.ver?.call(this, this) ?? this._parent?.getVersion();
return this.getVersionHandler()?.call(this, this);
}

/** Get help handler method. */
private getVersionHandler(): IVersionHandler | undefined {
return this.ver ?? this._parent?.getVersionHandler();
}

/** Get command description. */
Expand Down Expand Up @@ -1334,6 +1337,11 @@ export class Command<
return this.literalArgs;
}

/** Output generated help without exiting. */
public showVersion() {
Deno.stdout.writeSync(new TextEncoder().encode(this.getVersion()));
}

/** Output generated help without exiting. */
public showHelp() {
Deno.stdout.writeSync(new TextEncoder().encode(this.getHelp()));
Expand Down

0 comments on commit e6c3ccc

Please sign in to comment.