From 2bb4b4e691ac3372e19ee829a0bf46510b0a31d4 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Sun, 17 Mar 2019 14:35:54 +0100 Subject: [PATCH] Command line - improved error handling --- src/libslic3r/Config.cpp | 16 ++++++++-------- src/slic3r.cpp | 4 +++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/libslic3r/Config.cpp b/src/libslic3r/Config.cpp index 037bd3500..91e74a032 100644 --- a/src/libslic3r/Config.cpp +++ b/src/libslic3r/Config.cpp @@ -713,11 +713,8 @@ bool DynamicConfig::read_cli(int argc, char** argv, t_config_option_keys* extra, // Look for the cli -> option mapping. const auto it = opts.find(token); if (it == opts.end()) { - printf("Warning: unknown option --%s\n", token.c_str()); - // instead of continuing, return false to caller - // to stop execution and print usage - return false; - //continue; + boost::nowide::cerr << "Unknown option --" << token.c_str() << std::endl; + return false; } const t_config_option_key opt_key = it->second; const ConfigOptionDef &optdef = this->def()->options.at(opt_key); @@ -725,8 +722,8 @@ bool DynamicConfig::read_cli(int argc, char** argv, t_config_option_keys* extra, // look for it in the next token. if (optdef.type != coBool && optdef.type != coBools && value.empty()) { if (i == (argc-1)) { - printf("No value supplied for --%s\n", token.c_str()); - continue; + boost::nowide::cerr << "No value supplied for --" << token.c_str() << std::endl; + return false; } value = argv[++ i]; } @@ -759,7 +756,10 @@ bool DynamicConfig::read_cli(int argc, char** argv, t_config_option_keys* extra, static_cast(opt_base)->value = value; } else { // Any scalar value of a type different from Bool and String. - this->set_deserialize(opt_key, value, false); + if (! this->set_deserialize(opt_key, value, false)) { + boost::nowide::cerr << "Invalid value supplied for --" << token.c_str() << std::endl; + return false; + } } } return true; diff --git a/src/slic3r.cpp b/src/slic3r.cpp index b60c5b1dd..3b301ec2a 100644 --- a/src/slic3r.cpp +++ b/src/slic3r.cpp @@ -523,6 +523,8 @@ bool CLI::setup(int argc, char **argv) // If any option is unsupported, print usage and abort immediately. t_config_option_keys opt_order; if (! m_config.read_cli(argc, argv, &m_input_files, &opt_order)) { + // Separate error message reported by the CLI parser from the help. + boost::nowide::cerr << std::endl; this->print_help(); return false; } @@ -615,7 +617,7 @@ std::string CLI::output_filepath(const Model &model, IO::ExportFormat format) co }; auto proposed_path = boost::filesystem::path(model.propose_export_file_name_and_path(ext)); // use --output when available - std::string cmdline_param = m_config.opt_string("output", false); + std::string cmdline_param = m_config.opt_string("output"); if (! cmdline_param.empty()) { // if we were supplied a directory, use it and append our automatically generated filename boost::filesystem::path cmdline_path(cmdline_param);