Skip to content

Commit ee6a131

Browse files
authored
CLI extraction concept (#14)
1 parent 1435370 commit ee6a131

File tree

13 files changed

+200
-80
lines changed

13 files changed

+200
-80
lines changed

src/lib-base/src/CLI.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "CLI.h"
2+
#include "CLIResult.h"
3+
#include "ConfigTS.h"
4+
//#include "TSXformTypeString.h"
5+
#include <Util/CoutBuf.hpp>
6+
7+
#include <EnjoLibBoost/ProgramOptions.hpp>
8+
9+
using namespace EnjoLib;
10+
11+
CLI::CLI(){}
12+
CLI::~CLI(){}
13+
14+
EnjoLib::Result<CLIResult> CLI::GetConfigs(int argc, char ** argv) const
15+
{
16+
//const char * OPT_PLUGIN = "plugin";
17+
18+
const char * OPT_MIN_MONTH = "min-month";
19+
const char * OPT_MIN_YEAR = "min-year";
20+
const char * OPT_MAX_MONTH = "max-month";
21+
const char * OPT_MAX_YEAR = "max-year";
22+
//const char * OPT_LATEST_DATE = "latest-date";
23+
const char * OPT_SYMBOL = "sym";
24+
const char * OPT_PERIOD = "per";
25+
26+
EnjoLib::ProgramOptionsState popState;
27+
////popState.AddStr(OPT_PLUGIN, "Plugin name");
28+
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?
29+
popState.AddInt(OPT_MIN_YEAR, "Start year");
30+
popState.AddInt(OPT_MAX_MONTH, "End month");
31+
popState.AddInt(OPT_MAX_YEAR, "End year");
32+
popState.AddStr(OPT_SYMBOL, "Symbol name");
33+
popState.AddStr(OPT_PERIOD, "Period name");
34+
35+
popState.ReadArgs(argc, argv);
36+
const EnjoLib::ProgramOptions pops(popState);
37+
38+
ConfigSym confSym;
39+
40+
if (pops.IsHelpRequested())
41+
{
42+
/// TODO: Try also to react also on -h by adding here:
43+
/**
44+
bool ProgramOptions::IsHelpRequested() const
45+
{
46+
return GetBoolFromMap(ProgramOptionsState::OPT_HELP);
47+
}
48+
return GetBoolFromMap(ProgramOptionsState::OPT_HELP || return GetBoolFromMap(ProgramOptionsState::OPT_HELP_MIN););
49+
const char * OPT_HELP_MIN = "h";
50+
*/
51+
ELO
52+
LOG << pops.GetHelp() << Nl;
53+
54+
LOG << GetAdditionalHelp();
55+
LOG << Nl;
56+
LOG << "The default path of the script is: " << ConfigTS::DEFAULT_SCRIPT_FILE_NAME << Nl;
57+
LOG << "\n(C) 2012-2022 mj-xmr. Licensed under AGPLv3.\n";
58+
LOG << "https://github.com/mj-xmr/tsqsim\n";
59+
LOG << "\nRemember to be happy.\n\n";
60+
return EnjoLib::Result<CLIResult>(CLIResult(confSym), false);
61+
}
62+
63+
confSym.dates.Set0();
64+
65+
confSym.dates.monthStart = pops.GetIntFromMap(OPT_MIN_MONTH);
66+
confSym.dates.yearStart = pops.GetIntFromMap(OPT_MIN_YEAR);
67+
confSym.dates.monthEnd = pops.GetIntFromMap(OPT_MAX_MONTH);
68+
confSym.dates.yearEnd = pops.GetIntFromMap(OPT_MAX_YEAR);
69+
confSym.symbol = pops.GetStrFromMap(OPT_SYMBOL);
70+
confSym.period = pops.GetStrFromMap(OPT_PERIOD);
71+
72+
//auto pluginName = pops.GetStrFromMap (OPT_PLUGIN);
73+
74+
CLIResult res(confSym);
75+
76+
return EnjoLib::Result<CLIResult>(CLIResult(confSym), true);
77+
}

src/lib-base/src/CLI.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef CLI_H
2+
#define CLI_H
3+
4+
#include <Util/Str.hpp>
5+
#include <Util/Result.hpp>
6+
7+
struct CLIResult;
8+
9+
class CLI
10+
{
11+
public:
12+
CLI();
13+
virtual ~CLI();
14+
15+
EnjoLib::Result<CLIResult> GetConfigs(int argc, char ** argv) const;
16+
17+
virtual EnjoLib::Str GetAdditionalHelp() const = 0;
18+
19+
protected:
20+
21+
private:
22+
};
23+
24+
#endif // CLI_H

src/lib-base/src/CLIResult.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "CLIResult.h"

src/lib-base/src/CLIResult.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CLIRESULT_H
2+
#define CLIRESULT_H
3+
4+
#include "ConfigSym.h"
5+
6+
struct CLIResult
7+
{
8+
CLIResult(const ConfigSym & confSym)
9+
: m_confSym(confSym)
10+
{
11+
12+
}
13+
14+
ConfigSym m_confSym;
15+
16+
};
17+
18+
#endif // CLIRESULT_H

src/lib-base/src/ConfigSym.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ void ConfigSym::RegisterAndReadStrs(EnjoLib::Istream & f)
5454
RegisterAndReadStr(f, period, "h8");
5555
}
5656

57+
void ConfigSym::UpdateFromOther(const ConfigSym & cfgSymCmdLine)
58+
{
59+
dates.UpdateIfNot0(cfgSymCmdLine.dates);
60+
if (cfgSymCmdLine.symbol.size()) symbol = cfgSymCmdLine.symbol;
61+
if (cfgSymCmdLine.period.size()) period = cfgSymCmdLine.period;
62+
}
63+
5764
/*
5865
void ConfigSym::SetStandardVals()
5966
{

src/lib-base/src/ConfigSym.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ConfigSym : public ConfigBase
2020
void RegisterAndReadInts(EnjoLib::Istream & ifs) override;
2121
void RegisterAndReadFloats(EnjoLib::Istream & ifs) override;
2222
void RegisterAndReadStrs(EnjoLib::Istream & ifs) override;
23+
24+
void UpdateFromOther(const ConfigSym & cfgSymCmdLine);
2325

2426
//void SetStandardVals();
2527
bool ShiftRange(int shift);

src/tsqsim-lib/src/CLITsq.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "CLITsq.h"
2+
#include "TSXformTypeString.h"
3+
4+
#include <Ios/Osstream.hpp>
5+
#include <Template/Array.hpp>
6+
7+
using namespace EnjoLib;
8+
9+
CLITsq::CLITsq(){}
10+
CLITsq::~CLITsq(){}
11+
12+
EnjoLib::Str CLITsq::GetAdditionalHelp() const
13+
{
14+
Osstream oss;
15+
const TSXformTypeString types;
16+
oss << "Available transformations for text-based scripting:\n";
17+
for (const Str & name : types.GetNames())
18+
{
19+
oss << name << " ";
20+
}
21+
22+
return oss.str();
23+
}

src/tsqsim-lib/src/CLITsq.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef CLITSQ_H
2+
#define CLITSQ_H
3+
4+
#include "CLI.h"
5+
6+
#include <Util/Str.hpp>
7+
#include <Util/Result.hpp>
8+
9+
struct CLIResult;
10+
11+
class CLITsq : public CLI
12+
{
13+
public:
14+
CLITsq();
15+
virtual ~CLITsq();
16+
17+
EnjoLib::Str GetAdditionalHelp() const override;
18+
19+
protected:
20+
21+
private:
22+
};
23+
24+
#endif // CLITSQ_H

src/tsqsim-qt/src/main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "MyMainWindow.h"
2828
#include "Monster.h"
29+
#include "CLITsq.h"
30+
#include "CLIResult.h"
2931
#include <StrategyFactoryDummy.h>
3032
#include <SymbolFactoryClean.h>
3133
#include <ConfigMan.h>
@@ -50,7 +52,15 @@ int main(int argc, char *argv[])
5052
int mode = 0;
5153
if (argc > 1)
5254
{
53-
mode = CharManipulations().ToInt(argv[1]);
55+
/// TODO: Restore?
56+
//mode = CharManipulations().ToInt(argv[1]);
57+
}
58+
// Experiment:
59+
const CLITsq cli;
60+
const EnjoLib::Result<CLIResult> & result = cli.GetConfigs(argc, argv);
61+
if (not result.isSuccess)
62+
{
63+
return 0;
5464
}
5565
//screenShot = true;
5666
const EnjoLib::Str screenShotOutDir = argc > 2 ? argv[2] : "";
@@ -88,6 +98,7 @@ int main(int argc, char *argv[])
8898
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
8999
QApplication::setGraphicsSystem("raster");
90100
#endif
101+
/// TODO: Check it it's useful:
91102
QApplication a(argc, argv);
92103

93104
//cout << "mode = " << mode << "\nscreenShotOutDir = " << screenShotOutDir << "\nscreenShotSymbol = " << screenShotSymbol << "\nscreenShotPeriod = " << screenShotPeriod << "\ndataInputFileName = " << dataInputFileName << endl;

src/tsqsim/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enjoSetupTarget(${PROJECT_NAME})
1111
tsqSetupSymlinks(${PROJECT_NAME})
1212

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

1617
target_link_libraries(${PROJECT_NAME} tsqsim-lib)

0 commit comments

Comments
 (0)