Module bpaf::_unusual::cargohelper

source ·
Expand description

Implementing cargo commands

With cargo_helper you can use your application as cargo command

Combinatoric usage
#[derive(Debug, Clone)]
pub struct Options {
    argument: usize,
    switch: bool,
}

pub fn options() -> OptionParser<Options> {
    let argument = long("argument")
        .help("An argument")
        .argument::<usize>("ARG");
    let switch = short('s').help("A switch").switch();
    let options = construct!(Options { argument, switch });

    cargo_helper("pretty", options).to_options()
}
Derive usage
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options("pretty"))]
pub struct Options {
    /// An argument
    argument: usize,
    /// A switch
    #[bpaf(short)]
    switch: bool,
}
Examples

Let’s say the goal is to parse an argument and a switch:

% app --argument 15
Options { argument: 15, switch: false }

But when used as a cargo subcommand, cargo will also pass the command name, this example uses wrong subcommand name to bypass the helper and show how it would look without it

% app wrong --argument 15
No such command: `wrong`, did you mean `-s`?

When used with the right command - helper simply consumes it

% app pretty --argument 42 -s
Options { argument: 42, switch: true }

And it doesn’t show up in --help so not to confuse users

% app --help
Usage: --argument ARG [-s]

Available options:
        --argument <ARG>  An argument
    -s                    A switch
    -h, --help            Prints help information