Skip to content

Commit

Permalink
feat: Add Argument class and types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayfri committed Aug 31, 2021
1 parent 8189ca2 commit 3171882
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/classes/arguments/Argument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {ArgumentContext} from '../contexts';

type ArgumentValidator = (argument: string, ctx: ArgumentContext) => boolean;
type ArgumentParser<T> = (argument: string, ctx: ArgumentContext) => T | null | Promise<T | null>;

export type ArgumentFunction<T> = (options: ArgumentBuilder<T>) => Argument<T>;

export enum ArgumentType {
BOOLEAN = 'boolean',
CHANNEL = 'channel',
COMMAND = 'command',
CHOICE = 'choice',
EMOJI = 'emoji',
ENUM = 'enum',
EVENT = 'event',
FLOAT = 'float',
GUILD = 'guild',
GUILD_MEMBER = 'guild_member',
INTEGER = 'integer',
MESSAGE = 'message',
REGEX = 'regex',
ROLE = 'role',
SNOWFLAKE = 'snowflake',
STRING = 'string',
TEXT_CHANNEL = 'text_channel',
USER = 'user',
}

type DefaultValueArgument<T> = {defaultValue: T};
type CoalescingArgument = {coalescing: boolean};
type OptionalArgument = {optional: boolean};
export type ArgumentBuilder<T> = Partial<(DefaultValueArgument<T> | CoalescingArgument | OptionalArgument) & {description: string}>;

export interface ArgumentOptions<T> {
coalescing?: boolean;
defaultValue?: T;
description?: string;
optional?: boolean;
}

export class Argument<T> {
public constructor(public type: ArgumentType, public options: ArgumentOptions<T>, public validator: ArgumentValidator, public parser: ArgumentParser<T>) {}
}

export class CommandArgument<T> {
public coalescing: boolean;
public defaultValue: T | undefined;
public description: string;
public optional: boolean;
public parse: ArgumentParser<T>;
public type: ArgumentType;
public validate: ArgumentValidator;

public constructor(public name: string, public index: number, argument: Argument<T>) {
this.validate = argument.validator;
this.coalescing = argument.options.coalescing ?? false;
this.defaultValue = argument.options.defaultValue ?? undefined;
this.description = argument.options.description ?? '';
this.optional = argument.options.optional ?? false;
this.type = argument.type;
this.parse = argument.parser;
}

public get isSimple() {
return !this.optional && !this.coalescing && !this.defaultValue;
}
}

0 comments on commit 3171882

Please sign in to comment.