Commit 1a2eaba5 authored by kulvait's avatar kulvait

Parsing arguments improvements

parent cfb5211f
#pragma once
#include "PROG/parseArgs.h"
#include "CLI/CLI.hpp" //Command line parser
namespace CTL::util {
class Arguments
{
public:
Arguments(int argc, char ** argv, std::string appName="");
std::shared_ptr<CLI::App> getCliApp();
//Call this function for parsing
int parse();
virtual void defineArguments() = 0;
virtual int preParse() = 0;
virtual int postParse() = 0;
protected:
std::shared_ptr<CLI::App> cliApp;
int argc;
char ** argv;
};
} // namespace CTL
#pragma once
#include "PROG/parseArgs.h"
#include "PROG/RunTimeInfo.hpp"
#include "PLOG/PlogSetup.h"
namespace CTL::util {
class Program
{
public:
Program(int argc, char* argv[], plog::Severity debugLevel = plog::debug, bool logToFile = true, bool logToConsole = true);
void startLog();
void endLog(bool reportTimings=false);
util::RunTimeInfo getRunTimeInfo();
protected:
private:
std::chrono::time_point<std::chrono::steady_clock> start_time;
util::RunTimeInfo rti;
int argc;
char ** argv;
};
} // namespace CTL
......@@ -10,7 +10,7 @@
#include "stringFormatter.h"
namespace CTL {
namespace io {
namespace util {
/**
* @brief Class to provide run time information.
*
......
#include "PROG/Arguments.hpp"
namespace CTL::util {
Arguments::Arguments(int argc, char* argv[], std::string appName)
: argc(argc)
, argv(argv)
{
cliApp = std::make_shared<CLI::App>(appName);
}
int Arguments::parse()
{
this->defineArguments();
try
{
int preVal, postVal;
preVal = preParse();
if(preVal != 0)
{
return preVal;
}
cliApp->parse(argc, argv);
postVal = postParse();
if(postVal != 0)
{
return postVal;
}
} catch(const CLI::CallForHelp& e)
{
cliApp->exit(e); // Prints help message
return 1;
} catch(const CLI::ParseError& e)
{
int exitcode = cliApp->exit(e);
LOGE << io::xprintf("There was perse error with exit code %d catched.\n %s", exitcode,
cliApp->help().c_str());
return -1;
} catch(...)
{
LOGE << "Unknown exception catched";
return -1;
}
return 0;
}
std::shared_ptr<CLI::App> Arguments::getCliApp() { return cliApp; }
} // namespace CTL::util
#include "PROG/Program.hpp"
namespace CTL::util {
Program::Program(
int argc, char* argv[], plog::Severity debugLevel, bool logToFile, bool logToConsole)
: argc(argc)
, argv(argv)
{
plog::Severity verbosityLevel = debugLevel; // debug, info, ...
rti = util::RunTimeInfo(argc, argv);
std::string csvLogFile = "";
if(logToFile)
{
std::string csvLogFile = io::xprintf(
"/tmp/%s.csv", rti.getExecutableName().c_str()); // Set to empty string to disable
}
std::string xpath = rti.getExecutableDirectoryPath();
plog::PlogSetup plogSetup(verbosityLevel, csvLogFile, logToConsole);
plogSetup.initLogging();
start_time = std::chrono::steady_clock::now();
}
void Program::startLog() { LOGI << io::xprintf("START %s", rti.getExecutableName().c_str()); }
void Program::endLog(bool reportTimings)
{
if(reportTimings)
{
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start_time);
LOGI << io::xprintf("END %s, duration %0.3f s.", rti.getExecutableName().c_str(),
float(duration.count() / 1000.0f));
} else
{
LOGI << io::xprintf("END %s", rti.getExecutableName().c_str());
}
}
util::RunTimeInfo Program::getRunTimeInfo() { return rti; }
} // namespace CTL::util
#include "ARGPARSE/parseArgs.h"
#include "PROG/parseArgs.h"
namespace CTL {
namespace util {
......
#include "RunTimeInfo.hpp"
#include "PROG/RunTimeInfo.hpp"
namespace CTL {
namespace io {
namespace util {
RunTimeInfo::RunTimeInfo()
: RunTimeInfo(0, nullptr)
{
......
#define CATCH_CONFIG_RUNNER
#include "PlogSetup.h"
#include "catch.hpp"
#include "RunTimeInfo.hpp"
#include "PROG/RunTimeInfo.hpp"
using namespace CTL;
......@@ -27,7 +27,7 @@ int main(int argc, char* argv[])
std::string basedir()
{
io::RunTimeInfo rti;
util::RunTimeInfo rti;
std::string path = rti.getExecutableDirectoryPath();//build dir
return io::getParent(path);
}
......
......@@ -8,7 +8,7 @@
// Internal libs
#include "CTIOL.h"
#include "RunTimeInfo.hpp"
#include "PROG/RunTimeInfo.hpp"
using namespace CTL;
......
......@@ -5,7 +5,7 @@
#include "stringFormatter.h"
#include "DEN/DenFileInfo.hpp"
#include "DEN/DenFrame2DReader.hpp"
#include "RunTimeInfo.hpp"
#include "PROG/RunTimeInfo.hpp"
#include "littleEndianAlignment.h"
using namespace CTL;
......
// Logging, testing
#include "catch.hpp"
#include <plog/Log.h>
#include "RunTimeInfo.hpp"
#include "PROG/RunTimeInfo.hpp"
using namespace CTL;
TEST_CASE("TEST: RunTimeInfo.hpp", "RunTimeInfo")
{
io::RunTimeInfo rti;
util::RunTimeInfo rti;
std::string exe = rti.getExecutableName();
std::string path = rti.getExecutablePath();
std::string dir = rti.getExecutableDirectoryPath();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment