-
Notifications
You must be signed in to change notification settings - Fork 152
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
1 parent
d0cf35c
commit 1f7bf22
Showing
6 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! How to parse `--foo=true --bar=false` and turn them into bool. | ||
use structopt::StructOpt; | ||
|
||
fn true_or_false(s: &str) -> Result<bool, &'static str> { | ||
match s { | ||
"true" => Ok(true), | ||
"false" => Ok(false), | ||
_ => Err("expected `true` or `false`"), | ||
} | ||
} | ||
|
||
#[derive(StructOpt, Debug, PartialEq)] | ||
struct Opt { | ||
// Default parser for `try_from_str` is FromStr::from_str. | ||
// `impl FromStr for bool` parses `true` or `false` so this | ||
// works as expected. | ||
#[structopt(long, parse(try_from_str))] | ||
foo: bool, | ||
|
||
// Of course, this could be done with an explicit parser function. | ||
#[structopt(long, parse(try_from_str = true_or_false))] | ||
bar: bool, | ||
|
||
// `bool` can be positional only with explicit `parse(...)` annotation | ||
#[structopt(long, parse(try_from_str))] | ||
boom: bool, | ||
} | ||
|
||
fn main() { | ||
assert_eq!( | ||
Opt::from_iter(&["test", "--foo=true", "--bar=false", "true"]), | ||
Opt { | ||
foo: true, | ||
bar: false, | ||
boom: true | ||
} | ||
); | ||
// no beauty, only truth and falseness | ||
assert!(Opt::from_iter_safe(&["test", "--foo=beauty"]).is_err()); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use structopt::StructOpt; | ||
|
||
#[derive(StructOpt, Debug)] | ||
struct Opt { | ||
verbose: bool, | ||
} | ||
|
||
fn main() { | ||
Opt::from_args(); | ||
} |
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 @@ | ||
error: `bool` cannot be used as positional parameter with default parser | ||
|
||
= help: if you want to create a flag add `long` or `short` | ||
= help: If you really want a boolean parameter add an explicit parser, for example `parse(try_from_str)` | ||
= note: see also https://github.com/TeXitoi/structopt/tree/master/examples/true_or_false.rs | ||
|
||
--> $DIR/positional_bool.rs:5:14 | ||
| | ||
5 | verbose: bool, | ||
| ^^^^ |