pub struct Command<T> { /* private fields */ }
Expand description

Builder structure for the command

Created with command, implements parser for the inner structure, gives access to help.

Implementations

Add a brief description to a command

bpaf uses this description along with the command name in help output so it shouldn’t exceed one or two lines. If help isn’t specified bpaf falls back to descr from the inner parser.

Combinatoric usage
fn inner() -> OptionParser<bool> {
    short('i')
        .help("Mysterious inner switch")
        .switch()
        .to_options()
        .descr("performs an operation")
}

fn mysterious_parser() -> impl Parser<bool> {
    command("mystery", inner())
        .help("This command performs a mystery operation")
}
Derive usage

bpaf_derive uses doc comments for inner parser, no specific options are available. See descr for more details

/// This command performs a mystery operation
#[derive(Debug, Clone, Bpaf)]
#[bpaf(command)]
struct Mystery {
    #[bpaf(short)]
    /// Mysterious inner switch
    inner: bool,
}
Example
$ app --help
    <skip>
Available commands:
    mystery  This command performs a mystery operation
Examples found in repository?
examples/enum_tuple.rs (line 27)
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    let bar = short('b')
        .long("bar")
        .help("some bar command")
        .argument("BAR")
        .optional();

    let bar_cmd = construct!(Foo { bar })
        .to_options()
        .descr("This command will try to do foo given a bar argument");

    let opt = command("foo", bar_cmd)
        .help("command for doing foo")
        .map(Command::Foo)
        .to_options()
        .run();

    println!("{:#?}", opt);
}
More examples
Hide additional examples
examples/git.rs (line 49)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let dry_run = long("dry_run").switch();
    let all = long("all").switch();
    let repository = positional("SRC").fallback("origin".to_string());
    let fetch = construct!(Opt::Fetch {
        dry_run,
        all,
        repository
    })
    .to_options()
    .descr("fetches branches from remote repository");

    let fetch_cmd = command("fetch", fetch);

    let interactive = short('i').switch();
    let all = long("all").switch();
    let files = positional("FILE").many();
    let add = construct!(Opt::Add {
        interactive,
        all,
        files
    })
    .to_options()
    .descr("add files to the staging area");

    let add_cmd = command("add", add).help("add files to the staging area");

    let opt = construct!([fetch_cmd, add_cmd])
        .to_options()
        .descr("The stupid content tracker")
        .run();

    println!("{:?}", opt);
}

Add a custom short alias for a command

Behavior is similar to short, only first short name is visible.

Add a custom hidden long alias for a command

Behavior is similar to long, but since you had to specify the first long name when making the command - this one becomes a hidden alias.

Trait Implementations

Consume zero or more items from a command line and collect them into Vec Read more

Consume one or more items from a command line Read more

Turn a required argument into optional one Read more

Apply a failing transformation to a contained value Read more

Apply a pure transformation to a contained value Read more

Parse stored String using FromStr instance Read more

Validate or fail with a message Read more

Use this value as default if value isn’t present on a command line Read more

Use value produced by this function as default if value isn’t present Read more

Ignore this parser during any sort of help generation Read more

Attach help message to a complex parser Read more

Transform Parser into OptionParser to attach metadata and run Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.