Class: Argument<T, Options>
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 } 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;
}
}
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, 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.'
});
}
}
}
Type parameters
Name | Type |
---|---|
T | unknown |
Options | extends Options = Options |
Hierarchy
-
AliasPiece
<Options
,"arguments"
>↳
Argument
Implements
IArgument
<T
>
Constructors
constructor
• new Argument<T
, Options
>(context
, options?
): Argument
<T
, Options
>
Type parameters
Name | Type |
---|---|
T | unknown |
Options | extends ArgumentOptions = ArgumentOptions |
Parameters
Name | Type |
---|---|
context | LoaderContext |
options | Options |
Returns
Argument
<T
, Options
>
Overrides
Defined in
projects/framework/src/lib/structures/Argument.ts:108
Properties
aliases
• aliases: readonly string
[]
The aliases for the piece.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:877
enabled
• enabled: boolean
Whether or not the piece is enabled.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:234
location
• Readonly
location: PieceLocation
The location metadata for the piece's file.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:226
name
• Readonly
name: string
The name of the piece.
Implementation of
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:230
options
• Readonly
options: Options
The raw options passed to this Piece
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:238
store
• Readonly
store: ArgumentStore
The store that contains the piece.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:222
Accessors
container
• get
container(): Container
A reference to the Container object for ease of use.
Returns
See
container
Inherited from
AliasPiece.container
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:244
Methods
error
▸ error(options
): Result
<T
>
Constructs an Err result containing an ArgumentError with a custom type.
Parameters
Name | Type | Description |
---|---|---|
options | Omit <Options <T >, "argument" > | The options to pass to the ArgumentError. |
Returns
Result
<T
>
Defined in
projects/framework/src/lib/structures/Argument.ts:126
ok
▸ ok(value
): Result
<T
>
Wraps a value into a successful value.
Parameters
Name | Type | Description |
---|---|---|
value | T | The value to wrap. |
Returns
Result
<T
>
Defined in
projects/framework/src/lib/structures/Argument.ts:118
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
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:249
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
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:254
reload
▸ reload(): Promise
<void
>
Reloads the piece by loading the same path in the store.
Returns
Promise
<void
>
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:262
run
▸ run(parameter
, context
): AwaitableResult
<T
>
The method which is called when invoking the argument.
Parameters
Name | Type | Description |
---|---|---|
parameter | string | The string parameter to parse. |
context | Context <T > | The context for the method call, contains the message, command, and other options. |
Returns
Implementation of
Defined in
projects/framework/src/lib/structures/Argument.ts:112
toJSON
▸ toJSON(): AliasPieceJSON
Defines the JSON.stringify
behavior of this alias piece.
Returns
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:882
unload
▸ unload(): Promise
<void
>
Unloads and disables the piece.
Returns
Promise
<void
>
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/esm/index.d.mts:258