-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #3112 from epage/override
docs(derive): Show how to override special types
- Loading branch information
Showing
4 changed files
with
83 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
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,44 @@ | ||
*Jump to [source](custom_bool.rs)* | ||
|
||
Example of overriding the magic `bool` behavior | ||
|
||
```bash | ||
$ custom_bool --help | ||
clap [..] | ||
|
||
A simple to use, efficient, and full-featured Command Line Argument Parser | ||
|
||
USAGE: | ||
custom_bool[EXE] [OPTIONS] --foo <FOO> <BOOM> | ||
|
||
ARGS: | ||
<BOOM> | ||
|
||
OPTIONS: | ||
--bar <BAR> [default: false] | ||
--foo <FOO> | ||
-h, --help Print help information | ||
-V, --version Print version information | ||
$ custom_bool | ||
? failed | ||
error: The following required arguments were not provided: | ||
--foo <FOO> | ||
<BOOM> | ||
|
||
USAGE: | ||
custom_bool [OPTIONS] --foo <FOO> <BOOM> | ||
|
||
For more information try --help | ||
$ custom_bool --foo true false | ||
[examples/derive_ref/custom_bool.rs:31] opt = Opt { | ||
foo: true, | ||
bar: false, | ||
boom: false, | ||
} | ||
$ custom_bool --foo true --bar true false | ||
[examples/derive_ref/custom_bool.rs:31] opt = Opt { | ||
foo: true, | ||
bar: true, | ||
boom: false, | ||
} | ||
``` |
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,32 @@ | ||
use clap::Parser; | ||
|
||
#[derive(Parser, Debug, PartialEq)] | ||
#[clap(about, author, version)] | ||
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. | ||
#[clap(long, parse(try_from_str))] | ||
foo: bool, | ||
|
||
// Of course, this could be done with an explicit parser function. | ||
#[clap(long, parse(try_from_str = true_or_false), default_value_t)] | ||
bar: bool, | ||
|
||
// `bool` can be positional only with explicit `parse(...)` annotation | ||
#[clap(parse(try_from_str))] | ||
boom: bool, | ||
} | ||
|
||
fn true_or_false(s: &str) -> Result<bool, &'static str> { | ||
match s { | ||
"true" => Ok(true), | ||
"false" => Ok(false), | ||
_ => Err("expected `true` or `false`"), | ||
} | ||
} | ||
|
||
fn main() { | ||
let opt = Opt::parse(); | ||
dbg!(opt); | ||
} |