-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move (a copy of) cargo_helper into batteries
and redo the exampels
- Loading branch information
Showing
6 changed files
with
174 additions
and
56 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,25 @@ | ||
? Let's say the goal is to parse an argument and a switch: | ||
> --argument 15 | ||
OK | ||
Options { argument: 15, switch: false } | ||
|
||
? But when used as a `cargo` subcommand, cargo will also pass the command name, this example | ||
? uses _wrong_ subcommand name to bypass the helper and show how it would look without it | ||
> wrong --argument 15 | ||
Stderr | ||
No such command: `wrong`, did you mean `-s`? | ||
|
||
? When used with the right command - helper simply consumes it | ||
> pretty --argument 42 -s | ||
OK | ||
Options { argument: 42, switch: true } | ||
|
||
? And it doesn't show up in `--help` so not to confuse users | ||
> --help | ||
Stdout | ||
Usage: --argument ARG [-s] | ||
|
||
Available options: | ||
--argument <ARG> An argument | ||
-s A switch | ||
-h, --help Prints help information |
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,19 @@ | ||
// | ||
use bpaf::*; | ||
// | ||
#[allow(dead_code)] | ||
#[derive(Debug, Clone)] | ||
pub struct Options { | ||
argument: usize, | ||
switch: bool, | ||
} | ||
|
||
pub fn options() -> OptionParser<Options> { | ||
let argument = long("argument") | ||
.help("An argument") | ||
.argument::<usize>("ARG"); | ||
let switch = short('s').help("A switch").switch(); | ||
let options = construct!(Options { argument, switch }); | ||
|
||
cargo_helper("pretty", options).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,13 @@ | ||
// | ||
use bpaf::*; | ||
// | ||
#[allow(dead_code)] | ||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options("pretty"))] | ||
pub struct Options { | ||
/// An argument | ||
argument: usize, | ||
/// A switch | ||
#[bpaf(short)] | ||
switch: bool, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<details> | ||
<summary>Combinatoric usage</summary> | ||
|
||
```no_run | ||
# use bpaf::*; | ||
# #[allow(dead_code)] | ||
#[derive(Debug, Clone)] | ||
pub struct Options { | ||
argument: usize, | ||
switch: bool, | ||
} | ||
pub fn options() -> OptionParser<Options> { | ||
let argument = long("argument") | ||
.help("An argument") | ||
.argument::<usize>("ARG"); | ||
let switch = short('s').help("A switch").switch(); | ||
let options = construct!(Options { argument, switch }); | ||
cargo_helper("pretty", options).to_options() | ||
} | ||
``` | ||
|
||
</details> | ||
<details> | ||
<summary>Derive usage</summary> | ||
|
||
```no_run | ||
# use bpaf::*; | ||
# #[allow(dead_code)] | ||
#[derive(Debug, Clone, Bpaf)] | ||
#[bpaf(options("pretty"))] | ||
pub struct Options { | ||
/// An argument | ||
argument: usize, | ||
/// A switch | ||
#[bpaf(short)] | ||
switch: bool, | ||
} | ||
``` | ||
|
||
</details> | ||
<details> | ||
<summary>Examples</summary> | ||
|
||
|
||
Let's say the goal is to parse an argument and a switch: | ||
```console | ||
% app --argument 15 | ||
Options { argument: 15, switch: false } | ||
``` | ||
|
||
But when used as a `cargo` subcommand, cargo will also pass the command name, this example | ||
uses _wrong_ subcommand name to bypass the helper and show how it would look without it | ||
```console | ||
% app wrong --argument 15 | ||
No such command: `wrong`, did you mean `-s`? | ||
``` | ||
|
||
When used with the right command - helper simply consumes it | ||
```console | ||
% app pretty --argument 42 -s | ||
Options { argument: 42, switch: true } | ||
``` | ||
|
||
And it doesn't show up in `--help` so not to confuse users | ||
```console | ||
% app --help | ||
Usage: --argument ARG [-s] | ||
|
||
Available options: | ||
--argument <ARG> An argument | ||
-s A switch | ||
-h, --help Prints help information | ||
``` | ||
|
||
</details> |
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