Skip to content

Commit

Permalink
Allow delimiters to be set on a per command basis (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis authored and Lakelezz committed Aug 18, 2019
1 parent 3a4e2ed commit 6f7797e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions command_attr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
_ => {
match_options!(name, values, options, span => [
checks;
delimiters;
min_args;
max_args;
required_permissions;
Expand All @@ -191,6 +192,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
bucket,
aliases,
description,
delimiters,
usage,
example,
min_args,
Expand Down Expand Up @@ -245,6 +247,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
bucket: #bucket,
names: &[#_name, #(#aliases),*],
desc: #description,
delimiters: &[#(#delimiters),*],
usage: #usage,
example: #example,
min_args: #min_args,
Expand Down
1 change: 1 addition & 0 deletions command_attr/src/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ pub struct Options {
pub bucket: Option<String>,
pub aliases: Vec<String>,
pub description: Option<String>,
pub delimiters: Vec<String>,
pub usage: Option<String>,
pub example: Option<String>,
pub min_args: Option<u16>,
Expand Down
29 changes: 27 additions & 2 deletions src/framework/standard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,10 @@ impl Framework for StandardFramework {
}
};

let mut args = Args::new(stream.rest(), &self.config.delimiters);

match invoke {
Invoke::Help(name) => {
let args = Args::new(stream.rest(), &self.config.delimiters);

let before = self.before.clone();
let after = self.after.clone();
let owners = self.config.owners.clone();
Expand All @@ -738,6 +738,31 @@ impl Framework for StandardFramework {
});
}
Invoke::Command { command, group } => {
let mut args = {
use std::borrow::Cow;

let mut delims = Cow::Borrowed(&self.config.delimiters);

// If user has configured the command's own delimiters, use those instead.
if !command.options.delimiters.is_empty() {
// FIXME: Get rid of this allocation.
let mut v = Vec::with_capacity(command.options.delimiters.len());

for delim in command.options.delimiters {
if delim.len() == 1 {
v.push(Delimiter::Single(delim.chars().next().unwrap()));
} else {
// This too.
v.push(Delimiter::Multiple(delim.to_string()));
}
}

delims = Cow::Owned(v);
}

Args::new(stream.rest(), &delims)
};

if let Some(error) =
self.should_fail(&mut ctx, &msg, &mut args, &command.options, &group.options)
{
Expand Down
3 changes: 3 additions & 0 deletions src/framework/standard/structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct CommandOptions {
pub names: &'static [&'static str],
/// Command description, used by other commands.
pub desc: Option<&'static str>,
/// Delimiters used to split the arguments of the command by.
/// If empty, the [global delimiters](struct.Configuration.html#method.delimiters) are used.
pub delimiters: &'static [&'static str],
/// Command usage schema, used by other commands.
pub usage: Option<&'static str>,
/// Example arguments, used by other commands.
Expand Down

0 comments on commit 6f7797e

Please sign in to comment.