-
-
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.
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
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 @@ | ||
extern crate clap; | ||
|
||
use clap::{App, Arg, SubCommand}; | ||
|
||
fn main() { | ||
|
||
let matches = App::new("MyApp") | ||
.subcommand(SubCommand::with_name("ls") | ||
.aliases(&["list", "dir"]) | ||
.about("Adds files to myapp") | ||
.version("0.1") | ||
.author("Kevin K.") | ||
.arg(Arg::with_name("input") | ||
.help("the file to add") | ||
.index(1) | ||
.required(true)) | ||
) | ||
.get_matches(); | ||
|
||
// You can check if a subcommand was used like normal | ||
if matches.is_present("add") { | ||
println!("'myapp add' was run."); | ||
} | ||
|
||
// You can get the independent subcommand matches (which function exactly like App matches) | ||
if let Some(ref matches) = matches.subcommand_matches("add") { | ||
// Safe to use unwrap() because of the required() option | ||
println!("Adding file: {}", matches.value_of("input").unwrap()); | ||
} | ||
|
||
// You can also match on a subcommand's name | ||
match matches.subcommand_name() { | ||
Some("add") => println!("'myapp add' was used"), | ||
None => println!("No subcommand was used"), | ||
_ => println!("Some other subcommand was used"), | ||
} | ||
|
||
// Continued program logic goes here... | ||
} |
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