From b7b147c82e93ab86eb99a80f1a051e4c648b7425 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 25 Dec 2016 21:55:04 -0800 Subject: switch mt-tool to clap argument parser --- Cargo.lock | 52 +++++++++++++++++++++ Cargo.toml | 1 + src/bin/mt-tool.rs | 134 +++++++++++++++++++++-------------------------------- 3 files changed, 105 insertions(+), 82 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25a46e2..d293506 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "modelthing" version = "0.1.0" dependencies = [ + "clap 2.19.2 (registry+https://github.com/rust-lang/crates.io-index)", "docopt 0.6.86 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -21,6 +22,11 @@ dependencies = [ "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ansi_term" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "atty" version = "0.1.2" @@ -59,6 +65,21 @@ name = "buf-read-ext" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "clap" +version = "2.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "term_size 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-segmentation 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "colored" version = "1.3.2" @@ -648,6 +669,16 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "term_size" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "textnonce" version = "0.4.1" @@ -737,6 +768,16 @@ name = "unicode-normalization" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-segmentation" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-width" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.0.2" @@ -773,6 +814,11 @@ name = "utf8-ranges" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vec_map" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.8" @@ -785,12 +831,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" +"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" "checksum atty 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d0fd4c0631f06448cc45a6bbb3b710ebb7ff8ccb96a0800c994afe23a70d5df2" "checksum bit-set 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "84527c7b0452f22545cc010e72d366a435561d2b28b978035550b3778c4d428d" "checksum bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5b97c2c8e8bbb4251754f559df8af22fb264853c7d009084a576cdf12565089d" "checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3" "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" "checksum buf-read-ext 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc384072fac32fb50f4a327c33e2004897d11c561d008dd6031fb7f19b04de2c" +"checksum clap 2.19.2 (registry+https://github.com/rust-lang/crates.io-index)" = "305ad043f009db535a110200541d4567b63e172b1fe030313fbb92565da7ed24" "checksum colored 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "585756a5d597a0ecdf9c963be84c6eb0e25a3590b535ac6f27e98254266b4f4c" "checksum cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0e3d6405328b6edb412158b3b7710e2634e23f3614b9bb1c412df7952489a626" "checksum diff 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e48977eec6d3b7707462c2dc2e1363ad91b5dd822cf942537ccdc2085dc87587" @@ -859,6 +907,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "67f84c44fbb2f91db7fef94554e6b2ac05909c9c0b0bc23bb98d3a1aebfe7f7c" "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" "checksum term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3deff8a2b3b6607d6d7cc32ac25c0b33709453ca9cceac006caac51e963cf94a" +"checksum term_size 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f7f5f3f71b0040cecc71af239414c23fd3c73570f5ff54cf50e03cef637f2a0" "checksum textnonce 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e6e4d19cdd620f4767b7449be15f0a9d078b2def9c82b468f8b41a7140259a2a" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" @@ -871,10 +920,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "13a5906ca2b98c799f4b1ab4557b76367ebd6ae5ef14930ec841c74aed5f3764" "checksum unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c1f7ceb96afdfeedee42bade65a0d585a6a0106f681b6749c8ff4daa8df30b3f" "checksum unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "26643a2f83bac55f1976fb716c10234485f9202dcd65cfbdf9da49867b271172" +"checksum unicode-segmentation 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c3bc443ded17b11305ffffe6b37e2076f328a5a8cb6aa877b1b98f77699e98b5" +"checksum unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2d6722facc10989f63ee0e20a83cd4e1714a9ae11529403ac7e0afd069abc39e" "checksum unicode-xid 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f69506a2561962651710609304bbb961fa3da598c812f877975a82e48ee144f9" "checksum unsafe-any 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b351086021ebc264aea3ab4f94d61d889d98e5e9ec2d985d993f50133537fd3a" "checksum url 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "48ccf7bd87a81b769cf84ad556e034541fb90e1cd6d4bc375c822ed9500cd9d7" "checksum user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ef4711d107b21b410a3a974b1204d9accc8b10dad75d8324b5d755de1617d47" "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" +"checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index cd16e93..18d91c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["bnewbold "] [dependencies] getopts = "^0.2" +clap = "2" toml = "0.2" log = "0.3" env_logger = "0.3" 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) { } } -fn print_usage(opts: Options) { - let brief = "usage:\tmt-tool [options]"; - println!(""); - print!("{}", opts.usage(&brief)); - println!(""); - println!(r#"Commands: - parse checks that file contains a valid modelica model - list dumps all the known models in database - load checks that directory contains valid model and metadata - transpile_js converts modelica to javascript function - transpile_scheme converts modelica to scheme function -"#); -} - 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"); - 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 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(" 'model to load'")) + .subcommand(SubCommand::with_name("transpile_js") + .about("converts modelica to javascript function") + .arg_from_usage(" 'model to load'")) + .subcommand(SubCommand::with_name("transpile_scheme") + .about("converts modelica to scheme function") + .arg_from_usage(" 'model to load'")) + .subcommand(SubCommand::with_name("solve_for") + .about("") + .arg_from_usage(" 'model to load' + --dep ... 'dependent variable' + --indep ... '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); + }, } } -- cgit v1.2.3