aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-12-25 21:55:04 -0800
committerbnewbold <bnewbold@robocracy.org>2016-12-25 21:55:04 -0800
commitb7b147c82e93ab86eb99a80f1a051e4c648b7425 (patch)
tree396733e7b8476b01ef53e2ccfb0630eeed22a0c9 /src/bin
parent0adbd20ff8b4d4f8156ee941d3d0b17e80c3a80f (diff)
downloadmodelthing-b7b147c82e93ab86eb99a80f1a051e4c648b7425.tar.gz
modelthing-b7b147c82e93ab86eb99a80f1a051e4c648b7425.zip
switch mt-tool to clap argument parser
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/mt-tool.rs134
1 files changed, 52 insertions, 82 deletions
diff --git a/src/bin/mt-tool.rs b/src/bin/mt-tool.rs
index 95a8938..317ac20 100644
--- a/src/bin/mt-tool.rs
+++ b/src/bin/mt-tool.rs
@@ -1,14 +1,13 @@
extern crate modelthing;
extern crate rustc_serialize;
-extern crate getopts;
+extern crate clap;
extern crate modelica_parser;
use modelthing::transpile_scheme::TranspileScheme;
use modelthing::transpile_js::TranspileJS;
use modelthing::modelica_model::ModelicaModelExt;
-use getopts::Options;
-use std::env;
+use clap::{App, SubCommand};
use std::io::Read;
use std::fs::File;
use std::path::Path;
@@ -37,97 +36,68 @@ fn parse_modelica_files(paths: Vec<String>) {
}
}
-fn print_usage(opts: Options) {
- let brief = "usage:\tmt-tool <CMD> [options]";
- println!("");
- print!("{}", opts.usage(&brief));
- println!("");
- println!(r#"Commands:
- parse <file> checks that file contains a valid modelica model
- list dumps all the known models in database
- load <dir> checks that directory contains valid model and metadata
- transpile_js <dir> converts modelica to javascript function
- transpile_scheme <dir> converts modelica to scheme function
-"#);
-}
-
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");
- opts.optmulti("", "dep", "dependent variable", "VAR");
- opts.optmulti("", "indep", "dependent variable", "VAR");
-
- 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;
- }
+ let matches = App::new("mt-tool")
+ .version(env!("CARGO_PKG_VERSION"))
+ .subcommand(SubCommand::with_name("parse")
+ .about("checks that file contains a valid modelica model")
+ .arg_from_usage("<FILE>... 'file to parse from'"))
+ .subcommand(SubCommand::with_name("list")
+ .about("dumps all the known models in database"))
+ .subcommand(SubCommand::with_name("load")
+ .about("checks that directory contains valid model and metadata")
+ .arg_from_usage("<DIR> 'model to load'"))
+ .subcommand(SubCommand::with_name("transpile_js")
+ .about("converts modelica to javascript function")
+ .arg_from_usage("<DIR> 'model to load'"))
+ .subcommand(SubCommand::with_name("transpile_scheme")
+ .about("converts modelica to scheme function")
+ .arg_from_usage("<DIR> 'model to load'"))
+ .subcommand(SubCommand::with_name("solve_for")
+ .about("")
+ .arg_from_usage("<DIR> 'model to load'
+ --dep <VAR>... 'dependent variable'
+ --indep <VAR>... 'independent variable'"))
+ .get_matches();
- if matches.free.is_empty() {
- println!("At least a CMD is required; see --help");
- exit(-1);
- }
-
- let cmd = matches.free[0].clone();
- match cmd.as_str() {
- "parse" => { parse_modelica_files(matches.free[1..].iter().map(|x| x.to_string()).collect()); },
- "solve_for" => {
- if matches.free.len() != 2 {
- println!("Expected a path to load");
- exit(-1);
- }
- let me = modelthing::load_model_entry(Path::new(&matches.free[1])).unwrap();
- println!("{:?}", me.ast.solve_for(matches.opt_strs("indep"), matches.opt_strs("dep")));
+
+ match matches.subcommand() {
+ ("parse", Some(subm)) => {
+ parse_modelica_files(subm.values_of_lossy("FILE").unwrap());
},
- "transpile_scheme" => {
- 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();
+ ("solve_for", Some(subm)) => {
+ let dir = Path::new(subm.value_of("DIR").unwrap());
+ let indep = subm.values_of_lossy("indep").unwrap();
+ let dep = subm.values_of_lossy("dep").unwrap();
+ let me = modelthing::load_model_entry(dir).unwrap();
+ println!("{:?}", me.ast.solve_for(indep, dep));
+ },
+ ("transpile_scheme", Some(subm)) => {
+ let dir = Path::new(subm.value_of("DIR").unwrap());
+ let me = modelthing::load_model_entry(dir).unwrap();
println!("{}", me.ast.repr_scheme().unwrap());
},
- "transpile_js" => {
- 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();
+ ("transpile_js", Some(subm)) => {
+ let dir = Path::new(subm.value_of("DIR").unwrap());
+ let me = modelthing::load_model_entry(dir).unwrap();
println!("{}", me.ast.repr_js().unwrap());
},
- "list" => {
- let search_path =
- if matches.free.len() >= 2 { matches.free[1].clone() }
- else { "examples".to_string() };
- for m in modelthing::search_models(Path::new(&search_path)) {
+ ("list", Some(subm)) => {
+ let dir = Path::new(subm.value_of("DIR").unwrap_or("examples"));
+ for m in modelthing::search_models(dir) {
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();
+ ("load", Some(subm)) => {
+ let dir = Path::new(subm.value_of("DIR").unwrap());
+ let me = modelthing::load_model_entry(dir).unwrap();
println!("{:?}", me);
},
- unknown => { println!("Unknown command: {}", unknown); exit(-1); },
+ _ => {
+ println!("Missing or unimplemented command!");
+ println!("{}", matches.usage());
+ exit(-1);
+ },
}
}