Crates.io | command-parser |
lib.rs | command-parser |
version | 1.0.1 |
source | src |
created_at | 2024-06-25 23:59:59.384127 |
updated_at | 2024-06-26 00:09:48.102751 |
description | parse commands for chat bots |
homepage | https://github.com/mzntori/command-parser |
repository | https://github.com/mzntori/command-parser |
max_upload_size | |
id | 1283980 |
size | 18,728 |
Simple crate that can be used to parse commands for chatbots like for example on twitch.
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:
foo
."
.
In the example the two arguments are arg1
and long arg 2
.option_prefix
.
The only option in the example is opt
.option_prefix
and seperated by :
.
The value part of the pair can be a word or a string enclosed by "
.
In the example above key1
s value is val1
and key2
s value is long val2
.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 \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()));