pub struct Parser {
pub prefix: char,
pub option_prefix: char,
}
Expand description
Used to parse a Command
from a string.
§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 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 = Command {
prefix: '!',
option_prefix: '-',
name: "foo".to_string(),
arguments: vec!["arg1".to_string(), "long arg 2".to_string()],
options: HashSet::from(["opt".to_string()]),
parameters: HashMap::from([
("key1".to_string(), "val1".to_string()),
("key2".to_string(), "long val2".to_string())
])
};
assert_eq!(p.parse(command_string).unwrap(), command);
Fields§
§prefix: char
Prefix of the command.
<prefix><name> ...
Should not be set to ' '
as most chats trim leading spaces.
option_prefix: char
Prefix of options and parameters.
... <option_prefix><option> ... <option_prefix><param key>:<param value>
Should not be set to ' '
or '"'
as it may not result in expected outcomes.