Skip to content

Commit

Permalink
CLI extraction concept (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mj-xmr authored Dec 3, 2021
1 parent 1435370 commit ee6a131
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 80 deletions.
77 changes: 77 additions & 0 deletions src/lib-base/src/CLI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "CLI.h"
#include "CLIResult.h"
#include "ConfigTS.h"
//#include "TSXformTypeString.h"
#include <Util/CoutBuf.hpp>

#include <EnjoLibBoost/ProgramOptions.hpp>

using namespace EnjoLib;

CLI::CLI(){}
CLI::~CLI(){}

EnjoLib::Result<CLIResult> CLI::GetConfigs(int argc, char ** argv) const
{
//const char * OPT_PLUGIN = "plugin";

const char * OPT_MIN_MONTH = "min-month";
const char * OPT_MIN_YEAR = "min-year";
const char * OPT_MAX_MONTH = "max-month";
const char * OPT_MAX_YEAR = "max-year";
//const char * OPT_LATEST_DATE = "latest-date";
const char * OPT_SYMBOL = "sym";
const char * OPT_PERIOD = "per";

EnjoLib::ProgramOptionsState popState;
////popState.AddStr(OPT_PLUGIN, "Plugin name");
popState.AddInt(OPT_MIN_MONTH, "Start month"); /// TODO: Option Add(char*, char*, int & refVal);. A new class beside the old one or replace the old one?
popState.AddInt(OPT_MIN_YEAR, "Start year");
popState.AddInt(OPT_MAX_MONTH, "End month");
popState.AddInt(OPT_MAX_YEAR, "End year");
popState.AddStr(OPT_SYMBOL, "Symbol name");
popState.AddStr(OPT_PERIOD, "Period name");

popState.ReadArgs(argc, argv);
const EnjoLib::ProgramOptions pops(popState);

ConfigSym confSym;

if (pops.IsHelpRequested())
{
/// TODO: Try also to react also on -h by adding here:
/**
bool ProgramOptions::IsHelpRequested() const
{
return GetBoolFromMap(ProgramOptionsState::OPT_HELP);
}
return GetBoolFromMap(ProgramOptionsState::OPT_HELP || return GetBoolFromMap(ProgramOptionsState::OPT_HELP_MIN););
const char * OPT_HELP_MIN = "h";
*/
ELO
LOG << pops.GetHelp() << Nl;

LOG << GetAdditionalHelp();
LOG << Nl;
LOG << "The default path of the script is: " << ConfigTS::DEFAULT_SCRIPT_FILE_NAME << Nl;
LOG << "\n(C) 2012-2022 mj-xmr. Licensed under AGPLv3.\n";
LOG << "https://github.com/mj-xmr/tsqsim\n";
LOG << "\nRemember to be happy.\n\n";
return EnjoLib::Result<CLIResult>(CLIResult(confSym), false);
}

confSym.dates.Set0();

confSym.dates.monthStart = pops.GetIntFromMap(OPT_MIN_MONTH);
confSym.dates.yearStart = pops.GetIntFromMap(OPT_MIN_YEAR);
confSym.dates.monthEnd = pops.GetIntFromMap(OPT_MAX_MONTH);
confSym.dates.yearEnd = pops.GetIntFromMap(OPT_MAX_YEAR);
confSym.symbol = pops.GetStrFromMap(OPT_SYMBOL);
confSym.period = pops.GetStrFromMap(OPT_PERIOD);

//auto pluginName = pops.GetStrFromMap (OPT_PLUGIN);

CLIResult res(confSym);

return EnjoLib::Result<CLIResult>(CLIResult(confSym), true);
}
24 changes: 24 additions & 0 deletions src/lib-base/src/CLI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CLI_H
#define CLI_H

#include <Util/Str.hpp>
#include <Util/Result.hpp>

struct CLIResult;

class CLI
{
public:
CLI();
virtual ~CLI();

EnjoLib::Result<CLIResult> GetConfigs(int argc, char ** argv) const;

virtual EnjoLib::Str GetAdditionalHelp() const = 0;

protected:

private:
};

#endif // CLI_H
1 change: 1 addition & 0 deletions src/lib-base/src/CLIResult.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "CLIResult.h"
18 changes: 18 additions & 0 deletions src/lib-base/src/CLIResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CLIRESULT_H
#define CLIRESULT_H

#include "ConfigSym.h"

struct CLIResult
{
CLIResult(const ConfigSym & confSym)
: m_confSym(confSym)
{

}

ConfigSym m_confSym;

};

#endif // CLIRESULT_H
7 changes: 7 additions & 0 deletions src/lib-base/src/ConfigSym.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ void ConfigSym::RegisterAndReadStrs(EnjoLib::Istream & f)
RegisterAndReadStr(f, period, "h8");
}

void ConfigSym::UpdateFromOther(const ConfigSym & cfgSymCmdLine)
{
dates.UpdateIfNot0(cfgSymCmdLine.dates);
if (cfgSymCmdLine.symbol.size()) symbol = cfgSymCmdLine.symbol;
if (cfgSymCmdLine.period.size()) period = cfgSymCmdLine.period;
}

/*
void ConfigSym::SetStandardVals()
{
Expand Down
2 changes: 2 additions & 0 deletions src/lib-base/src/ConfigSym.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ConfigSym : public ConfigBase
void RegisterAndReadInts(EnjoLib::Istream & ifs) override;
void RegisterAndReadFloats(EnjoLib::Istream & ifs) override;
void RegisterAndReadStrs(EnjoLib::Istream & ifs) override;

void UpdateFromOther(const ConfigSym & cfgSymCmdLine);

//void SetStandardVals();
bool ShiftRange(int shift);
Expand Down
23 changes: 23 additions & 0 deletions src/tsqsim-lib/src/CLITsq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "CLITsq.h"
#include "TSXformTypeString.h"

#include <Ios/Osstream.hpp>
#include <Template/Array.hpp>

using namespace EnjoLib;

CLITsq::CLITsq(){}
CLITsq::~CLITsq(){}

EnjoLib::Str CLITsq::GetAdditionalHelp() const
{
Osstream oss;
const TSXformTypeString types;
oss << "Available transformations for text-based scripting:\n";
for (const Str & name : types.GetNames())
{
oss << name << " ";
}

return oss.str();
}
24 changes: 24 additions & 0 deletions src/tsqsim-lib/src/CLITsq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CLITSQ_H
#define CLITSQ_H

#include "CLI.h"

#include <Util/Str.hpp>
#include <Util/Result.hpp>

struct CLIResult;

class CLITsq : public CLI
{
public:
CLITsq();
virtual ~CLITsq();

EnjoLib::Str GetAdditionalHelp() const override;

protected:

private:
};

#endif // CLITSQ_H
13 changes: 12 additions & 1 deletion src/tsqsim-qt/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "MyMainWindow.h"
#include "Monster.h"
#include "CLITsq.h"
#include "CLIResult.h"
#include <StrategyFactoryDummy.h>
#include <SymbolFactoryClean.h>
#include <ConfigMan.h>
Expand All @@ -50,7 +52,15 @@ int main(int argc, char *argv[])
int mode = 0;
if (argc > 1)
{
mode = CharManipulations().ToInt(argv[1]);
/// TODO: Restore?
//mode = CharManipulations().ToInt(argv[1]);
}
// Experiment:
const CLITsq cli;
const EnjoLib::Result<CLIResult> & result = cli.GetConfigs(argc, argv);
if (not result.isSuccess)
{
return 0;
}
//screenShot = true;
const EnjoLib::Str screenShotOutDir = argc > 2 ? argv[2] : "";
Expand Down Expand Up @@ -88,6 +98,7 @@ int main(int argc, char *argv[])
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
QApplication::setGraphicsSystem("raster");
#endif
/// TODO: Check it it's useful:
QApplication a(argc, argv);

//cout << "mode = " << mode << "\nscreenShotOutDir = " << screenShotOutDir << "\nscreenShotSymbol = " << screenShotSymbol << "\nscreenShotPeriod = " << screenShotPeriod << "\ndataInputFileName = " << dataInputFileName << endl;
Expand Down
1 change: 1 addition & 0 deletions src/tsqsim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enjoSetupTarget(${PROJECT_NAME})
tsqSetupSymlinks(${PROJECT_NAME})

# TODO: Rather copy than symlink?
# TODO: Move scripts to tsqsim-lib
enjoSymlink(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/scripts ${CMAKE_BINARY_DIR}/../scripts)

target_link_libraries(${PROJECT_NAME} tsqsim-lib)
Expand Down
14 changes: 4 additions & 10 deletions src/tsqsim/src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
#include <Util/CoutBuf.hpp>

#include <Util/Trim.hpp>
#include <STD/Iostream.hpp>
#include <STD/Iostream.hpp> /// TODO: Remove

#include <STD/VectorCpp.hpp>

using namespace std;
using namespace EnjoLib;
Expand All @@ -39,7 +38,7 @@ void App::Run(const ConfigSym & confSymCmdLine) const
const ConfigTF2 & confTF2 = *gcfgMan.cfgTF2.get();
ConfigSym & confSym = *gcfgMan.cfgSym.get();

UpdateCfgSym(confSymCmdLine, confSym);
confSym.UpdateFromOther(confSymCmdLine);

VecStr symbols = {confSym.symbol};
VecStr periods = {confSym.period};
Expand Down Expand Up @@ -69,8 +68,10 @@ void App::Run(const ConfigSym & confSymCmdLine) const

if (confTF.REPEAT)
{
/// TODO: Log::WriteLine("q to quit"); in CoutBuf
cout << "q to quit" << endl;
EnjoLib::Str line;
/// TODO: GetLine in Ios wrapper
getline(cin, line.strRW());
line = EnjoLib::Trim().trim(line);
if (line == "q")
Expand All @@ -80,10 +81,3 @@ void App::Run(const ConfigSym & confSymCmdLine) const
} while (confTF.REPEAT);
//LOGL << "Plugin name = " << plugin << Nl;
}

void App::UpdateCfgSym(const ConfigSym & cfgSymCmdLine, ConfigSym & cfgFile) const
{
cfgFile.dates.UpdateIfNot0(cfgSymCmdLine.dates);
if (cfgSymCmdLine.symbol.size()) cfgFile.symbol = cfgSymCmdLine.symbol;
if (cfgSymCmdLine.period.size()) cfgFile.period = cfgSymCmdLine.period;
}
1 change: 0 additions & 1 deletion src/tsqsim/src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class App
void Run(const ConfigSym & cfgSymCmdLine) const;

protected:
void UpdateCfgSym(const ConfigSym & cfgSymCmdLine, ConfigSym & cfgFile) const;

private:
};
Expand Down
75 changes: 7 additions & 68 deletions src/tsqsim/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#include "App.h"

#include "TSXformTypeString.h"
#include "ConfigSym.h"
#include "ConfigTS.h"
#include "CLITsq.h"
#include "CLIResult.h"

#include <EnjoLibBoost/ProgramOptions.hpp>

#include <Math/GeneralMath.hpp>
#include <Util/Str.hpp>
#include <Util/CoutBuf.hpp>
#include <Util/StrColour.hpp>
#include <Template/Array.hpp>

#include <exception>

Expand All @@ -19,71 +13,16 @@ using namespace EnjoLib;

int main(int argc, char ** argv)
{
//const char * OPT_PLUGIN = "plugin";

const char * OPT_MIN_MONTH = "min-month";
const char * OPT_MIN_YEAR = "min-year";
const char * OPT_MAX_MONTH = "max-month";
const char * OPT_MAX_YEAR = "max-year";
//const char * OPT_LATEST_DATE = "latest-date";
const char * OPT_SYMBOL = "sym";
const char * OPT_PERIOD = "per";

try
{
ProgramOptionsState popState;
////popState.AddStr(OPT_PLUGIN, "Plugin name");
popState.AddInt(OPT_MIN_MONTH, "Start month"); /// TODO: Option Add(char*, char*, int & refVal);. A new class beside the old one or replace the old one?
popState.AddInt(OPT_MIN_YEAR, "Start year");
popState.AddInt(OPT_MAX_MONTH, "End month");
popState.AddInt(OPT_MAX_YEAR, "End year");
popState.AddStr(OPT_SYMBOL, "Symbol name");
popState.AddStr(OPT_PERIOD, "Period name");

popState.ReadArgs(argc, argv);
const ProgramOptions pops(popState);

if (pops.IsHelpRequested())
const CLITsq cli;
const EnjoLib::Result<CLIResult> & result = cli.GetConfigs(argc, argv);

if (not result.isSuccess)
{
/// TODO: Try also to react also on -h by adding here:
/**
bool ProgramOptions::IsHelpRequested() const
{
return GetBoolFromMap(ProgramOptionsState::OPT_HELP);
}
return GetBoolFromMap(ProgramOptionsState::OPT_HELP || return GetBoolFromMap(ProgramOptionsState::OPT_HELP_MIN););
const char * OPT_HELP_MIN = "h";
*/

const TSXformTypeString types;
ELO
LOG << pops.GetHelp() << Nl;
LOG << "Available transformations for text-based scripting:\n";
for (const Str & name : types.GetNames())
{
LOG << name << " ";
}
LOG << Nl;
LOG << "The default path of the script is: " << ConfigTS::DEFAULT_SCRIPT_FILE_NAME << Nl;
LOG << "\n(C) 2012-2022 mj-xmr. Licensed under AGPLv3.\n";
LOG << "https://github.com/mj-xmr/tsqsim\n";
LOG << "\nRemember to be happy.\n\n";
return 0;
}

ConfigSym confSym;
confSym.dates.Set0();

confSym.dates.monthStart = pops.GetIntFromMap(OPT_MIN_MONTH);
confSym.dates.yearStart = pops.GetIntFromMap(OPT_MIN_YEAR);
confSym.dates.monthEnd = pops.GetIntFromMap(OPT_MAX_MONTH);
confSym.dates.yearEnd = pops.GetIntFromMap(OPT_MAX_YEAR);
confSym.symbol = pops.GetStrFromMap(OPT_SYMBOL);
confSym.period = pops.GetStrFromMap(OPT_PERIOD);


//auto pluginName = pops.GetStrFromMap (OPT_PLUGIN);
App().Run(confSym);
App().Run(result.value.m_confSym);
}
catch (exception & ex)
{
Expand Down

0 comments on commit ee6a131

Please sign in to comment.