Skip to main content
Home
Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
Downloads24/wk
Published11 months ago (0.0.4)

A parser for slash commands

Slash Command Parser

A generic parser for slash command text input. Pulls inspiration from Slack and Discord slash commands.

Usage

Basic Usage:

import parse from 'jsr:@inro/slash-command-parser'

const interaction = parse('/todos add name: My Todo Name')

assert(interaction, {
  text: 'add name: My Todo Name',
  command: 'todos',
  options: { name: 'My Todo Name' },
  subCommands: ['add'],
})

Pass in a template to parse options:

import parse, {
  OptionDefinition,
  OptionType,
} from 'jsr:@inro/slash-command-parser'

const template: OptionDefinition[] = [
  { name: 'item', type: OptionType.string },
  { name: 'count', type: OptionType.integer },
  { name: 'complete', type: OptionType.boolean },
]

const interaction = parse(
  '/todos add item: acorn count: 2 complete: false',
  template,
)

assert(interaction, {
  text: 'add item: acorn count: 2 complete: false',
  command: 'todos',
  options: { item: 'acorn', count: 2, complete: false },
  subCommands: ['add'],
})

If you want greater control over parsing or error handling, you can call each step individually:

import {
  parseCommand,
  parseOptions,
  parseSubCommands,
} from 'jsr:@inro/slash-command-parser'

const content = 'add item: lettuce howmany: 2 complete: false'

// Just parsing command + text can give you a "Slack"-style slashcommand
// Also useful if you want to use a custom subCommand or options parser
const { command, text } = parseCommand(content)

// Expand to support 4 sub-commands instead of 2
const { subCommands, remaining } = parseSubCommands(text, 4)
const options = parseOptions(remaining, template)

console.log({ command, text, options, subCommands })

See more usage examples in the test file.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@inro/slash-command-parser

Import symbol

import * as slash_command_parser from "@inro/slash-command-parser";
or

Import directly with a jsr specifier

import * as slash_command_parser from "jsr:@inro/slash-command-parser";

Add Package

pnpm i jsr:@inro/slash-command-parser
or (using pnpm 10.8 or older)
pnpm dlx jsr add @inro/slash-command-parser

Import symbol

import * as slash_command_parser from "@inro/slash-command-parser";

Add Package

yarn add jsr:@inro/slash-command-parser
or (using Yarn 4.8 or older)
yarn dlx jsr add @inro/slash-command-parser

Import symbol

import * as slash_command_parser from "@inro/slash-command-parser";

Add Package

vlt install jsr:@inro/slash-command-parser

Import symbol

import * as slash_command_parser from "@inro/slash-command-parser";

Add Package

npx jsr add @inro/slash-command-parser

Import symbol

import * as slash_command_parser from "@inro/slash-command-parser";

Add Package

bunx jsr add @inro/slash-command-parser

Import symbol

import * as slash_command_parser from "@inro/slash-command-parser";