@feizk/parser
v2.0.1
Published
A flexible package to parse messages for commands, subcommands, and arguments with configurable prefixes
Maintainers
Readme
@feizk/parser
A flexible package to parse messages for commands and arguments with configurable prefixes.
Installation
npm install @feizk/parserUsage
import { Parser } from '@feizk/parser';
const parser = new Parser({ prefix: '!' });
const result = await parser.parse('!help filter name(test) <@123>');
if (result) {
console.log(result.command); // 'help'
console.log(result.subcommands); // ['filter']
console.log(result.args); // { name: 'test' }
console.log(result.mentions); // [{ type: 'user', id: '123' }]
}Options
| Option | Type | Default | Description |
| --------------- | -------------------------------- | --------- | ------------------------------------- |
| prefix | string \| string[] | - | Required. The prefix(es) to match |
| caseSensitive | boolean | false | Case sensitivity for prefix matching |
| delimiter | string | ' ' | Argument delimiter |
| argFormat | 'typed' \| 'equals' \| 'named' | 'typed' | Argument format style |
| debug | boolean | false | Enable debug logging |
Argument Formats
- typed:
key(value) - equals:
key=value - named:
--key value
Schema Validation
parser.registerSchema('help', {
allowedSubcommands: ['filter'],
args: {
name: { type: 'string', required: true, minLength: 3, maxLength: 50 },
status: { type: 'string', allowedValues: ['active', 'inactive'] },
age: { type: 'number', min: 0, max: 120 },
createdAt: {
type: 'date',
min: '2020-01-01T00:00:00Z',
max: '2030-01-01T00:00:00Z',
},
tags: {
type: 'array',
minItems: 1,
maxItems: 10,
itemType: 'string',
pattern: '^[a-zA-Z0-9]+$',
},
email: { type: 'string', pattern: '^[^@]+@[^@]+\\.[^@]+$' },
},
});
const result = parser.parse(
'!help filter name(test) createdAt(2023-01-01T00:00:00Z) tags(a,b,c)',
);
// Validates against schema and returns validation errors if anyArgument Validation Options
In addition to type and required, arguments can have the following validation properties:
String arguments:
minLength: Minimum lengthmaxLength: Maximum lengthpattern: Regex pattern to matchallowedValues: Array of allowed string values
Number arguments:
min: Minimum valuemax: Maximum valueallowedValues: Array of allowed number values
Date arguments:
min: Minimum date (ISO string or timestamp)max: Maximum date (ISO string or timestamp)allowedValues: Array of allowed date values
Array arguments:
minItems: Minimum number of itemsmaxItems: Maximum number of itemsitemType: Type that each element must be (e.g., 'string', 'number')pattern: Regex pattern to match for each string elementallowedValues: Array of allowed values (each item must be in this list)
All types:
allowedValues: List of allowed values for the argument
License
MIT
