Skip to main content

Class: Argument<T, O>

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).

example

// TypeScript:
import { Argument, PieceContext } from '@sapphire/framework';
import { URL } from '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: PieceContext) {
super(context, { name: 'hyperlink', aliases: ['url'] });
}

public run(argument: string): Argument.Result<URL> {
try {
return this.ok(new URL(argument));
} catch {
return this.error(argument, 'ArgumentHyperlinkInvalidURL', '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;
}
}

example

// 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) {
try {
return this.ok(new URL(argument));
} catch {
return this.error(argument, 'ArgumentHyperlinkInvalidURL', 'The argument did not resolve to a valid URL.');
}
}
}

Type parameters

NameType
Tunknown
Oextends Options = Options

Hierarchy

Implements

Constructors

constructor

โ€ข new Argument<T, O>(context, options?)

Type parameters

NameType
Tunknown
Oextends ArgumentOptions<O> = ArgumentOptions

Parameters

NameType
contextPieceContext
options?AliasPieceOptions

Inherited from

AliasPiece.constructor

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:691

Properties

aliases

โ€ข aliases: readonly string[]

The aliases for the piece.

Inherited from

AliasPiece.aliases

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:690


enabled

โ€ข enabled: boolean

Whether or not the piece is enabled.

Inherited from

AliasPiece.enabled

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:447


location

โ€ข Readonly location: PieceLocation

The location metadata for the piece's file.

Inherited from

AliasPiece.location

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:439


name

โ€ข Readonly name: string

The name of the piece.

Implementation of

IArgument.name

Inherited from

AliasPiece.name

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:443


options

โ€ข Readonly options: O

The raw options passed to this Piece

Inherited from

AliasPiece.options

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:451


store

โ€ข Readonly store: Store<Piece<PieceOptions>>

The store that contains the piece.

Inherited from

AliasPiece.store

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:435

Accessors

container

โ€ข get container(): Container

A reference to the {@link Container} object for ease of use.

see container

Returns

Container

Inherited from

AliasPiece.container

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:457

Methods

error

โ–ธ error(options): Result<T>

Constructs an ArgumentError with a custom type.

Parameters

NameType
optionsOmit<Options<T>, "argument">

Returns

Result<T>

Defined in

projects/framework/src/lib/structures/Argument.ts:107


ok

โ–ธ ok(value): Result<T>

Wraps a value into a successful value.

Parameters

NameTypeDescription
valueTThe value to wrap.

Returns

Result<T>

Defined in

projects/framework/src/lib/structures/Argument.ts:97


onLoad

โ–ธ onLoad(): unknown

Per-piece listener that is called when the piece is loaded into the store. Useful to set-up asynchronous initialization tasks.

Returns

unknown

Inherited from

AliasPiece.onLoad

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:462


onUnload

โ–ธ onUnload(): unknown

Per-piece listener that is called when the piece is unloaded from the store. Useful to set-up clean-up tasks.

Returns

unknown

Inherited from

AliasPiece.onUnload

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:467


reload

โ–ธ reload(): Promise<void>

Reloads the piece by loading the same path in the store.

Returns

Promise<void>

Inherited from

AliasPiece.reload

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:475


run

โ–ธ Abstract run(parameter, context): Result<T>

The method which is called when invoking the argument.

Parameters

NameType
parameterstring
contextContext<T>

Returns

Result<T>

Implementation of

IArgument.run

Defined in

projects/framework/src/lib/structures/Argument.ts:91


toJSON

โ–ธ toJSON(): AliasPieceJSON

Defines the JSON.stringify behavior of this alias piece.

Returns

AliasPieceJSON

Inherited from

AliasPiece.toJSON

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:695


unload

โ–ธ unload(): Promise<void>

Unloads and disables the piece.

Returns

Promise<void>

Inherited from

AliasPiece.unload

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:471