Skip to main content
Module

x/cliffy/command/types/enum.ts

Command line framework for deno 🦕 Including Commandline-Interfaces, Prompts, CLI-Table, Arguments Parser and more...
Extremely Popular
Go to Latest
File
import { Type } from "../type.ts";import type { ArgumentValue } from "../types.ts";import { InvalidTypeError } from "../../flags/_errors.ts";
/** Enum type. Allows only provided values. */export class EnumType<TValue extends string | number | boolean> extends Type<TValue> { private readonly allowedValues: ReadonlyArray<TValue>;
constructor(values: ReadonlyArray<TValue> | Record<string, TValue>) { super(); this.allowedValues = Array.isArray(values) ? values : Object.values(values); }
public parse(type: ArgumentValue): TValue { for (const value of this.allowedValues) { if (value.toString() === type.value) { return value; } }
throw new InvalidTypeError(type, this.allowedValues.slice()); }
public override values(): Array<TValue> { return this.allowedValues.slice(); }
public override complete(): Array<TValue> { return this.values(); }}