-
-
Save snoyberg/73f9fdb15faf7c07302b88b2e0280fd7 to your computer and use it in GitHub Desktop.
Clap
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
extern crate clap; | |
use clap::{Arg, App}; | |
struct HelloArgs { | |
name: String, | |
} | |
impl HelloArgs { | |
fn new() -> Self { | |
// basic app information | |
let app = App::new("hello-clap") | |
.version("1.0") | |
.about("Says hello") | |
.author("Michael Snoyman"); | |
// Define the name command line option | |
let name_option = Arg::with_name("name") | |
.long("name") // allow --name | |
.short("n") // allow -n | |
.takes_value(true) | |
.help("Who to say hello to") | |
.required(true); | |
// now add in the argument we want to parse | |
let app = app.arg(name_option); | |
// extract the matches | |
let matches = app.get_matches(); | |
// Extract the actual name | |
let name = matches.value_of("name") | |
.expect("This can't be None, we said it was required"); | |
HelloArgs { name: name.to_string() } | |
} | |
} | |
fn main() { | |
let hello = HelloArgs::new(); | |
println!("Hello, {}!", hello.name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment