Skip to content

Instantly share code, notes, and snippets.

@snoyberg snoyberg/clap-example.rs Secret
Created Aug 28, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.