Skip to content

Commit

Permalink
refactor(command): refactor possible values
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 26, 2021
1 parent 37dc946 commit 045c56e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
3 changes: 1 addition & 2 deletions command/command.ts
Expand Up @@ -589,8 +589,7 @@ export class Command<
const completeHandler: ICompleteHandler = (
cmd: Command,
parent?: Command,
) =>
handler.complete?.(cmd, parent) || handler.values?.(cmd, parent) || [];
) => handler.complete?.(cmd, parent) || [];
this.complete(name, completeHandler, options);
}

Expand Down
24 changes: 8 additions & 16 deletions command/help/_help_generator.ts
Expand Up @@ -240,25 +240,17 @@ export class HelpGenerator {
italic(option.conflicts.map(getFlag).join(", ")),
);

let possibleValues: Array<string | number | RegExp> | undefined;
if (option.value instanceof RegExp) {
possibleValues = [option.value];
} else if (Array.isArray(option.value)) {
possibleValues = option.value;
} else {
const type = this.cmd.getType(option.args[0]?.type)?.handler;
if (type instanceof Type) {
possibleValues = type.values?.(this.cmd, this.cmd.getParent());
const type = this.cmd.getType(option.args[0]?.type)?.handler;
if (type instanceof Type) {
const possibleValues = type.values?.(this.cmd, this.cmd.getParent());
if (possibleValues?.length) {
hints.push(
bold(`Values: `) +
possibleValues.map((value: unknown) => inspect(value)).join(", "),
);
}
}

if (possibleValues) {
hints.push(
bold(`Values: `) +
possibleValues.map((value: unknown) => inspect(value)).join(", "),
);
}

if (hints.length) {
return `(${hints.join(", ")})`;
}
Expand Down
4 changes: 4 additions & 0 deletions command/types/enum.ts
Expand Up @@ -24,6 +24,10 @@ export class EnumType<T extends string | number> extends Type<T> {
public values(): T[] {
return this.allowedValues.slice();
}

public complete(): T[] {
return this.values();
}
}

export type TypeValue<T extends Type<unknown>> = T extends Type<infer V> ? V
Expand Down

0 comments on commit 045c56e

Please sign in to comment.