Skip to main content

command

Attribute Macro command 

Source
#[command]
Expand description

Defines a complete command with handler, clap Command, and template from a single source.

This macro extends #[handler] to generate both the handler wrapper AND the complete clap Command definition. This eliminates mismatches between handler expectations and CLI definitions since everything is derived from one source.

§Command Attributes

AttributeTypeRequiredDescription
namestringYesCommand name
aboutstringNoShort description
long_aboutstringNoDetailed description
visible_aliasstringNoCommand alias
hideboolNoHide from help
templatestringNoTemplate name (defaults to command name)

§Parameter Annotations

§Flags (#[flag(...)])

AttributeTypeDescription
shortcharShort flag (e.g., -a)
longstringLong flag, defaults to param name with _-
helpstringHelp text
hideboolHide from help

§Arguments (#[arg(...)])

AttributeTypeDescription
shortcharShort option (e.g., -f)
longstringLong option, defaults to param name with _-
helpstringHelp text
value_namestringPlaceholder in help
defaultstringDefault value
hideboolHide from help
positionalboolPositional argument (no -- prefix)

§Pass-through

AnnotationTypeDescription
#[ctx]&CommandContextAccess command context
#[matches]&ArgMatchesAccess raw matches

§Generated Code

For a function fn foo(...), the macro generates:

  • fn foo(...) - original function (preserved for testing)
  • fn foo__handler(...) - wrapper for dispatch
  • fn foo__expected_args() - verification metadata
  • fn foo__command() - clap Command definition
  • fn foo__template() - template name
  • struct foo_Handler - implements Handler trait

§Template Convention

The template attribute is optional. When omitted, it defaults to the command name. For example, #[command(name = "list")] will use template "list".

§Example

use standout_macros::command;

#[command(name = "list", about = "List all items")]
fn list_items(
    #[flag(short = 'a', help = "Show all")] all: bool,
    #[arg(short = 'f', help = "Filter")] filter: Option<String>,
) -> Result<Vec<Item>, Error> {
    storage::list(all, filter)
}

// Use:
// - list_items__command() returns the clap Command
// - list_items__template() returns "list"
// - list_items_Handler implements Handler