Skip to content

Commit

Permalink
Add "true" or "false" example
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepySkeleton committed Dec 2, 2019
1 parent d1a50bf commit 3b6acb4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ How to use `#[structopt(skip)]`.
### [Aliases](subcommand_aliases.rs)

How to use aliases

### [`true` or `false`](true_or_false.rs)

How to express "`"true"` or `"false"` argument.
16 changes: 16 additions & 0 deletions examples/true_or_false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! How to express "`"true"` or `"false"` argument.
use structopt::StructOpt;

type Bool = bool;

#[derive(StructOpt, Debug)]
struct Opt {
verbose: Bool,
}

fn main() {
assert!(Opt::from_iter_safe("test", "true").is_ok());
assert!(Opt::from_iter_safe("test", "false").is_ok());
assert!(Opt::from_iter_safe("test", "beauty").is_err()); // no beauty, only truth and falseness
}
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,17 @@
//! If you would like to use a custom string parser other than `FromStr`, see
//! the [same titled section](#custom-string-parsers) below.
//!
//! **Note:**
//! _________________
//! Pay attention that *only literal occurrence* of this types is special, for example
//! `Option<T>` is special while `::std::option::Option<T>` is not.
//!
//! If you need to avoid special casing you can make a `type` alias and
//! use it in place of the said type, see
//! [this](https://github.com/TeXitoi/structopt/tree/master/examples/true_or_false.rs)
//! for example.
//! _________________
//!
//! Thus, the `speed` argument is generated as:
//!
//! ```
Expand Down

0 comments on commit 3b6acb4

Please sign in to comment.