From d8cf3d3a78bf8e143a4839ab540ac30e29868ba5 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sat, 29 Oct 2016 17:18:17 -0700 Subject: expand scope of mt-tool --- src/bin/mt-tool.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/src/bin/mt-tool.rs b/src/bin/mt-tool.rs index d71a2eb..803bc46 100644 --- a/src/bin/mt-tool.rs +++ b/src/bin/mt-tool.rs @@ -1,22 +1,20 @@ extern crate modelthing; extern crate rustc_serialize; -extern crate docopt; +extern crate getopts; use modelthing::modelica_parser; -use docopt::Docopt; +use getopts::Options; use std::env; use std::io::Read; use std::fs::File; +use std::path::Path; use std::time::Instant; +use std::process::exit; -fn main() { - let args: Args = Docopt::new(USAGE) - .and_then(|d| d.argv(env::args()).decode()) - .unwrap_or_else(|e| e.exit()); - - for input in &args.arg_inputs { +fn parse_modelica_files(paths: Vec) { + for input in &paths { let mut s = String::new(); if let Err(err) = File::open(input).and_then(|mut f| f.read_to_string(&mut s)) { println!("Input `{}`: I/O Error {}", @@ -36,12 +34,66 @@ fn main() { } } -const USAGE: &'static str = " -Usage: modelthing-modelica ... -Parses each input file. -"; +fn print_usage(opts: Options) { + let brief = "usage:\tmt-tool [options]"; + println!(""); + print!("{}", opts.usage(&brief)); + println!("Commands:\n\ + \tparse checks that file contains a valid modelica model\n\ + \tlist dumps all the known models in database\n\ + \tload checks that directory contains valid model and metadata\n\ +"); +} + +fn main() { + + let args: Vec = env::args().collect(); + + let mut opts = Options::new(); + opts.optflag("h", "help", "print this help menu"); + opts.optflag("", "version", "print the version"); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(f) => { + println!("{}\n", f.to_string()); + print_usage(opts); + exit(-1); + } + }; + + if matches.opt_present("help") { + print_usage(opts); + return; + } + + if matches.opt_present("version") { + println!("modelthing {}", env!("CARGO_PKG_VERSION")); + return; + } + + if matches.free.is_empty() { + println!("At least a CMD is required; see --help"); + exit(-1); + } -#[derive(Debug, RustcDecodable)] -struct Args { - arg_inputs: Vec, + let cmd = matches.free[0].clone(); + match cmd.as_str() { + "parse" => { parse_modelica_files(matches.free[1..].iter().map(|x| x.to_string()).collect()); }, + "list" => { + // XXX: search path? + for m in modelthing::search_models(Path::new("examples")) { + println!("{}", m) + } + }, + "load" => { + if matches.free.len() != 2 { + println!("Expected a single path to load"); + exit(-1); + } + let me = modelthing::load_model_entry(Path::new(&matches.free[1])).unwrap(); + println!("{:?}", me); + }, + unknown => { println!("Unknown command: {}", unknown); exit(-1); }, + } } -- cgit v1.2.3