pub struct Command<T> { /* private fields */ }
Expand description
Implementations
sourceimpl<P> Command<P>
impl<P> Command<P>
sourcepub fn help<M>(self, help: M) -> Self where
M: Into<String>,
pub fn help<M>(self, help: M) -> Self where
M: Into<String>,
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?
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
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);
}
Trait Implementations
sourceimpl<T> Parser<T> for Command<T>
impl<T> Parser<T> for Command<T>
sourcefn some(self, message: &'static str) -> ParseSome<Self> where
Self: Sized + Parser<T>,
fn some(self, message: &'static str) -> ParseSome<Self> where
Self: Sized + Parser<T>,
Consume one or more items from a command line Read more
sourcefn optional(self) -> ParseOptional<Self> where
Self: Sized + Parser<T>,
fn optional(self) -> ParseOptional<Self> where
Self: Sized + Parser<T>,
Turn a required argument into optional one Read more
sourcefn parse<F, R, E>(self, f: F) -> ParseWith<T, Self, F, E, R> where
Self: Sized + Parser<T>,
F: Fn(T) -> Result<R, E>,
E: ToString,
fn parse<F, R, E>(self, f: F) -> ParseWith<T, Self, F, E, R> where
Self: Sized + Parser<T>,
F: Fn(T) -> Result<R, E>,
E: ToString,
Apply a failing transformation to a contained value Read more
sourcefn map<F, R>(self, map: F) -> ParseMap<T, Self, F, R> where
Self: Sized + Parser<T>,
F: Fn(T) -> R + 'static,
fn map<F, R>(self, map: F) -> ParseMap<T, Self, F, R> where
Self: Sized + Parser<T>,
F: Fn(T) -> R + 'static,
Apply a pure transformation to a contained value Read more
sourcefn guard<F>(self, check: F, message: &'static str) -> ParseGuard<Self, F> where
Self: Sized + Parser<T>,
F: Fn(&T) -> bool,
fn guard<F>(self, check: F, message: &'static str) -> ParseGuard<Self, F> where
Self: Sized + Parser<T>,
F: Fn(&T) -> bool,
Validate or fail with a message Read more
sourcefn fallback(self, value: T) -> ParseFallback<Self, T> where
Self: Sized + Parser<T>,
fn fallback(self, value: T) -> ParseFallback<Self, T> where
Self: Sized + Parser<T>,
Use this value as default if value isn’t present on a command line Read more
sourcefn fallback_with<F, E>(self, fallback: F) -> ParseFallbackWith<T, Self, F, E> where
Self: Sized + Parser<T>,
F: Fn() -> Result<T, E>,
E: ToString,
fn fallback_with<F, E>(self, fallback: F) -> ParseFallbackWith<T, Self, F, E> where
Self: Sized + Parser<T>,
F: Fn() -> Result<T, E>,
E: ToString,
Use value produced by this function as default if value isn’t present Read more
sourcefn hide(self) -> ParseHide<Self> where
Self: Sized + Parser<T>,
fn hide(self) -> ParseHide<Self> where
Self: Sized + Parser<T>,
Ignore this parser during any sort of help generation Read more
sourcefn group_help(self, message: &'static str) -> ParseGroupHelp<Self> where
Self: Sized + Parser<T>,
fn group_help(self, message: &'static str) -> ParseGroupHelp<Self> where
Self: Sized + Parser<T>,
Attach help message to a complex parser Read more
sourcefn to_options(self) -> OptionParser<T> where
Self: Sized + Parser<T> + 'static,
fn to_options(self) -> OptionParser<T> where
Self: Sized + Parser<T> + 'static,
Transform Parser
into OptionParser
to attach metadata and run Read more
Auto Trait Implementations
impl<T> !RefUnwindSafe for Command<T>
impl<T> !Send for Command<T>
impl<T> !Sync for Command<T>
impl<T> Unpin for Command<T> where
T: Unpin,
impl<T> !UnwindSafe for Command<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more