Let ParseAll exit if unsuccessfull

This commit is contained in:
Alexey Rybalchenko 2016-05-12 11:35:24 +02:00 committed by Mohammad Al-Turany
parent 31456e71ff
commit 28aa5e4caa
8 changed files with 23 additions and 44 deletions

View File

@ -33,7 +33,7 @@ FairMQProgOptions::~FairMQProgOptions()
{ {
} }
int FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregistered) void FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregistered)
{ {
LOG(NOLOG) << ""; LOG(NOLOG) << "";
// init description // init description
@ -41,7 +41,8 @@ int FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregiste
// parse command line options // parse command line options
if (ParseCmdLine(argc, argv, fCmdLineOptions, fVarMap, allowUnregistered)) if (ParseCmdLine(argc, argv, fCmdLineOptions, fVarMap, allowUnregistered))
{ {
return 1; LOG(ERROR) << "Could not parse cmd options";
exit(EXIT_FAILURE);
} }
// if txt/INI configuration file enabled then parse it as well // if txt/INI configuration file enabled then parse it as well
@ -52,13 +53,14 @@ int FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregiste
{ {
if (ParseCfgFile(fConfigFile.string(), fConfigFileOptions, fVarMap, allowUnregistered)) if (ParseCfgFile(fConfigFile.string(), fConfigFileOptions, fVarMap, allowUnregistered))
{ {
return 1; LOG(ERROR) << "Could not parse config";
exit(EXIT_FAILURE);
} }
} }
else else
{ {
LOG(ERROR) << "config file '" << fConfigFile << "' not found"; LOG(ERROR) << "config file '" << fConfigFile << "' not found";
return 1; exit(EXIT_FAILURE);
} }
} }
@ -123,32 +125,30 @@ int FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregiste
id = fVarMap["id"].as<std::string>(); id = fVarMap["id"].as<std::string>();
} }
std::string file_extension = boost::filesystem::extension(file); std::string fileExtension = boost::filesystem::extension(file);
std::transform(file_extension.begin(), file_extension.end(), file_extension.begin(), ::tolower); std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
if (file_extension == ".json") if (fileExtension == ".json")
{ {
UserParser<FairMQParser::JSON>(file, id); UserParser<FairMQParser::JSON>(file, id);
} }
else else
{ {
if (file_extension == ".xml") if (fileExtension == ".xml")
{ {
UserParser<FairMQParser::XML>(file, id); UserParser<FairMQParser::XML>(file, id);
} }
else else
{ {
LOG(ERROR) << "mq-config command line called but file extension '" LOG(ERROR) << "mq-config command line called but file extension '"
<< file_extension << fileExtension
<< "' not recognized. Program will now exit"; << "' not recognized. Program will now exit";
return 1; exit(EXIT_FAILURE);
} }
} }
} }
} }
return 0;
} }
int FairMQProgOptions::NotifySwitchOption() int FairMQProgOptions::NotifySwitchOption()

View File

@ -36,7 +36,7 @@ class FairMQProgOptions : public FairProgOptions
// parse command line and txt/INI configuration file. // parse command line and txt/INI configuration file.
// default parser for the mq-configuration file (JSON/XML) is called if command line key mq-config is called // default parser for the mq-configuration file (JSON/XML) is called if command line key mq-config is called
virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false); virtual void ParseAll(const int argc, char** argv, bool allowUnregistered = false);
// external parser, store function // external parser, store function
template <typename T, typename ...Args> template <typename T, typename ...Args>

View File

@ -27,7 +27,7 @@
#include <tuple> #include <tuple>
/* /*
* FairProgOptions abstract base class * FairProgOptions abstract base class
* parse command line, configuration file options as well as environment variables. * parse command line, configuration file options as well as environment variables.
* *
* The user defines in the derived class the option descriptions and * The user defines in the derived class the option descriptions and
@ -42,15 +42,14 @@
* fVisibleOptions.add(fCmdlineOptions); * fVisibleOptions.add(fCmdlineOptions);
* } * }
* virtual ~MyOptions() {} * virtual ~MyOptions() {}
* virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false) * virtual void ParseAll(const int argc, char** argv, bool allowUnregistered = false)
* { * {
* if(ParseCmdLine(argc, argv, fCmdlineOptions, fVarMap, allowUnregistered)) * if (ParseCmdLine(argc, argv, fCmdlineOptions, fVarMap, allowUnregistered))
* { * {
* return 1; * exit(EXIT_FAILURE);
* } * }
* *
* PrintOptions(); * PrintOptions();
* return 0;
* } * }
* } * }
*/ */
@ -110,7 +109,7 @@ class FairProgOptions
int ParseEnvironment(const std::function<std::string(std::string)>&); int ParseEnvironment(const std::function<std::string(std::string)>&);
virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false) = 0;// TODO change return type to bool and propagate to executable virtual void ParseAll(const int argc, char** argv, bool allowUnregistered = false) = 0;// TODO change return type to bool and propagate to executable
virtual int PrintOptions(); virtual int PrintOptions();
int PrintHelp() const; int PrintHelp() const;

View File

@ -37,11 +37,7 @@ int main(int argc, char** argv)
FairMQProgOptions config; FairMQProgOptions config;
config.AddToCmdLineOptions(samplerOptions); config.AddToCmdLineOptions(samplerOptions);
config.ParseAll(argc, argv);
if (config.ParseAll(argc, argv))
{
return 0;
}
FairMQBenchmarkSampler sampler; FairMQBenchmarkSampler sampler;
sampler.SetProperty(FairMQBenchmarkSampler::MsgSize, msgSize); sampler.SetProperty(FairMQBenchmarkSampler::MsgSize, msgSize);

View File

@ -24,11 +24,7 @@ int main(int argc, char** argv)
try try
{ {
FairMQProgOptions config; FairMQProgOptions config;
config.ParseAll(argc, argv);
if (config.ParseAll(argc, argv))
{
return 0;
}
FairMQMerger merger; FairMQMerger merger;
runStateMachine(merger, config); runStateMachine(merger, config);

View File

@ -24,11 +24,7 @@ int main(int argc, char** argv)
try try
{ {
FairMQProgOptions config; FairMQProgOptions config;
config.ParseAll(argc, argv);
if (config.ParseAll(argc, argv))
{
return 0;
}
FairMQProxy proxy; FairMQProxy proxy;
runStateMachine(proxy, config); runStateMachine(proxy, config);

View File

@ -35,11 +35,7 @@ int main(int argc, char** argv)
FairMQProgOptions config; FairMQProgOptions config;
config.AddToCmdLineOptions(sinkOptions); config.AddToCmdLineOptions(sinkOptions);
config.ParseAll(argc, argv);
if (config.ParseAll(argc, argv))
{
return 0;
}
FairMQSink sink; FairMQSink sink;
sink.SetProperty(FairMQSink::NumMsgs, numMsgs); sink.SetProperty(FairMQSink::NumMsgs, numMsgs);

View File

@ -24,11 +24,7 @@ int main(int argc, char** argv)
try try
{ {
FairMQProgOptions config; FairMQProgOptions config;
config.ParseAll(argc, argv);
if (config.ParseAll(argc, argv))
{
return 0;
}
FairMQSplitter splitter; FairMQSplitter splitter;
runStateMachine(splitter, config); runStateMachine(splitter, config);