#[command(hide)] to hide a command from the help message

This commit is contained in:
TheAwiteb 2023-03-02 20:18:28 +03:00
parent 430175278d
commit 7c23bfc546
No known key found for this signature in database
GPG key ID: ABF818BD15DC2D34
3 changed files with 18 additions and 4 deletions

View file

@ -12,6 +12,8 @@ pub(crate) struct Command {
pub name: String, pub name: String,
/// Parser for arguments of this command. /// Parser for arguments of this command.
pub parser: ParserType, pub parser: ParserType,
/// Whether the command is hidden from the help message.
pub hidden: bool,
} }
impl Command { impl Command {
@ -29,6 +31,7 @@ impl Command {
parser, parser,
// FIXME: error on/do not ignore separator // FIXME: error on/do not ignore separator
separator: _, separator: _,
hide,
} = attrs; } = attrs;
let name = match (rename, rename_rule) { let name = match (rename, rename_rule) {
@ -47,7 +50,7 @@ impl Command {
let description = description.map(|(d, _)| d); let description = description.map(|(d, _)| d);
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());
Ok(Self { prefix, description, parser, name }) Ok(Self { prefix, description, parser, name, hidden: hide.is_some() })
} }
pub fn get_prefixed_command(&self) -> String { pub fn get_prefixed_command(&self) -> String {
@ -56,6 +59,6 @@ impl Command {
} }
pub(crate) fn description_is_enabled(&self) -> bool { pub(crate) fn description_is_enabled(&self) -> bool {
self.description != Some("off".to_owned()) self.description != Some("off".to_owned()) && !self.hidden
} }
} }

View file

@ -17,6 +17,7 @@ pub(crate) struct CommandAttrs {
pub rename: Option<(String, Span)>, pub rename: Option<(String, Span)>,
pub parser: Option<(ParserType, Span)>, pub parser: Option<(ParserType, Span)>,
pub separator: Option<(String, Span)>, pub separator: Option<(String, Span)>,
pub hide: Option<((), Span)>,
} }
/// A single k/v attribute for `BotCommands` derive macro. /// A single k/v attribute for `BotCommands` derive macro.
@ -41,6 +42,7 @@ enum CommandAttrKind {
Rename(String), Rename(String),
ParseWith(ParserType), ParseWith(ParserType),
Separator(String), Separator(String),
Hide,
} }
impl CommandAttrs { impl CommandAttrs {
@ -58,6 +60,7 @@ impl CommandAttrs {
rename: None, rename: None,
parser: None, parser: None,
separator: None, separator: None,
hide: 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<()> {
@ -77,6 +80,7 @@ impl CommandAttrs {
Rename(r) => insert(&mut this.rename, r, attr.sp), Rename(r) => insert(&mut this.rename, r, attr.sp),
ParseWith(p) => insert(&mut this.parser, p, attr.sp), ParseWith(p) => insert(&mut this.parser, p, attr.sp),
Separator(s) => insert(&mut this.separator, s, attr.sp), Separator(s) => insert(&mut this.separator, s, attr.sp),
Hide => insert(&mut this.hide, (), attr.sp),
}?; }?;
Ok(this) Ok(this)
@ -100,10 +104,11 @@ impl CommandAttr {
"rename" => Rename(value.expect_string()?), "rename" => Rename(value.expect_string()?),
"parse_with" => ParseWith(ParserType::parse(value)?), "parse_with" => ParseWith(ParserType::parse(value)?),
"separator" => Separator(value.expect_string()?), "separator" => Separator(value.expect_string()?),
"hide" => Hide,
_ => { _ => {
return Err(compile_error_at( return Err(compile_error_at(
"unexpected attribute name (expected one of `prefix`, `description`, \ "unexpected attribute name (expected one of `prefix`, `description`, \
`rename`, `parse_with` and `separator`", `rename`, `parse_with`, `separator` and `hide`",
key.span(), key.span(),
)) ))
} }

View file

@ -13,13 +13,19 @@ pub(crate) struct CommandEnum {
impl CommandEnum { impl CommandEnum {
pub fn from_attributes(attributes: &[syn::Attribute]) -> Result<Self> { pub fn from_attributes(attributes: &[syn::Attribute]) -> Result<Self> {
let attrs = CommandAttrs::from_attributes(attributes)?; let attrs = CommandAttrs::from_attributes(attributes)?;
let CommandAttrs { prefix, description, rename_rule, rename, parser, separator } = attrs; let CommandAttrs { prefix, description, rename_rule, rename, parser, separator, hide } =
attrs;
if let Some((_rename, sp)) = rename { if let Some((_rename, sp)) = rename {
return Err(compile_error_at( return Err(compile_error_at(
"`rename` attribute can only be applied to enums *variants*", "`rename` attribute can only be applied to enums *variants*",
sp, sp,
)); ));
} else if let Some((_hide, sp)) = hide {
return Err(compile_error_at(
"`hide` 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);