-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(macros): add macro to create custom enums to use as types
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 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,39 @@ | ||
// You can use clap's value_t! macro with a custom enum by implementing the std::str::FromStr | ||
// trait which is very straight forward. There are two ways to do this, for simple enums you | ||
// can use clap's simple_enum! macro, but if you require additional functionality you can | ||
// create and implement the trait manually. | ||
// | ||
// In the following example we will create an enum with 4 values, assign a positional argument | ||
// that accepts only one of those values, and use clap to parse the argument. | ||
// | ||
// Start with bringing the trait into scope. | ||
|
||
// Add clap like normal | ||
#[macro_use] | ||
extern crate clap; | ||
|
||
use clap::{App, Arg}; | ||
|
||
// Define your enum, the simple_num! macro takes a enum name followed by => and each value | ||
// separated by a ',' | ||
simple_enum!{ Vals => Foo, Bar, Baz, Qux } | ||
|
||
fn main() { | ||
// Create the application like normal | ||
let m = App::new("myapp") | ||
// Use a single positional argument that is required | ||
.arg(Arg::from_usage("<type> 'The type to use'") | ||
// Define the list of possible values | ||
.possible_values(vec!["Foo", "Bar", "Baz", "Qux"])) | ||
.get_matches(); | ||
|
||
let t = value_t_or_exit!(m.value_of("type"), Vals); | ||
|
||
// Now we can use our enum like normal. | ||
match t { | ||
Vals::Foo => println!("Found a Foo"), | ||
Vals::Bar => println!("Found a Bar"), | ||
Vals::Baz => println!("Found a Baz"), | ||
Vals::Qux => println!("Found a Qux") | ||
} | ||
} |
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