very_custom_usage/
very_custom_usage.rs

1//! A way to customize usage for a nested command
2//!
3//! Usually you would go with generated usage or by overriding it using `usage` attribute
4//! to the top level or command level bpaf annotation. By taking advantage of command being just a
5//! set of options with it's own help message and a custom prefix you can override the usage with
6//! an arbitrary string, including one generated at runtime by doing something like this:
7
8use bpaf::*;
9
10// this defines top level set of options and refers to an external parser `cmd_usage`
11// At this point cmd_usage can be any parser that produces Cmd
12#[derive(Debug, Clone, Bpaf)]
13#[bpaf(options)]
14#[allow(dead_code)]
15enum Opts {
16    Cmd(#[bpaf(external(cmd_usage))] Cmd),
17}
18
19// bpaf defines command as something with its own help message that can be accessed with a
20// positional command name - inside of the command there is an OptionParser, this struct
21// defines the parser we are going to use later
22#[derive(Debug, Clone, Bpaf)]
23#[bpaf(options)]
24#[allow(dead_code)]
25struct Cmd {
26    opt: bool,
27}
28
29// At this point we have OptionParser<Cmd> and we want to turn that into a regular parser
30// with custom usage string - for that we are using two functions from combinatoric api:
31// `usage` and `command`
32fn cmd_usage() -> impl Parser<Cmd> {
33    cmd().usage("A very custom usage goes here").command("cmd")
34}
35
36fn main() {
37    println!("{:?}", opts().run());
38}