Skip to main content

Command

Struct Command 

Source
pub struct Command {
Show 17 fields pub canonical: String, pub aliases: Vec<String>, pub spellings: Vec<String>, pub summary: String, pub description: String, pub arguments: Vec<Argument>, pub flags: Vec<Flag>, pub examples: Vec<Example>, pub subcommands: Vec<Command>, pub best_practices: Vec<String>, pub anti_patterns: Vec<String>, pub semantic_aliases: Vec<String>, pub handler: Option<HandlerFn>, pub async_handler: Option<AsyncHandlerFn>, pub extra: HashMap<String, Value>, pub exclusive_groups: Vec<Vec<String>>, pub mutating: bool,
}
Expand description

A command in the registry, potentially with subcommands.

Commands are the central unit of argot. Each command has a canonical name, optional aliases and alternate spellings, human-readable documentation, typed positional arguments, named flags, usage examples, and an optional handler closure. Commands can be nested arbitrarily deep via Command::subcommands.

Use Command::builder to construct instances — direct struct construction is intentionally not exposed.

§Serialization

Command implements serde::Serialize / Deserialize. The handler field is skipped during serialization (it cannot be represented in JSON) and will be None after deserialization.

§Examples

let cmd = Command::builder("deploy")
    .alias("d")
    .summary("Deploy the app")
    .description("Deploys to the specified environment.")
    .argument(
        Argument::builder("env")
            .description("Target environment")
            .required()
            .build()
            .unwrap(),
    )
    .flag(
        Flag::builder("dry-run")
            .short('n')
            .description("Simulate only")
            .build()
            .unwrap(),
    )
    .example(Example::new("deploy to prod", "myapp deploy production"))
    .build()
    .unwrap();

assert_eq!(cmd.canonical, "deploy");
assert_eq!(cmd.aliases, vec!["d"]);

Fields§

§canonical: String

The primary, canonical name used to invoke this command.

§aliases: Vec<String>

Alternative names that resolve to this command (e.g. "ls" for "list").

§spellings: Vec<String>

Alternate capitalizations or spellings (e.g. "LIST" for "list").

Spellings differ from aliases semantically: they represent the same word written differently rather than a short-form abbreviation.

§summary: String

One-line description shown in command listings.

§description: String

Full prose description shown in detailed help output.

§arguments: Vec<Argument>

Ordered list of positional arguments accepted by this command.

§flags: Vec<Flag>

Named flags (long and/or short) accepted by this command.

§examples: Vec<Example>

Usage examples shown in help and Markdown documentation.

§subcommands: Vec<Command>

Nested sub-commands (e.g. remote add, remote remove).

§best_practices: Vec<String>

Prose tips about correct usage, surfaced to AI agents.

§anti_patterns: Vec<String>

Prose warnings about incorrect usage, surfaced to AI agents.

§semantic_aliases: Vec<String>

Natural-language phrases describing what this command does.

Used for intent-based discovery (e.g. crate::query::Registry::match_intent) but intentionally excluded from normal help output.

§handler: Option<HandlerFn>

Optional runtime handler invoked by crate::cli::Cli::run.

Skipped during JSON serialization/deserialization.

§async_handler: Option<AsyncHandlerFn>

Optional async runtime handler (feature: async).

Skipped during JSON serialization.

§extra: HashMap<String, Value>

Arbitrary application-defined metadata.

Use this to attach structured data that is not covered by the built-in fields (e.g., permission requirements, category tags, telemetry labels).

Serialized to JSON as an object; absent from output when empty.

§exclusive_groups: Vec<Vec<String>>

Groups of mutually exclusive flag names.

At most one flag in each group may be provided in a single invocation. Validated at build time (flags must exist) and enforced at parse time.

§mutating: bool

Whether this command mutates state (creates, updates, or deletes resources).

Set to true for commands that are not safe to run freely without a --dry-run preview first. AI agents use this field to decide whether they need explicit user confirmation before dispatching.

Use CommandBuilder::mutating to set this field.

§Examples

let cmd = Command::builder("delete")
    .summary("Delete a resource")
    .mutating()
    .build()
    .unwrap();

assert!(cmd.mutating);

Implementations§

Source§

impl Command

Source

pub fn builder(canonical: impl Into<String>) -> CommandBuilder

Create a new CommandBuilder with the given canonical name.

§Arguments
  • canonical — The primary command name. Must be non-empty after trimming (enforced by CommandBuilder::build).
§Examples
let cmd = Command::builder("list").build().unwrap();
assert_eq!(cmd.canonical, "list");

Trait Implementations§

Source§

impl Clone for Command

Source§

fn clone(&self) -> Command

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Command

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Command

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for Command

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Command

Commands are ordered by canonical name, then by their full field contents.

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Command

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Command

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Command

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Command

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,