-
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.
Merge pull request #246 from pacak/docs
More docs
- Loading branch information
Showing
34 changed files
with
1,264 additions
and
691 deletions.
There are no files selected for viewing
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 @@ | ||
Help message lists subcommand | ||
> --help | ||
Commands have their own arguments | ||
|
||
> run --name Bob | ||
> test | ||
> test --name bob |
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 @@ | ||
// | ||
use bpaf::*; | ||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub enum Options { | ||
#[bpaf(command("run"))] | ||
/// Run a binary | ||
Run { | ||
/// Name of a binary crate | ||
name: String, | ||
}, | ||
|
||
/// Run a self test | ||
#[bpaf(command)] | ||
Test, | ||
} |
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,11 @@ | ||
`bpaf` generates help message with a short name only as described | ||
|
||
> --help | ||
And accepts the short name only | ||
|
||
> -s 42 | ||
long name is missing | ||
|
||
> --switch 42 |
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,13 @@ | ||
// | ||
use bpaf::*; | ||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub struct Options { | ||
/// A custom switch | ||
#[bpaf(short, switch)] | ||
switch: bool, | ||
|
||
/// Custom number | ||
#[bpaf(positional("NUM"))] | ||
argument: usize, | ||
} |
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 @@ | ||
`bpaf` uses custom names in help message | ||
|
||
> --help | ||
As well as accepts them on a command line and uses in error message | ||
|
||
> --switch | ||
> -A 42 -s | ||
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,13 @@ | ||
// | ||
use bpaf::*; | ||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub struct Options { | ||
/// A custom switch | ||
#[bpaf(short, long)] | ||
switch: bool, | ||
|
||
/// A custom argument | ||
#[bpaf(long("my-argument"), short('A'))] | ||
argument: usize, | ||
} |
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,18 @@ | ||
Help message reflects mutually exclusive parts | ||
|
||
> --help | ||
At least one branch needs to succeed | ||
|
||
> | ||
And in this example only one branch can succeed | ||
|
||
> --name Cargo.toml | ||
> --url https://crates.io --auth-method digest | ||
While both branches can succeed at once - only one will actually succeed and afetr that | ||
parsing fails since there are unconsumed items | ||
|
||
> --url https://crates.io --auth-method digest --name Cargo.toml |
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,17 @@ | ||
// | ||
use bpaf::*; | ||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub enum Options { | ||
File { | ||
/// Read input from a file | ||
name: String, | ||
}, | ||
|
||
Url { | ||
/// Read input from URL | ||
url: String, | ||
/// Authentication method to use for the URL | ||
auth_method: String, | ||
}, | ||
} |
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,12 @@ | ||
bpaf generates a help message | ||
|
||
> --help | ||
And two parsers. Numeric argument is required, boolean switch is optional and fall back value | ||
is false. | ||
|
||
> --switch | ||
> --switch --argument 42 | ||
> --argument 42 |
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,11 @@ | ||
use bpaf::*; | ||
|
||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub struct Options { | ||
/// A custom switch | ||
switch: bool, | ||
|
||
/// A custom argument | ||
argument: usize, | ||
} |
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 @@ | ||
Help message lists all possible options | ||
|
||
> --help | ||
Parser accepts one and only one value from enum in this example | ||
|
||
> --input Cargo.toml --html | ||
> --input Cargo.toml --manpage | ||
> --input hello |
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,20 @@ | ||
// | ||
use bpaf::*; | ||
#[derive(Debug, Clone, Bpaf)] | ||
pub enum Format { | ||
/// Produce output in HTML format | ||
Html, | ||
/// Produce output in Markdown format | ||
Markdown, | ||
/// Produce output in manpage format | ||
Manpage, | ||
} | ||
|
||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub struct Options { | ||
/// File to process | ||
input: String, | ||
#[bpaf(external(format))] | ||
format: Format, | ||
} |
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,11 @@ | ||
Help as usual | ||
|
||
> --help | ||
And parsed values are differnt from what user passes | ||
|
||
> --width 10 --height 3 | ||
Additionally height cannot exceed 10 | ||
|
||
> --width 555 --height 42 |
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,17 @@ | ||
// | ||
use bpaf::*; | ||
fn small(size: &usize) -> bool { | ||
*size < 10 | ||
} | ||
|
||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub struct Options { | ||
// double the width | ||
#[bpaf(short, argument::<usize>("PX"), map(|w| w*2))] | ||
width: usize, | ||
|
||
// make sure the hight is below 10 | ||
#[bpaf(argument::<usize>("LENGTH"), guard(small, "must be less than 10"))] | ||
height: usize, | ||
} |
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 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 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
24 changes: 2 additions & 22 deletions
24
documentation/_documentation/_1_tutorials/_2_derive_api/_3_postpr/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,28 +1,8 @@ | ||
#### Applying transformations to parsed values | ||
#### Transforming parsed values | ||
|
||
Once field have a consumer you can start applying transformations from [`Parser`] trait. | ||
Annotation share the same names and follow the same composition rules as in Combinatoric API. | ||
|
||
```no_run | ||
# use bpaf::*; | ||
fn small(size: &usize) -> bool { | ||
*size < 10 | ||
} | ||
|
||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options)] | ||
pub struct Options { | ||
// double the width | ||
#[bpaf(short, argument::<usize>("PX"), map(|w| w*2))] | ||
width: usize, | ||
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_postpr.md"))] | ||
|
||
// make sure the hight is below 10 | ||
#[bpaf(argument::<usize>("LENGTH"), guard(small, "must be less than 10"))] | ||
height: usize, | ||
} | ||
fn main() { | ||
let opts = options().run(); | ||
println!("{:?}", opts); | ||
} | ||
``` |
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 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
Oops, something went wrong.