Trait clap::Subcommand

source ·
pub trait Subcommand: FromArgMatches + Sized {
    // Required methods
    fn augment_subcommands(cmd: Command<'_>) -> Command<'_>;
    fn augment_subcommands_for_update(cmd: Command<'_>) -> Command<'_>;
    fn has_subcommand(name: &str) -> bool;
}
Expand description

Parse a sub-command into a user-defined enum.

Implementing this trait lets a parent container delegate subcommand behavior to Self. with:

  • #[clap(subcommand)] field: SubCmd: Attribute can be used with either struct fields or enum variants that impl Subcommand.
  • #[clap(flatten)] Variant(SubCmd): Attribute can only be used with enum variants that impl Subcommand.

See the derive reference for attributes and best practices.

NOTE: Deriving requires the [derive feature flag][crate::_features]

§Example

#[derive(clap::Parser)]
struct Args {
   #[clap(subcommand)]
   action: Action,
}

#[derive(clap::Subcommand)]
enum Action {
   Add,
   Remove,
}

Required Methods§

source

fn augment_subcommands(cmd: Command<'_>) -> Command<'_>

Append to Command so it can instantiate Self.

See also CommandFactory.

source

fn augment_subcommands_for_update(cmd: Command<'_>) -> Command<'_>

Append to Command so it can update self.

This is used to implement #[clap(flatten)]

See also CommandFactory.

source

fn has_subcommand(name: &str) -> bool

Test whether Self can parse a specific subcommand

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T: Subcommand> Subcommand for Box<T>

Implementors§