Function bpaf::command

source · []
pub fn command<T, M>(
    name: &'static str,
    help: Option<M>,
    subparser: OptionParser<T>
) -> Parser<T> where
    T: 'static,
    M: Into<String>, 
Expand description

Subcommand parser

// Define a parser to use in a subcommand in a usual way.
// This parser accepts a single --workspace switch
let ws = long("workspace").help("Check all packages in the workspace").switch();
let decorated: OptionParser<bool> = Info::default()
    .descr("Check a package for errors")
    .for_parser(ws);

// Convert subparser into a parser.
// Note description "Check a package for errors" is specified twice:
// - Parser uses version from `descr` when user calls `% prog check --help`,
// - Parser uses version from `command` user calls `% prog --help` along
//   with descriptions for other commands if present.
let check: Parser<bool> = command("check", Some("Check a local package for errors"), decorated);

// when ther's several commands it can be a good idea to wrap each into a enum either before
// or after converting it into subparser:
#[derive(Clone, Debug)]
enum Command {
    Check(bool)
}
let check: Parser<Command> = check.map(Command::Check);

// at this point command line accepts following commands:
// `% prog --help`            - display a global help and exit
// `% prog check --help`      - display help specific to check subcommand and exit
// `% prog check`             - produce `Command::Check(false)`
// `% prog check --workspace` - produce `Command::Check(true)`
let opt = Info::default().for_parser(check);