Skip to main content

Abstract Class: Argument<T, Options>

Defined in: projects/framework/src/lib/structures/Argument.ts:104

The base argument class. This class is abstract and is to be extended by subclasses implementing the methods. In Sapphire's workflow, arguments are called when using Args's methods (usually used inside Commands by default).

Examples​

// TypeScript:
import { Argument } from '@sapphire/framework';
import { URL } from 'node:url';

// Define a class extending `Argument`, then export it.
// NOTE: You can use `export default` or `export =` too.
export class CoreArgument extends Argument<URL> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'hyperlink', aliases: ['url'] });
}

public run(argument: string, context: Argument.Context): Argument.Result<URL> {
try {
return this.ok(new URL(argument));
} catch {
return this.error({
parameter: argument,
context,
identifier: 'ArgumentHyperlinkInvalidURL',
message: 'The argument did not resolve to a valid URL.'
});
}
}
}

// Augment the ArgType structure so `args.pick('url')`, `args.repeat('url')`
// and others have a return type of `URL`.
declare module '@sapphire/framework' {
export interface ArgType {
url: URL;
}
}
// JavaScript:
const { Argument } = require('@sapphire/framework');

// Define a class extending `Argument`, then export it.
module.exports = class CoreArgument extends Argument {
constructor(context) {
super(context, { name: 'hyperlink', aliases: ['url'] });
}

run(argument, context) {
try {
return this.ok(new URL(argument));
} catch {
return this.error({
parameter: argument,
context,
identifier: 'ArgumentHyperlinkInvalidURL',
message: 'The argument did not resolve to a valid URL.'
});
}
}
}

Extends​

  • AliasPiece<Options, "arguments">

Type Parameters​

Type ParameterDefault type
Tunknown
Options extends OptionsOptions

Implements​

Constructors​

Constructor​

new Argument<T, Options>(context: LoaderContext, options: Options): Argument<T, Options>

Defined in: projects/framework/src/lib/structures/Argument.ts:108

Parameters​

ParameterType
contextLoaderContext
optionsOptions

Returns​

Argument<T, Options>

Overrides​

AliasPiece<Options, 'arguments'>.constructor

Methods​

error()​

error(options: Omit<Options<T>, "argument">): Result<T>

Defined in: projects/framework/src/lib/structures/Argument.ts:126

Constructs an Err result containing an ArgumentError with a custom type.

Parameters​

ParameterTypeDescription
optionsOmit<Options<T>, "argument">The options to pass to the ArgumentError.

Returns​

Result<T>


ok()​

ok(value: T): Result<T>

Defined in: projects/framework/src/lib/structures/Argument.ts:118

Wraps a value into a successful value.

Parameters​

ParameterTypeDescription
valueTThe value to wrap.

Returns​

Result<T>


run()​

abstract run(parameter: string, context: Context<T>): AwaitableResult<T>

Defined in: projects/framework/src/lib/structures/Argument.ts:112

The method which is called when invoking the argument.

Parameters​

ParameterTypeDescription
parameterstringThe string parameter to parse.
contextContext<T>The context for the method call, contains the message, command, and other options.

Returns​

AwaitableResult<T>

Implementation of​

IArgument.run