Skip to content

Commit

Permalink
feat(command): allow method as version param (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Mar 16, 2021
1 parent 2e7c35d commit fd647b8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
29 changes: 21 additions & 8 deletions command/command.ts
Expand Up @@ -59,6 +59,7 @@ import type {
ITypeHandler,
ITypeInfo,
ITypeOptions,
IVersionHandler,
} from "./types.ts";

interface IDefaultOption<
Expand Down Expand Up @@ -106,7 +107,7 @@ export class Command<
private _name = "COMMAND";
private _parent?: P;
private _globalParent?: Command;
private ver?: string;
private ver?: IVersionHandler;
private desc: IDescription = "";
private fn?: IAction;
private options: IOption[] = [];
Expand Down Expand Up @@ -412,17 +413,29 @@ export class Command<

/**
* Set command version.
* @param version Semantic version string.
* @param version Semantic version string string or method that returns the version string.
*/
public version(version: string): this {
this.cmd.ver = version;
public version(
version:
| string
| IVersionHandler<Partial<CO>, Partial<CA>, CG, PG>,
): this {
if (typeof version === "string") {
this.cmd.ver = () => version;
} else if (typeof version === "function") {
this.cmd.ver = version;
}
return this;
}

/**
* Set command help.
* @param help Help string or method that returns the help string.
*/
public help(
help:
| string
| ((this: Command<Partial<CO>, Partial<CA>, CG, PG>) => string)
| IHelpHandler<Partial<CO>, Partial<CA>, CG, PG>
| HelpOptions,
): this {
if (typeof help === "string") {
Expand Down Expand Up @@ -924,7 +937,7 @@ export class Command<
prepend: true,
action: async function () {
await Deno.stdout.write(
new TextEncoder().encode(this.ver + "\n"),
new TextEncoder().encode(this.getVersion() + "\n"),
);
Deno.exit(0);
},
Expand Down Expand Up @@ -1292,7 +1305,7 @@ export class Command<

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

/** Get command description. */
Expand Down Expand Up @@ -1332,7 +1345,7 @@ export class Command<
return this.getHelpHandler().call(this, this);
}

/** Get generated help. */
/** Get help handler method. */
private getHelpHandler(): IHelpHandler {
return this._help ?? this._parent?.getHelpHandler() as IHelpHandler;
}
Expand Down
15 changes: 15 additions & 0 deletions command/types.ts
Expand Up @@ -220,3 +220,18 @@ export type IHelpHandler<
P extends Command | undefined = any,
C extends Command<O, A, G, PG, P> = Command<O, A, G, PG, P>,
> = (this: C, cmd: C) => string;

/** Version callback method to print the version. Invoked by the `--help` option command and the `.getVersion()` and `.showHelp()` method's. */
export type IVersionHandler<
// deno-lint-ignore no-explicit-any
O extends Record<string, any> | void = any,
// deno-lint-ignore no-explicit-any
A extends Array<unknown> = any,
// deno-lint-ignore no-explicit-any
G extends Record<string, any> | void = any,
// deno-lint-ignore no-explicit-any
PG extends Record<string, any> | void = any,
// deno-lint-ignore no-explicit-any
P extends Command | undefined = any,
C extends Command<O, A, G, PG, P> = Command<O, A, G, PG, P>,
> = (this: C, cmd: C) => string;

0 comments on commit fd647b8

Please sign in to comment.