Created
February 8, 2018 14:06
-
-
Save gibfahn/f4f20ffa41a47787a1a0c1b64a0147ba to your computer and use it in GitHub Desktop.
Trying out structopt based on yoshuawuyt's example
This file contains hidden or 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 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