Struct bpaf::params::ParsePositional
source · [−]pub struct ParsePositional<T> { /* private fields */ }
Expand description
Parse a positional item, created with positional
You can add extra information to positional parsers with help
and strict
on this struct.
Implementations
sourceimpl<T> ParsePositional<T>
impl<T> ParsePositional<T>
sourcepub fn help<M>(self, help: M) -> Selfwhere
M: Into<String>,
pub fn help<M>(self, help: M) -> Selfwhere
M: Into<String>,
Add a help message to a positional
parser
Combinatoric usage
fn parse_name() -> impl Parser<String> {
positional::<String>("NAME")
.help("a flag that does a thing")
}
Derive usage
bpaf_derive
converts doc comments into option help by following those rules:
- It skips blank lines, if present.
- It stops parsing after a double blank line.
#[derive(Debug, Clone, Bpaf)]
struct Options (
/// This line is part of help message
///
/// So is this one
///
///
/// But this one isn't
String,
);
See also NamedArg::help
Examples found in repository?
More examples
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
fn main() {
let file = positional::<OsString>("FILE")
.help("File name to concatenate, with no FILE or when FILE is -, read standard input")
.optional()
.parse::<_, Box<dyn Read>, std::io::Error>(|path| {
Ok(if let Some(path) = path {
if path == "-" {
Box::new(stdin())
} else {
Box::new(File::open(path)?)
}
} else {
Box::new(stdin())
})
})
.to_options()
.descr("Concatenate a file to standard output")
.run();
let reader = BufReader::new(file);
for line in reader.lines() {
println!("{}", line.unwrap());
}
}
sourcepub fn strict(self) -> Self
pub fn strict(self) -> Self
Changes positional parser to be “strict” positional
Usually positional items can appear anywhere on a command line:
$ ls -d bpaf
$ ls bpaf -d
here ls
takes a positional item bpaf
and a flag -d
But in some cases it might be useful to have a stricter separation between positonal items and flags, such as passing arguments to a subprocess:
$ cargo run --example basic -- --help
here cargo
takes a --help
as a positional item and passes it to the example
bpaf
allows to require user to pass --
for positional items with strict
annotation.
bpaf
would display such positional elements differently in usage line as well. If your
app requires several different strict positional elements - it’s better to place
this annotation only to the first one.
Example
Usage line for a cargo-run like app that takes an app and possibly many strictly positional child arguments can look like this:
$ app --help
Usage: [-p SPEC] [[--bin NAME] | [--example NAME]] [--release] [<BIN>] -- <CHILD_ARG>...
<skip>
Combinatoric usage
fn options() -> impl Parser<Vec<std::ffi::OsString>> {
positional::<std::ffi::OsString>("OPTS")
.strict()
.many()
}
Derive usage
Not available at the moment
Trait Implementations
sourceimpl<T: Clone> Clone for ParsePositional<T>
impl<T: Clone> Clone for ParsePositional<T>
sourcefn clone(&self) -> ParsePositional<T>
fn clone(&self) -> ParsePositional<T>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more