-
Notifications
You must be signed in to change notification settings - Fork 18
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 #84 from jhilmer/allow-multiple-flags
add support for repeated flags
- Loading branch information
Showing
3 changed files
with
272 additions
and
12 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,55 @@ | ||
use seahorse::{App, Context, Flag, FlagType}; | ||
use std::env; | ||
|
||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
let name = "multiple_flags"; | ||
|
||
let app = App::new(name) | ||
.author(env!("CARGO_PKG_AUTHORS")) | ||
.description(env!("CARGO_PKG_DESCRIPTION")) | ||
.usage("multiple_flags [args]") | ||
.version(env!("CARGO_PKG_VERSION")) | ||
.action(action) | ||
.flag( | ||
Flag::new("verbose", FlagType::Bool) | ||
.description("Increase verbosity level by repeat --verbose(-v) multiple times") | ||
.alias("v") | ||
.multiple(), | ||
) | ||
.flag( | ||
Flag::new("header", FlagType::String) | ||
.description("Set header of the request, argument can be repeated") | ||
.alias("H") | ||
.multiple(), | ||
) | ||
.flag( | ||
Flag::new("offset", FlagType::Uint) | ||
.description("Counter offset, argument can be repeated") | ||
.alias("o") | ||
.multiple(), | ||
); | ||
|
||
app.run(args); | ||
} | ||
|
||
fn action(c: &Context) { | ||
// Count the number of times the flag was passed | ||
let verbosity_level = c.bool_flag_vec("verbose").iter().flatten().count(); | ||
|
||
println!("Verbosity level: {}", verbosity_level); | ||
|
||
// Print only the first 'header' flag passed | ||
println!("Headers: {:?}", c.string_flag("header")); | ||
|
||
// To access all 'header' flags passed, if the flag is not marked as multiple the | ||
// vector will only contain one element, the rest will be ignored. | ||
for header in c.string_flag_vec("header") { | ||
println!("Header: {:?}", header); | ||
} | ||
|
||
// Access all 'offset' flags passed | ||
for offset in c.uint_flag_vec("offset") { | ||
println!("offset: {:?}", offset); | ||
} | ||
} |
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