Module _7_commands

Source
Available on crate feature extradocs only.
Expand description

 

← Making nested parsers

↑ Derive API tutorial ↑

Making a cargo command →

§Parsing subcommands

The easiest way to define a group of subcommands is to have them inside the same enum with variant constructors annotated with #[bpaf(command("name"))] with or without the name

#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub enum Options {
    #[bpaf(command("run"))]
    /// Run a binary
    Run {
        /// Name of a binary crate
        name: String,
    },

    /// Run a self test
    #[bpaf(command)]
    Test,
}

fn main() {
    println!("{:?}", options().run())
}
Output

Help message lists subcommand

$ app --help

Usage: app COMMAND ...

Available options:
-h, --help
Prints help information

Available commands:
run
Run a binary
test
Run a self test

Commands have their own arguments

$ app run --name Bob
Run { name: "Bob" }
$ app test
Test
$ app test --name bob
Error: --name is not expected in this context

 

← Making nested parsers

↑ Derive API tutorial ↑

Making a cargo command →