-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
256 additions
and
377 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
It is also possible to make dynamic choice about the parsers. This example defines two parsers | ||
for distance - imperial and metric and picks one from some source available at runtime only. | ||
|
||
Help message will contain only one parser | ||
|
||
> --help | ||
and only one parser will produce a result | ||
|
||
> --distance 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
use bpaf::*; | ||
|
||
pub fn options() -> OptionParser<f64> { | ||
let miles = long("distance") | ||
.help("distance in miles") | ||
.argument::<f64>("MILES") | ||
.map(|d| d * 1.609344); | ||
|
||
let km = long("distance") | ||
.help("distance in km") | ||
.argument::<f64>("KM"); | ||
|
||
// suppose this is reading from config fule | ||
let use_metric = true; | ||
|
||
// without use of `boxed` here branches have different types so it won't typecheck | ||
// boxed make it so branches have the same type as long as they return the same type | ||
let distance = if use_metric { | ||
km.boxed() | ||
} else { | ||
miles.boxed() | ||
}; | ||
|
||
distance.to_options() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
If `bpaf` can parse first positional argument as number - it becomes a numeric prefix | ||
|
||
> 10 eat | ||
Otherwise it gets ignored | ||
|
||
> "just eat" | ||
|
||
If validation passes but second argument is missing - in this example there's no fallback | ||
|
||
> 10 | ||
Help should show that the prefix is optional | ||
|
||
> --help |
6 changes: 5 additions & 1 deletion
6
documentation/_documentation/_2_howto/_07_skip_positional/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
#### Skipping optional positional items if parsing or validation fails | ||
|
||
#![cfg_attr(not(doctest), doc = include_str!("docs/numeric_prefix.md"))] | ||
Combinations like [`Parser::optional`] and | ||
[`ParseOptional::catch`](crate::parsers::ParseOptional::catch) allow to try to parse something | ||
and then handle the error as if pase attempt never existed | ||
|
||
#![cfg_attr(not(doctest), doc = include_str!("docs2/numeric_prefix.md"))] |
5 changes: 3 additions & 2 deletions
5
documentation/_documentation/_2_howto/_08_cargo_helper/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#### Implementing cargo commands | ||
|
||
With [`cargo_helper`](crate::batteries::cargo_helper) you can use your application as a `cargo` command | ||
With [`cargo_helper`](crate::batteries::cargo_helper) you can use your application as a `cargo` command. | ||
You will need to enable `batteries` feature while importing `bpaf`. | ||
|
||
#![cfg_attr(not(doctest), doc = include_str!("docs/cargo_helper.md"))] | ||
#![cfg_attr(not(doctest), doc = include_str!("docs2/cargo_helper.md"))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.