Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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")
.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
.help("Who to say hello to")
// 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, {}!",;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment