mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-18 15:20:15 +01:00
Ability to hide the command aliases from the help message
This commit is contained in:
parent
3d050eaf43
commit
ed90485fdf
4 changed files with 28 additions and 18 deletions
|
@ -63,7 +63,7 @@ fn impl_descriptions(infos: &[Command], global: &CommandEnum) -> proc_macro2::To
|
||||||
.filter(|command| command.description_is_enabled())
|
.filter(|command| command.description_is_enabled())
|
||||||
.map(|command @ Command { prefix, name, aliases, ..}| {
|
.map(|command @ Command { prefix, name, aliases, ..}| {
|
||||||
let description = command.description().unwrap_or_default();
|
let description = command.description().unwrap_or_default();
|
||||||
let aliases = aliases.clone().map(|(aliases, _)| aliases).unwrap_or_default();
|
let aliases = (!command.hidden_aliases).then(|| aliases.clone().map(|(aliases, _)| aliases).unwrap_or_default()).unwrap_or_default();
|
||||||
quote! { CommandDescription { prefix: #prefix, command: #name, description: #description, aliases: &[#(#aliases),*]} }
|
quote! { CommandDescription { prefix: #prefix, command: #name, description: #description, aliases: &[#(#aliases),*]} }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,8 @@ pub(crate) struct Command {
|
||||||
pub parser: ParserType,
|
pub parser: ParserType,
|
||||||
/// Whether the command is hidden from the help message.
|
/// Whether the command is hidden from the help message.
|
||||||
pub hidden: bool,
|
pub hidden: bool,
|
||||||
|
/// Whether the aliases of the command are hidden from the help message.
|
||||||
|
pub hidden_aliases: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
|
@ -40,6 +42,7 @@ impl Command {
|
||||||
// FIXME: error on/do not ignore command separator
|
// FIXME: error on/do not ignore command separator
|
||||||
command_separator: _,
|
command_separator: _,
|
||||||
hide,
|
hide,
|
||||||
|
hide_aliases,
|
||||||
} = attrs;
|
} = attrs;
|
||||||
|
|
||||||
let name = match (rename, rename_rule) {
|
let name = match (rename, rename_rule) {
|
||||||
|
@ -57,8 +60,9 @@ impl Command {
|
||||||
let prefix = prefix.map(|(p, _)| p).unwrap_or_else(|| global_options.prefix.clone());
|
let prefix = prefix.map(|(p, _)| p).unwrap_or_else(|| global_options.prefix.clone());
|
||||||
let parser = parser.map(|(p, _)| p).unwrap_or_else(|| global_options.parser_type.clone());
|
let parser = parser.map(|(p, _)| p).unwrap_or_else(|| global_options.parser_type.clone());
|
||||||
let hidden = hide.is_some();
|
let hidden = hide.is_some();
|
||||||
|
let hidden_aliases = hide_aliases.is_some();
|
||||||
|
|
||||||
Ok(Self { prefix, description, parser, name, aliases, hidden })
|
Ok(Self { prefix, description, parser, name, aliases, hidden, hidden_aliases })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_prefixed_command(&self) -> String {
|
pub fn get_prefixed_command(&self) -> String {
|
||||||
|
|
|
@ -25,6 +25,7 @@ pub(crate) struct CommandAttrs {
|
||||||
pub separator: Option<(String, Span)>,
|
pub separator: Option<(String, Span)>,
|
||||||
pub command_separator: Option<(String, Span)>,
|
pub command_separator: Option<(String, Span)>,
|
||||||
pub hide: Option<((), Span)>,
|
pub hide: Option<((), Span)>,
|
||||||
|
pub hide_aliases: Option<((), Span)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A single k/v attribute for `BotCommands` derive macro.
|
/// A single k/v attribute for `BotCommands` derive macro.
|
||||||
|
@ -53,6 +54,7 @@ enum CommandAttrKind {
|
||||||
Separator(String),
|
Separator(String),
|
||||||
CommandSeparator(String),
|
CommandSeparator(String),
|
||||||
Hide,
|
Hide,
|
||||||
|
HideAliases,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandAttrs {
|
impl CommandAttrs {
|
||||||
|
@ -73,6 +75,7 @@ impl CommandAttrs {
|
||||||
separator: None,
|
separator: None,
|
||||||
command_separator: None,
|
command_separator: None,
|
||||||
hide: None,
|
hide: None,
|
||||||
|
hide_aliases: None,
|
||||||
},
|
},
|
||||||
|mut this, attr| {
|
|mut this, attr| {
|
||||||
fn insert<T>(opt: &mut Option<(T, Span)>, x: T, sp: Span) -> Result<()> {
|
fn insert<T>(opt: &mut Option<(T, Span)>, x: T, sp: Span) -> Result<()> {
|
||||||
|
@ -119,6 +122,7 @@ impl CommandAttrs {
|
||||||
Separator(s) => insert(&mut this.separator, s, attr.sp),
|
Separator(s) => insert(&mut this.separator, s, attr.sp),
|
||||||
CommandSeparator(s) => insert(&mut this.command_separator, s, attr.sp),
|
CommandSeparator(s) => insert(&mut this.command_separator, s, attr.sp),
|
||||||
Hide => insert(&mut this.hide, (), attr.sp),
|
Hide => insert(&mut this.hide, (), attr.sp),
|
||||||
|
HideAliases => insert(&mut this.hide_aliases, (), attr.sp),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
Ok(this)
|
Ok(this)
|
||||||
|
@ -174,6 +178,7 @@ impl CommandAttr {
|
||||||
"separator" => Separator(value.expect_string()?),
|
"separator" => Separator(value.expect_string()?),
|
||||||
"command_separator" => CommandSeparator(value.expect_string()?),
|
"command_separator" => CommandSeparator(value.expect_string()?),
|
||||||
"hide" => value.expect_none("hide").map(|_| Hide)?,
|
"hide" => value.expect_none("hide").map(|_| Hide)?,
|
||||||
|
"hide_aliases" => value.expect_none("hide_aliases").map(|_| HideAliases)?,
|
||||||
"alias" => Aliases(vec![value.expect_string()?]),
|
"alias" => Aliases(vec![value.expect_string()?]),
|
||||||
"aliases" => Aliases(
|
"aliases" => Aliases(
|
||||||
value
|
value
|
||||||
|
|
|
@ -3,6 +3,21 @@ use crate::{
|
||||||
rename_rules::RenameRule, Result,
|
rename_rules::RenameRule, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Create a if block that checks if the given attribute is applied to a enum
|
||||||
|
/// itself, if so, return an error
|
||||||
|
macro_rules! variants_only_attr {
|
||||||
|
($($attr: ident),+) => {
|
||||||
|
$(
|
||||||
|
if let Some((_, sp)) = $attr {
|
||||||
|
return Err(compile_error_at(
|
||||||
|
concat!("`", stringify!($attr), "` attribute can only be applied to enums *variants*"),
|
||||||
|
sp,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
)+
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct CommandEnum {
|
pub(crate) struct CommandEnum {
|
||||||
pub prefix: String,
|
pub prefix: String,
|
||||||
/// The bool is true if the description contains a doc comment
|
/// The bool is true if the description contains a doc comment
|
||||||
|
@ -25,24 +40,10 @@ impl CommandEnum {
|
||||||
command_separator,
|
command_separator,
|
||||||
separator,
|
separator,
|
||||||
hide,
|
hide,
|
||||||
|
hide_aliases,
|
||||||
} = attrs;
|
} = attrs;
|
||||||
|
|
||||||
if let Some((_rename, sp)) = rename {
|
variants_only_attr![rename, hide, hide_aliases, aliases];
|
||||||
return Err(compile_error_at(
|
|
||||||
"`rename` attribute can only be applied to enums *variants*",
|
|
||||||
sp,
|
|
||||||
));
|
|
||||||
} else if let Some((_hide, sp)) = hide {
|
|
||||||
return Err(compile_error_at(
|
|
||||||
"`hide` attribute can only be applied to enums *variants*",
|
|
||||||
sp,
|
|
||||||
));
|
|
||||||
} else if let Some((_aliases, sp)) = aliases {
|
|
||||||
return Err(compile_error_at(
|
|
||||||
"`aliases` attribute can only be applied to enums *variants*",
|
|
||||||
sp,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut parser = parser.map(|(p, _)| p).unwrap_or(ParserType::Default);
|
let mut parser = parser.map(|(p, _)| p).unwrap_or(ParserType::Default);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue