From 0003e1b9c35f47ee696a47cf06fd3265ddd28c55 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 26 Aug 2018 15:46:52 -0500 Subject: [PATCH] FEATURE: Default to printing out our help if no subcommand is used. --- src/main.rs | 198 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 115 insertions(+), 83 deletions(-) diff --git a/src/main.rs b/src/main.rs index ecb44fc..b85aa14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ use ucglib::convert::traits; use ucglib::convert::ConverterRegistry; // TODO(jwall): List the target output types automatically. -fn do_flags<'a>() -> clap::ArgMatches<'a> { +fn do_flags<'a, 'b>() -> clap::App<'a, 'b> { clap_app!( ucg => (version: crate_version!()) @@ -55,7 +55,7 @@ fn do_flags<'a>() -> clap::ArgMatches<'a> { (@subcommand converters => (about: "list the available converters") ) - ).get_matches() + ) } fn run_converter(c: &traits::Converter, v: Rc, f: Option<&str>) -> traits::Result { @@ -201,99 +201,131 @@ fn visit_ucg_files( Ok(result) } -fn main() { - let app = do_flags(); - let cache: Rc> = Rc::new(RefCell::new(MemoryCache::new())); - let registry = ConverterRegistry::make_registry(); - if let Some(matches) = app.subcommand_matches("inspect") { - let file = matches.value_of("INPUT").unwrap(); - let sym = matches.value_of("sym"); - let target = matches.value_of("target").unwrap(); - let root = PathBuf::from(file); - let mut builder = build::Builder::new(root.parent().unwrap(), cache); - match registry.get_converter(target) { - Some(converter) => { - // TODO(jwall): We should warn if this is a test file. - let result = builder.build_file(file); - if !result.is_ok() { - eprintln!("{:?}", result.err().unwrap()); - process::exit(1); - } - let val = match sym { - Some(sym_name) => builder.get_out_by_name(sym_name), - None => builder.last, - }; - match val { - Some(value) => { - // We use None here because we always output to stdout for an inspect. - run_converter(converter, value, None).unwrap(); - eprintln!("Build successful"); - process::exit(0); - } - None => { - eprintln!("Build results in no value."); - process::exit(1); - } - } - } - None => { - eprintln!("No such converter {}", target); +fn inspect_command( + matches: &clap::ArgMatches, + cache: Rc>, + registry: &ConverterRegistry, +) { + let file = matches.value_of("INPUT").unwrap(); + let sym = matches.value_of("sym"); + let target = matches.value_of("target").unwrap(); + let root = PathBuf::from(file); + let mut builder = build::Builder::new(root.parent().unwrap(), cache); + match registry.get_converter(target) { + Some(converter) => { + // TODO(jwall): We should warn if this is a test file. + let result = builder.build_file(file); + if !result.is_ok() { + eprintln!("{:?}", result.err().unwrap()); process::exit(1); } - } - } else if let Some(matches) = app.subcommand_matches("build") { - let files = matches.values_of("INPUT"); - let recurse = matches.is_present("recurse"); - let mut ok = true; - if files.is_none() { - let curr_dir = std::env::current_dir().unwrap(); - let ok = visit_ucg_files(curr_dir.as_path(), recurse, false, cache.clone(), ®istry); - if let Ok(false) = ok { - process::exit(1) + let val = match sym { + Some(sym_name) => builder.get_out_by_name(sym_name), + None => builder.last, + }; + match val { + Some(value) => { + // We use None here because we always output to stdout for an inspect. + run_converter(converter, value, None).unwrap(); + eprintln!("Build successful"); + process::exit(0); + } + None => { + eprintln!("Build results in no value."); + process::exit(1); + } } } + None => { + eprintln!("No such converter {}", target); + process::exit(1); + } + } +} + +fn build_command( + matches: &clap::ArgMatches, + cache: Rc>, + registry: &ConverterRegistry, +) { + let files = matches.values_of("INPUT"); + let recurse = matches.is_present("recurse"); + let mut ok = true; + if files.is_none() { + let curr_dir = std::env::current_dir().unwrap(); + let ok = visit_ucg_files(curr_dir.as_path(), recurse, false, cache.clone(), ®istry); + if let Ok(false) = ok { + process::exit(1) + } + } + for file in files.unwrap() { + let pb = PathBuf::from(file); + if let Ok(false) = visit_ucg_files(&pb, recurse, false, cache.clone(), ®istry) { + ok = false; + } + } + if !ok { + process::exit(1) + } +} + +fn test_command( + matches: &clap::ArgMatches, + cache: Rc>, + registry: &ConverterRegistry, +) { + let files = matches.values_of("INPUT"); + let recurse = matches.is_present("recurse"); + if files.is_none() { + let curr_dir = std::env::current_dir().unwrap(); + let ok = visit_ucg_files(curr_dir.as_path(), recurse, true, cache.clone(), ®istry); + if let Ok(false) = ok { + process::exit(1) + } + } else { + let mut ok = true; for file in files.unwrap() { let pb = PathBuf::from(file); - if let Ok(false) = visit_ucg_files(&pb, recurse, false, cache.clone(), ®istry) { + //if pb.is_dir() { + if let Ok(false) = + visit_ucg_files(pb.as_path(), recurse, true, cache.clone(), ®istry) + { ok = false; } } if !ok { process::exit(1) } - } else if let Some(matches) = app.subcommand_matches("test") { - let files = matches.values_of("INPUT"); - let recurse = matches.is_present("recurse"); - if files.is_none() { - let curr_dir = std::env::current_dir().unwrap(); - let ok = visit_ucg_files(curr_dir.as_path(), recurse, true, cache.clone(), ®istry); - if let Ok(false) = ok { - process::exit(1) - } - } else { - let mut ok = true; - for file in files.unwrap() { - let pb = PathBuf::from(file); - //if pb.is_dir() { - if let Ok(false) = - visit_ucg_files(pb.as_path(), recurse, true, cache.clone(), ®istry) - { - ok = false; - } - } - if !ok { - process::exit(1) - } - } - process::exit(0); - } else if let Some(_todo) = app.subcommand_matches("converters") { - println!("Available converters:"); + } + process::exit(0); +} + +fn converters_command(registry: &ConverterRegistry) { + println!("Available converters:"); + println!(""); + for (name, c) in registry.get_converter_list().iter() { + println!("- {}", name); + println!(" Description: {}", c.description()); + println!(" Output Extension: `.{}`", c.file_ext()); + println!(""); + } +} + +fn main() { + let mut app = do_flags(); + let app_matches = app.clone().get_matches(); + let cache: Rc> = Rc::new(RefCell::new(MemoryCache::new())); + let registry = ConverterRegistry::make_registry(); + if let Some(matches) = app_matches.subcommand_matches("inspect") { + inspect_command(matches, cache, ®istry); + } else if let Some(matches) = app_matches.subcommand_matches("build") { + build_command(matches, cache, ®istry); + } else if let Some(matches) = app_matches.subcommand_matches("test") { + test_command(matches, cache, ®istry); + } else if let Some(_) = app_matches.subcommand_matches("converters") { + converters_command(®istry) + } else { + app.print_help().unwrap(); println!(""); - for (name, c) in registry.get_converter_list().iter() { - println!("- {}", name); - println!(" Description: {}", c.description()); - println!(" Output Extension: `.{}`", c.file_ext()); - println!(""); - } } }