From: Maxim Mamontov Date: Sat, 9 Nov 2013 21:06:43 +0000 (+0200) Subject: Beautified error notification. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/9977f098136de2dd74a62de2fc535cbdfafcda1f Beautified error notification. --- diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 886e17e2..c88de14d 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -118,6 +118,10 @@ template inline PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) { +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); T value; if (str2x(*argv, value)) throw ERROR(std::string("Bad argument: '") + *argv + "'"); @@ -129,6 +133,10 @@ template <> inline PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) { +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); m_param = *argv; return PARSER_STATE(false, --argc, ++argv); } diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index a1c8df8f..c1fc4c3e 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -178,6 +178,10 @@ class CONFIG_ACTION : public ACTION PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv) { +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); char * pos = strchr(*argv, '@'); if (pos != NULL) { @@ -1214,7 +1218,18 @@ blocks.Add("Connection options") .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, ""), "\tpassword for the administrative login") .Add("a", "address", SGCONF::MakeParamAction(config, ""), "connection params as a single string in format: :@:"); -SGCONF::PARSER_STATE state(blocks.Parse(--argc, ++argv)); // Skipping self name + +SGCONF::PARSER_STATE state(false, argc, argv); + +try +{ +state = blocks.Parse(--argc, ++argv); // Skipping self name +} +catch (const SGCONF::OPTION::ERROR& ex) +{ +std::cerr << ex.what() << "\n"; +return -1; +} if (state.stop) return 0; diff --git a/projects/sgconf/options.cpp b/projects/sgconf/options.cpp index 06889174..6fddaf38 100644 --- a/projects/sgconf/options.cpp +++ b/projects/sgconf/options.cpp @@ -103,7 +103,10 @@ try } catch (const ACTION::ERROR & ex) { - throw ERROR(m_longName + ": " + ex.what()); + if (m_longName.empty()) + throw ERROR("-" + m_shortName + ": " + ex.what()); + else + throw ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what()); } }