@biskyjs/framework
Version:
79 lines • 2.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Argument = void 0;
const args_1 = require("../parsers/args");
const pieces_1 = require("@sapphire/pieces");
/**
* 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 {@link Args}'s methods (usually used inside {@link Command}s by default).
*
* @example
* ```typescript
* // TypeScript:
* import { Argument, PieceContext } 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: 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
* // 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.');
* }
* }
* }
* ```
*/
class Argument extends pieces_1.AliasPiece {
/**
* Wraps a value into a successful value.
* @param value The value to wrap.
*/
ok(value) {
return args_1.Args.ok(value);
}
/**
* Constructs an {@link Err} result containing an {@link ArgumentError} with a custom type.
* @param options The options to pass to the ArgumentError.
*/
error(options) {
return args_1.Args.error({ argument: this, identifier: this.name, ...options });
}
}
exports.Argument = Argument;
//# sourceMappingURL=argument.js.map