forked from pacak/bpaf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require explicit type annotation for positional, argument and any
- Loading branch information
Showing
33 changed files
with
299 additions
and
90 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
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
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 |
---|---|---|
@@ -1,18 +1,42 @@ | ||
// | ||
use std::path::PathBuf; | ||
use std::{path::PathBuf, str::FromStr}; | ||
// | ||
use bpaf::*; | ||
#[derive(Debug, Clone)] | ||
// | ||
#[allow(dead_code)] | ||
pub struct Options { | ||
coin: Coin, | ||
file: PathBuf, | ||
name: Option<String>, | ||
} | ||
|
||
/// A custom datatype that doesn't implement [`FromOsStr`] but implements [`FromStr`] | ||
#[derive(Debug, Clone, Copy)] | ||
enum Coin { | ||
Heads, | ||
Tails, | ||
} | ||
impl FromStr for Coin { | ||
type Err = String; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"heads" => Ok(Coin::Heads), | ||
"tails" => Ok(Coin::Tails), | ||
_ => Err(format!("Expected 'heads' or 'tails', got '{}'", s)), | ||
} | ||
} | ||
} | ||
|
||
pub fn options() -> OptionParser<Options> { | ||
let file = positional::<PathBuf>("FILE").help("File to use"); | ||
// sometimes you can get away with not specifying type in positional's turbofish | ||
let name = positional("NAME").help("Name to look for").optional(); | ||
construct!(Options { file, name }).to_options() | ||
let coin = long("coin") | ||
.help("Coin toss results") | ||
.argument::<FromUtf8<Coin>>("COIN") | ||
.fallback(Coin::Heads); | ||
let name = positional::<String>("NAME") | ||
.help("Name to look for") | ||
.optional(); | ||
construct!(Options { coin, file, name }).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 |
---|---|---|
@@ -1,16 +1,37 @@ | ||
// | ||
use std::path::PathBuf; | ||
use std::{path::PathBuf, str::FromStr}; | ||
// | ||
use bpaf::*; | ||
#[derive(Debug, Clone, Bpaf)] | ||
// | ||
#[allow(dead_code)] | ||
#[bpaf(options)] | ||
pub struct Options { | ||
/// Coin toss results | ||
#[bpaf(argument::<FromUtf8<Coin>>("COIN"), fallback(Coin::Heads))] | ||
coin: Coin, | ||
|
||
/// File to use | ||
#[bpaf(positional::<PathBuf>("FILE"))] | ||
file: PathBuf, | ||
/// Name to look for | ||
#[bpaf(positional("NAME"))] | ||
name: Option<String>, | ||
} | ||
|
||
/// A custom datatype that doesn't implement [`FromOsStr`] but implements [`FromStr`] | ||
#[derive(Debug, Clone, Copy)] | ||
enum Coin { | ||
Heads, | ||
Tails, | ||
} | ||
impl FromStr for Coin { | ||
type Err = String; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"heads" => Ok(Coin::Heads), | ||
"tails" => Ok(Coin::Tails), | ||
_ => Err(format!("Expected 'heads' or 'tails', got '{}'", 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
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
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
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
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
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
Oops, something went wrong.