0.2.0 • Published 3 years ago
@mskri/command-parser v0.2.0
command-parser
A library for parsing message into command. Supports positional arguments and options (flags).
Can be used e.g. for node discord/twitch bots. Initially created for Monbot, a node package for creating discord bots.
Installation
$ npm i @mskri/command-parser
or, with yarn
$ yarn add @mskri/command-parser
Usage
parse(commands [, options]): MatchedCommand | FailedCommand | null
Parses a message into command. Returns matched command, failed command (e.g. when command fails due to missing parameter) or a null if the parsed message does not match any commands.
options
trigger: string = '!'
: A string which indicates that the following message is a command. Default trigger is!
.
MatchedCommand
Succesfully parsed command from a message and command did not fail any validations.
type CommandConfig = {
name: string;
trigger: string;
description?: string;
examples?: string[];
parameters?: Record<string, CommandParameter>;
options?: Record<string, CommandOption>;
subCommands?: CommandConfig[];
};
Properties
name: string
- Name of the command.description?: string
- Description for the command. Used in help manual.examples?: string[]
- List of example commands. Used in help manual.parameters?: Record<string, CommandParameter>
- Parameters that were parsed from the message. Parameters that are not defined in command configs are ignored.options?: Record<string, CommandOption>
- Options that were parsed from the message. Extra options passed in the message that are not defined in the command configs are ignored.subCommands?: CommandConfig[]
- Sub-commands that extend the parent command.
FailedCommand
Succesfully parsed command from a message but command failed some validation, e.g. parameters or options validation did not pass.
Properties
name: string
- Name of the command.error: string
- Reason why the command failed.