diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/mt-tool.rs | 82 |
1 files 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<String>) { + 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 <inputs>... -Parses each input file. -"; +fn print_usage(opts: Options) { + let brief = "usage:\tmt-tool <CMD> [options]"; + println!(""); + print!("{}", opts.usage(&brief)); + println!("Commands:\n\ + \tparse <file> checks that file contains a valid modelica model\n\ + \tlist dumps all the known models in database\n\ + \tload <dir> checks that directory contains valid model and metadata\n\ +"); +} + +fn main() { + + let args: Vec<String> = 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<String>, + 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); }, + } } |