From cd8dfd7b6937fd7b3b8421f3ad837983af93c7bc Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Tue, 4 Jan 2022 23:27:19 +0100 Subject: [PATCH] update runtime executable --- src/runtime/pixwerx.cpp | 62 ++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/runtime/pixwerx.cpp b/src/runtime/pixwerx.cpp index 8e6b2c5..32f8521 100644 --- a/src/runtime/pixwerx.cpp +++ b/src/runtime/pixwerx.cpp @@ -14,45 +14,73 @@ #include #include +#include int main(int argc,const char** argv) { argagg::parser argparser {{ { "help", {"-h", "--help"}, "shows this help message", 0}, { "file", {"-f", "--file"}, "load file to run", 1} - }}; + }}; argagg::parser_results args; + std::ostringstream usage; + usage + << "pixwerx rendering engine\n" + << '\n' + << "Usage: " << argv[0] << " [--help] [--file filename.lua]\n" + << '\n'; + + try { args = argparser.parse(argc,argv); } catch (std::exception &e) { - std::cerr << e.what() << std::endl; + + + argagg::fmt_ostream help(std::cerr); + + help << usage.str() << argparser << '\n' + << "Encountered exception while parsing arguments: " << e.what() + << '\n'; return -1; } std::ifstream input; - input.open(args["file"].as(),std::ifstream::in); + // run a file + if (args["file"]) { + + input.open(args["file"].as(),std::ifstream::in); + + if (!input.is_open()) { + std::cerr << "cannot open '" << args["file"].as() << "'" << std::endl; + } + + std::ostringstream sout; + std::copy(std::istreambuf_iterator(input), + std::istreambuf_iterator(), + std::ostreambuf_iterator(sout)); + + input.close(); + + pw::script::initialize(); + + pw::script s; + + return s.eval(sout.str().c_str()); + + // show help + } else if (args["help"]) { + + argagg::fmt_ostream help(std::cerr); + help << usage.str() << argparser; - if (!input.is_open()) { - std::cerr << "cannot open '" << args["file"].as() << "'" << std::endl; } - std::ostringstream sout; - std::copy(std::istreambuf_iterator(input), - std::istreambuf_iterator(), - std::ostreambuf_iterator(sout)); - - input.close(); - - pw::script::initialize(); - - pw::script s; - - return s.eval(sout.str().c_str()); + return 0; }