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§

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:

  1. It skips blank lines, if present.
  2. 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?
examples/numeric_prefix.rs (line 12)
10
11
12
13
14
15
16
17
18
pub fn options() -> OptionParser<Options> {
    let prefix = positional::<usize>("PREFIX")
        .help("Optional numeric command prefix")
        .optional()
        .catch();
    let command = positional::<String>("COMMAND").help("Required command name");

    construct!(Options { prefix, command }).to_options()
}
More examples
Hide additional examples
examples/cat.rs (line 17)
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());
    }
}

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Consume zero or more items from a command line and collect them into Vec Read more
Consume one or more items from a command line Read more
Turn a required argument into optional one Read more
Apply a failing transformation to a contained value Read more
Apply a pure transformation to a contained value Read more
Validate or fail with a message Read more
Use this value as default if value isn’t present on a command line Read more
Use value produced by this function as default if value isn’t present Read more
Ignore this parser during any sort of help generation Read more
Ignore this parser when generating usage line Read more
Attach help message to a complex parser Read more
Dynamic shell completion Read more
Static shell completion Read more
Add extra annotations to completion information Read more
Automagically restrict the inner parser scope to accept adjacent values only Read more
Parse anywhere Read more
Transform Parser into OptionParser to attach metadata and run Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.