command-parser

Crates.iocommand-parser
lib.rscommand-parser
version1.0.1
sourcesrc
created_at2024-06-25 23:59:59.384127
updated_at2024-06-26 00:09:48.102751
descriptionparse commands for chat bots
homepagehttps://github.com/mzntori/command-parser
repositoryhttps://github.com/mzntori/command-parser
max_upload_size
id1283980
size18,728
tori (mzntori)

documentation

https://docs.rs/command-parser/latest

README

command-parser

Simple crate that can be used to parse commands for chatbots like for example on twitch.

Command Syntax

In any examples in this documentation ! will be used as a prefix and - will be used as a option prefix.

A command that this can parse could look like this:

!foo arg1 "long arg 2" -opt -opt -key1:val1 -key2:"long val2"

A command consists of 4 different parts:

  • name: The name of the command is the first word after the prefix. In the example above that's foo.
  • arguments: Arguments are simple strings passed to the command. They are either single words or strings with spaces enclosed by ". In the example the two arguments are arg1 and long arg 2.
  • options: Options are a set of words. They are prefixed with the option_prefix. The only option in the example is opt.
  • parameters: Parameters are key-value pairs. They are prefixed with the option_prefix and seperated by :. The value part of the pair can be a word or a string enclosed by ". In the example above key1s value is val1 and key2s value is long val2.

Escaping

Since " is used to mark the borders of long arguments and values, it's not normally possible to include them in the string of the argument.

You can escape a long argument or value using :

  • \": produces "
  • \\: produces \

Example

use std::collections::{HashMap, HashSet};
use command_parser::{Parser, Command};

let p = Parser::new('!', '-');
let command_string = r##"!foo arg1 "long arg 2" -opt -opt -key1:val1 -key2:"long val2""##;
let command = p.parse(command_string).unwrap();

assert_eq!(command.name, "foo");
assert_eq!(command.arguments[1], "long arg 2");
assert!(command.options.contains("opt"));
assert_eq!(command.parameters.get("key2"), Some(&"long val2".to_string()));
Commit count: 12

cargo fmt