pub fn pure<T>(val: T) -> ParsePure<T>
Expand description
Parser that produces a fixed value
This parser produces T
without consuming anything from the command line, which can be useful
with construct!
. As with any parsers, T
should be Clone
and Debug
.
Both pure
and pure_with
are designed to put values into structures, to generate fallback
you should be using fallback
and fallback_with
.
See also pure_with
for a pure computation that can fail.
Combinatoric example
#[derive(Debug, Clone)]
pub struct Options {
name: String,
money: u32,
}
pub fn options() -> OptionParser<Options> {
// User can customise a name
let name = long("name").help("Use a custom user name").argument("NAME");
// but not starting amount of money
let money = pure(330);
construct!(Options { name, money }).to_options()
}
fn main() {
println!("{:?}", options().run())
}
Derive example
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(argument("NAME"))]
/// Use a custom user name
name: String,
#[bpaf(pure(330))]
money: u32,
}
fn main() {
println!("{:?}", options().run())
}
Output
pure
does not show up in --help
message
$ app --help
Usage: app --name=NAME
Available options:
- --name=NAME
- Use a custom user name
- -h, --help
- Prints help information
And there’s no way to alter the value from the command line
$ app --name Bob
Options { name: "Bob", money: 330 }
Options { name: "Bob", money: 330 }
Any attempts to do so would result in an error :)
$ app --money 100000 --name Hackerman
Error: --money is not expected in this context
Error: --money is not expected in this context
Examples found in repository?
examples/dynamic-tree.rs (line 67)
61fn make_parser(item: &Cog) -> Box<dyn Parser<&'static str>> {
62 match item {
63 Cog::Command {
64 help,
65 name,
66 operation,
67 } => Box::new(pure(*operation).to_options().descr(*help).command(name)),
68 Cog::Group { name, help, nested } => {
69 let nested = nested.iter().map(make_parser).collect::<Vec<_>>();
70 let inner = choose(nested);
71 inner.to_options().descr(*help).command(name).boxed()
72 }
73 }
74}
More examples
examples/dynamic.rs (line 59)
52fn main() {
53 let items = &[
54 ("banana", Ty::Bool),
55 ("width", Ty::Number),
56 ("name", Ty::String),
57 ];
58
59 let mut parser = pure(Vec::<(String, Value)>::new()).boxed();
60 for (name, ty) in items {
61 parser = cons(
62 parser,
63 match ty {
64 Ty::Bool => bool(name).boxed(),
65 Ty::Number => number(name).boxed(),
66 Ty::String => string(name).boxed(),
67 },
68 )
69 }
70
71 let options = parser.run();
72 println!("{:?}", options);
73}
examples/confusing.rs (line 37)
30fn main() {
31 let token = long("token")
32 .help("Token used for complex commands")
33 .argument::<String>("TOKEN")
34 .optional();
35
36 // start with defining 3 commands: simple, complex1 and complex2
37 let simple_parser = pure(PreCommand::Simple).to_options();
38 let simple = simple_parser.command("simple");
39
40 let complex1_parser = positional::<i32>("ARG");
41 let complex1 = construct!(PreCommand::Complex1(complex1_parser))
42 .to_options()
43 .descr("This is complex command 1")
44 .command("complex1");
45
46 let complex2_parser = positional::<i16>("ARG");
47
48 let complex2 = construct!(PreCommand::Complex2(complex2_parser))
49 .to_options()
50 .descr("This is complex command 2")
51 .command("complex2");
52
53 // compose then to accept any of those
54 let preparser = construct!([simple, complex1, complex2]);
55
56 // make a parser that accepts optional token and one of incomplete commands
57 // then create complete command or fail
58 let parser = construct!(token, preparser).parse(|(token, cmd)| match cmd {
59 PreCommand::Simple => Ok(Command::Simple),
60 PreCommand::Complex1(a) => match token {
61 Some(token) => Ok(Command::Complex1(token, a)),
62 None => Err("You must specify token to use with --token"),
63 },
64 PreCommand::Complex2(a) => match token {
65 Some(token) => Ok(Command::Complex2(token, a)),
66 None => Err("You must specify token to use with --token"),
67 },
68 });
69
70 let cmd = parser.to_options().run();
71 println!("{:?}", cmd);
72}