command-parser
Simple crate that can be used to parse commands for chatbots like for example on twitch.
Command Syntax
For more information about prefixes look at the fields of this struct.
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 arearg1
andlong arg 2
. - options: Options are a set of words.
They are prefixed with the
option_prefix
. The only option in the example isopt
. - 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 abovekey1
s value isval1
andkey2
s value islong 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 ;
use ;
let p = new;
let command_string = r##"!foo arg1 "long arg 2" -opt -opt -key1:val1 -key2:"long val2""##;
let command = p.parse.unwrap;
assert_eq!;
assert_eq!;
assert!;
assert_eq!;