3 releases (stable)
1.0.1 | Jun 26, 2024 |
---|---|
0.0.1 | Jun 25, 2024 |
#1483 in Web programming
18KB
278 lines
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 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 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()));
Dependencies
~255–710KB
~17K SLoC