Skip to content

Instantly share code, notes, and snippets.

@gibfahn
Created February 8, 2018 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gibfahn/f4f20ffa41a47787a1a0c1b64a0147ba to your computer and use it in GitHub Desktop.
Save gibfahn/f4f20ffa41a47787a1a0c1b64a0147ba to your computer and use it in GitHub Desktop.
Trying out structopt based on yoshuawuyt's example
extern crate structopt;
#[macro_use]
extern crate structopt_derive;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt()]
enum Cmds {
#[structopt(name = "add")]
/// Add a thing.
Add {
#[structopt(short = "i")]
interactive: bool,
#[structopt(short = "p")]
patch: bool,
files: Vec<String>,
},
#[structopt(name = "fetch")]
/// Fetch a thing.
Fetch {
#[structopt(long = "dry-run")]
dry_run: bool,
#[structopt(long = "all")]
all: bool,
repository: Option<String>,
},
#[structopt(name = "commit")]
/// Commit a thing.
Commit {
#[structopt(short = "m")]
message: Option<String>,
#[structopt(short = "a")]
all: bool,
},
}
fn main() {
let mut args = Cmds::from_args();
match &args {
&Cmds::Add { .. } => {
// this is where we'd pass the struct to some_function
// I thought this would require NLL, but it seems not!
do_mut(&mut args);
}
&Cmds::Fetch { .. } => {
// this is where we'd pass the struct to some_other_function
println!("{:?}", args);
}
&Cmds::Commit { .. } => {
println!("Commit: {:?}", args);
}
};
}
/// Some fn that takes a mutable reference.
fn do_mut(arg: &mut Cmds) {
// Do something here.
println!("Args are: {:?}", arg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment