A generic command parser meant for discord bots, though works for just about anything else
Go to file
2024-03-31 19:12:23 +03:00
.yarn/releases Refactor and new options 2024-03-28 16:44:13 +02:00
examples Added examples, improved readme 2024-03-28 17:53:53 +02:00
scripts Update to support both cjs and esm 2023-04-13 20:18:40 +03:00
src More typings 2024-03-31 19:12:23 +03:00
tests Actually implement the limit... 2024-03-31 18:52:54 +03:00
.drone.yml more tests & enforce structure 2023-02-12 18:08:52 +02:00
.eslintignore meta packages 2022-07-29 14:17:01 +03:00
.eslintrc.json Linter pass 2023-12-14 17:33:54 +02:00
.gitignore Refactor and new options 2024-03-28 16:44:13 +02:00
.yarnrc.yml Print a warning instead of emitting one 2024-03-28 17:21:36 +02:00
babel.config.cjs default help option to commands and subcommands 2023-05-05 17:32:29 +03:00
index.ts More typings 2024-03-31 19:12:23 +03:00
jest.config.cjs default help option to commands and subcommands 2023-05-05 17:32:29 +03:00
package.json v1.7.2 2024-03-31 18:53:16 +03:00
README.md Added examples, improved readme 2024-03-28 17:53:53 +02:00
tsconfig.cjs.json Refactor and new options 2024-03-28 16:44:13 +02:00
tsconfig.json Added examples, improved readme 2024-03-28 17:53:53 +02:00
yarn.lock Refactor and new options 2024-03-28 16:44:13 +02:00

Command Parser

A command parser initially made for quickly making discord bots, but works as a standalone parser too.

Your commands are expected to inherit from the Command class or implement the ICommand interface (requires slightly more work given that interfaces don't provide default implementations for the required functionality). The latter is required if you for any reason need to extend your own superclass of command/component. See examples for more about extending/implementing.

Example projects that implement the parser:
Discord music bot by me
My webserver template (uses parser for CLI and listening for input on stdin)

Example usage


const commands: Command[] = [
    new Command({
        name: 'ping',
        options: [
            {
                name: 'respond',            //
                type: OptionType.BOOLEAN,   // Boolean type expects the parser to have some kind of string -> boolean resolver, see the resolver interface for which methods are expected
                required: true,             // Will throw error if not provided
                defaultValue: true,         // The value that will be given back if no value is given, or if option is required but not provided
                flag: true                  // The parser will look for the option in the "--respond value" form
            }
        ]
    })
]; // Command definitions

const parser = new Parser({commands, prefix: ''}); // Empty prefix
const parsed = await parser.parseMessage('ping --respond');

/**
 * Parsed will be an object cotaining properties args, command, subcommand, and subcommandgroup
 * the args property is an object of the given options mapped by their names, e.g.
 * {
 *   respond: {
 *     ... properties,
 *     value: true
 *   }
 * }
 * 
*/

Some of the option types have built-in parsers (e.g. numbers, strings), but others will require the resolver implementation, and in some cases CommandOption extensions.