Skip to content

Instantly share code, notes, and snippets.

@snoyberg
Created August 28, 2019 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save snoyberg/73f9fdb15faf7c07302b88b2e0280fd7 to your computer and use it in GitHub Desktop.
Save snoyberg/73f9fdb15faf7c07302b88b2e0280fd7 to your computer and use it in GitHub Desktop.
Clap
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