bpaf
Lightweight and flexible command line argument parser with derive and combinatoric style API
Derive and combinatoric API
bpaf
supports both combinatoric and derive APIs and it’s possible to mix and match both APIs
at once. Both APIs provide access to mostly the same features, some things are more convenient
to do with derive (usually less typing), some - with combinatoric (usually maximum flexibility
and reducing boilerplate structs). In most cases using just one would suffice. Whenever
possible APIs share the same keywords and overall structure. Documentation is shared and
contains examples for both combinatoric and derive style.
bpaf
supports dynamic shell completion for bash
, zsh
, fish
and elvish
.
Quick start - combinatoric and derive APIs
-
Add
bpaf
under[dependencies]
in yourCargo.toml
[] = { = "0.9", = ["derive"] }
-
Define a structure containing command line attributes and run generated function
use Bpaf; /// Accept speed and distance, print them
-
Try to run the app
% very_basic --help Accept speed and distance, print them Usage: --speed=ARG --distance=ARG Available options: --speed=ARG Speed in KPH --distance=ARG Distance in miles -h, --help Prints help information -V, --version Prints version information % very_basic --speed 100 Expected --distance ARG, pass --help for usage information % very_basic --speed 100 --distance 500 Options: SpeedAndDistance { speed: 100.0, distance: 500.0 } % very_basic --version Version: 0.9.0 (taken from Cargo.toml by default)
-
Add
bpaf
under[dependencies]
in yourCargo.toml
[] = "0.9"
-
Declare parsers for components, combine them and run it
use ;
-
Try to run the app
% very_basic --help Accept speed and distance, print them Usage: --speed=ARG --distance=ARG Available options: --speed=ARG Speed in KPH --distance=ARG Distance in miles -h, --help Prints help information -V, --version Prints version information % very_basic --speed 100 Expected --distance ARG, pass --help for usage information % very_basic --speed 100 --distance 500 Options: SpeedAndDistance { speed: 100.0, distance: 500.0 } % very_basic --version Version: 0.5.0 (taken from Cargo.toml by default)
Design goals: flexibility, reusability, correctness
Library allows to consume command line arguments by building up parsers for individual arguments and combining those primitive parsers using mostly regular Rust code plus one macro. For example, it’s possible to take a parser that requires a single floating point number and transform it to a parser that takes several of them or takes it optionally so different subcommands or binaries can share a lot of the code:
// a regular function that doesn't depend on any context, you can export it
// and share across subcommands and binaries
// this parser accepts multiple `--speed` flags from a command line when used,
// collecting results into a vector
// this parser checks if `--speed` is present and uses value of 42.0 if it's not
At any point you can apply additional validation or fallback values in terms of current parsed state of each sub-parser and you can have several stages as well:
;
The library follows the parse, don’t validate approach when possible. Usually you parse your values just once, and then get the results as a Rust struct/enum with strict types in both combinatoric and derive APIs.
Design goals: restrictions
The main restriction that the library sets is that you can’t use parsed values (but not the fact that parser succeeded or failed) to decide how to parse subsequent values. In other words the parsers don’t have the monadic strength, only the applicative one.
To give an example, you can implement this description:
Program takes one of
--stdout
or--file
flag to specify the output target, when it’s--file
program also requires-f
attribute with the filename
But not this one:
Program takes an
-o
attribute with possible values of'stdout'
and'file'
, when it’s'file'
program also requires-f
attribute with the filename
This set of restrictions allows bpaf
to extract information about the structure of the
computations to generate help, dynamic completion and overall results in less confusing endures
experience
bpaf
performs no parameter names validation, in fact having multiple parameters with the same
name is fine, and you can combine them as alternatives and performs no fallback other than
fallback
. You need to pay attention to the order of the alternatives inside the
macro: parser that consumes the left most available argument on a command line wins, if this is
the same - left most parser wins. So to parse a parameter --test
that can be both
switch
and argument
you should put the argument one first.
You must place positional
items at the end of a structure in derive API or
consume them as last arguments in derive API.
Dynamic shell completion
bpaf
implements shell completion to allow to automatically fill in not only flag and command
names, but also argument and positional item values.
-
Enable
autocomplete
feature:= { = "0.9", = ["autocomplete"] }
-
Decorate
argument
andpositional
parsers withcomplete
to autocomplete argument values -
Depending on your shell, it generates the appropriate completion file and place it to wherever your shell is going to look for it. The name of the file should correspond in some way to name of your program. Consult the manual for your shell for the location and named conventions:
-
bash
$ your_program --bpaf-complete-style-bash >> ~/.bash_completion
-
zsh: note
_
at the beginning of the filename$ your_program --bpaf-complete-style-zsh > ~/.zsh/_your_program
-
fish
$ your_program --bpaf-complete-style-fish > ~/.config/fish/completions/your_program.fish
-
elvish
$ your_program --bpaf-complete-style-elvish >> ~/.config/elvish/rc.elv
-
-
Restart your shell - you need to do it only once or optionally after
bpaf
major version upgrade: generated completion files contain only instructions how to ask your program for possible completions and don’t change even if options are different. -
Generated scripts rely on your program being accessible in
$PATH
More examples
You can find a more examples here: https://github.com/pacak/bpaf/tree/master/examples
They’re usually documented or at least contain an explanation to important bits and you can see how they work by cloning the repo and running
$ cargo run --example example_name
Testing your own parsers
You can test your own parsers to maintain compatibility or simply checking expected output with run_inner
Cargo features
-
derive
: adds a dependency onbpaf_derive
crate and reexportBpaf
derive macro. You need to enable it to use derive API. Disabled by default. -
extradocs
: used internally to include tutorials to https://docs.rs/bpaf, no reason to enable it for local development unless you want to build your own copy of the documentation (https://github.com/rust-lang/cargo/issues/8905). Disabled by default. -
batteries
: helpers implemented with publicbpaf
API. Disabled by default. -
autocomplete
: enables support for shell autocompletion. Disabled by default. -
bright-color
,dull-color
: use more colors when printing--help
and such. Enabling either color feature adds some extra dependencies and might raise MRSV. If you are planning to use this feature in a published app - it’s best to expose them as feature flags:[] = ["bpaf/bright-color"] = ["bpaf/dull-color"]
Disabled by default.
-
docgen
: generate documentation from help declaration, seeOptionParser::render_markdown
. Disabled by default.