A generic parser for slash command text input
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

TypeScript 100.0%
7 1 0

Clone this repository

https://tangled.org/bpev.me/slasher https://tangled.org/did:plc:cyokxsil6nwosdaiwmqvlp3s/slasher
git@knot.bpev.me:bpev.me/slasher git@knot.bpev.me:did:plc:cyokxsil6nwosdaiwmqvlp3s/slasher

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

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:@bpev/slasher'

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

{
  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:@bpev/slasher'

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

parse('/todos add item: lettuce howmany: 2 complete: false', template)

{
  text: 'add item: lettuce howmany: 2 complete: false',
  command: 'todos',
  options: { item: 'lettuce', howmany: 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:@bpev/slasher'

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.