pub struct ParsePositional<T> {
metavar: &'static str,
help: Option<Doc>,
position: Position,
ty: PhantomData<T>,
}Expand description
Parse a positional item, created with positional
You can add extra information to positional parsers with help,
strict, or non_strict on this struct.
Fields§
§metavar: &'static str§help: Option<Doc>§position: Position§ty: PhantomData<T>Implementations§
Source§impl<T> ParsePositional<T>
impl<T> ParsePositional<T>
Sourcepub fn help<M>(self, help: M) -> Self
pub fn help<M>(self, help: M) -> Self
Add a help message to a positional parser
bpaf converts doc comments and string into help by following those rules:
- Everything up to the first blank line is included into a “short” help message
- Everything is included into a “long” help message
bpafpreserves linebreaks followed by a line that starts with a space- Linebreaks are removed otherwise
You can pass anything that can be converted into Doc, if you are not using
documentation generation functionality (doc) this can be &str.
Combinatoric example
#[derive(Debug, Clone)]
pub struct Options {
verbose: bool,
crate_name: String,
feature_name: Option<String>,
}
pub fn options() -> OptionParser<Options> {
let verbose = short('v')
.long("verbose")
.help("Display detailed information")
.switch();
let crate_name = positional("CRATE").help("Crate name to use");
let feature_name = positional("FEATURE")
.help("Display information about this feature")
.optional();
construct!(Options {
verbose,
// You must place positional items and commands after
// all other parsers
crate_name,
feature_name
})
.to_options()
}
fn main() {
println!("{:?}", options().run())
}Derive example
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
/// Display detailed information
#[bpaf(short, long)]
verbose: bool,
// You must place positional items and commands after
// all other parsers
#[bpaf(positional("CRATE"))]
/// Crate name to use
crate_name: String,
#[bpaf(positional("FEATURE"))]
/// Display information about this feature
feature_name: Option<String>,
}
fn main() {
println!("{:?}", options().run())
}Output
Positional items show up in a separate group of arguments if they contain a help message, otherwise they will show up only in Usage part.
Usage: app [-v] CRATE [FEATURE]
- CRATE
- Crate name to use
- FEATURE
- Display information about this feature
- -v, --verbose
- Display detailed information
- -h, --help
- Prints help information
You can mix positional items with regular items
Options { verbose: true, crate_name: "bpaf", feature_name: None }
And since bpaf API expects to have non positional items consumed before positional ones - you
can use them in a different order. In this example bpaf corresponds to a crate_name field and
--verbose – to verbose.
Options { verbose: true, crate_name: "bpaf", feature_name: None }
In previous examples optional field feature was missing, this one contains it.
Options { verbose: false, crate_name: "bpaf", feature_name: Some("autocomplete") }
Users can use -- to tell bpaf to treat remaining items as positionals - this might be
required to handle unusual items.
Options { verbose: false, crate_name: "bpaf", feature_name: Some("--verbose") }
Options { verbose: false, crate_name: "bpaf", feature_name: Some("--verbose") }
Without using -- bpaf would only accept items that don’t start with - as positional.
Error: expected CRATE, got --detailed. Pass --help for usage information
Error: expected CRATE, pass --help for usage information
You can use any to work around this restriction.
Sourcepub fn strict(self) -> ParsePositional<T>
pub fn strict(self) -> ParsePositional<T>
Changes positional parser to be a “strict” positional
Usually positional items can appear anywhere on a command line:
$ ls -d bpaf
$ ls bpaf -dhere 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 -- --helphere 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.
Combinatoric example
#[derive(Debug, Clone)]
pub struct Options {
verbose: bool,
binary: String,
args: Vec<String>,
}
pub fn options() -> OptionParser<Options> {
let verbose = short('v')
.long("verbose")
.help("Produce detailed report")
.switch();
let binary = long("bin").help("Binary to execute").argument("BIN");
let args = positional("ARG")
.help("Arguments for the binary")
.strict()
.many();
construct!(Options {
verbose,
binary,
args
})
.to_options()
}
fn main() {
println!("{:?}", options().run())
}Derive example
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(short, long)]
/// Produce detailed report
verbose: bool,
#[bpaf(long("bin"), argument("BIN"))]
/// Binary to execute
binary: String,
#[bpaf(positional("ARG"), strict, many)]
/// Arguments for the binary
args: Vec<String>,
}
fn main() {
println!("{:?}", options().run())
}Output
Usage line for a cargo-run like app that takes an app name and possibly many strictly positional child arguments can look like this:
Usage: app [-v] --bin=BIN -- [ARG]...
- ARG
- Arguments for the binary
- -v, --verbose
- Produce detailed report
- --bin=BIN
- Binary to execute
- -h, --help
- Prints help information
Here any argument passed before double dash goes to the parser itself
Options { verbose: true, binary: "dd", args: [] }
Anything after it - collected into strict arguments
Options { verbose: false, binary: "dd", args: ["--verbose"] }
Sourcepub fn non_strict(self) -> Self
pub fn non_strict(self) -> Self
Changes positional parser to be a “not strict” positional
Ensures the parser always rejects “strict” positions to the right of the separator, --.
Essentially the inverse operation to ParsePositional::strict, which can be used to ensure
adjacent strict and nonstrict args never conflict with eachother.
fn meta(&self) -> Meta
Trait Implementations§
Source§impl<T: Clone> Clone for ParsePositional<T>
impl<T: Clone> Clone for ParsePositional<T>
Source§fn clone(&self) -> ParsePositional<T>
fn clone(&self) -> ParsePositional<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more