| 31 | using namespace chi; |
| 32 | |
| 33 | int run(const std::vector<std::string>& opts, const char* argv0) { |
| 34 | po::options_description run_opts; |
| 35 | run_opts.add_options()("input-file", po::value<std::string>(), |
| 36 | "The input file, - for stdin. Should be a chig module")( |
| 37 | "subargs", po::value<std::vector<std::string>>(), "Arguments to call main with"); |
| 38 | |
| 39 | po::positional_options_description pos; |
| 40 | pos.add("input-file", 1).add("subargs", -1); |
| 41 | |
| 42 | po::variables_map vm; |
| 43 | auto parsed_opts = |
| 44 | po::command_line_parser(opts).options(run_opts).positional(pos).allow_unregistered().run(); |
| 45 | po::store(parsed_opts, vm); |
| 46 | po::notify(vm); |
| 47 | |
| 48 | std::vector<std::string> command_opts = |
| 49 | po::collect_unrecognized(parsed_opts.options, po::include_positional); |
| 50 | // add the name into it |
| 51 | command_opts.insert(command_opts.begin(), argv0); |
| 52 | |
| 53 | if (vm.count("input-file") == 0) { |
| 54 | std::cerr << "chig compile: error: no input files" << std::endl; |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | std::string infile = vm["input-file"].as<std::string>(); |
| 59 | |
| 60 | Context c{fs::current_path()}; |
| 61 | |
| 62 | // load module |
| 63 | GraphModule* jmod = nullptr; |
| 64 | |
| 65 | Result res; |
| 66 | |
| 67 | if (infile == "-") { |
| 68 | nlohmann::json read_json = {}; |
| 69 | std::cin >> read_json; |
| 70 | res += c.addModuleFromJson("main", read_json, &jmod); |
| 71 | |
| 72 | } else { |
| 73 | // make sure it's an actual file |
| 74 | fs::path inpath = infile; |
| 75 | // remove extension if the user added it |
| 76 | inpath.replace_extension(""); |
| 77 | |
| 78 | fs::path moduleName = fs::relative(fs::current_path(), c.workspacePath() / "src") / inpath; |
| 79 | |
| 80 | ChiModule* cMod; |
| 81 | res += c.loadModule(moduleName.string(), LoadSettings::Default, &cMod); |
| 82 | if (!res) { |
| 83 | std::cerr << res.dump(); |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | jmod = dynamic_cast<GraphModule*>(cMod); |
| 88 | } |
| 89 | |
| 90 | if (!res) { |
no test coverage detected