Skip to content

Commit

Permalink
Object oriented command line argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mhtvsSFrpHdE committed Jan 18, 2023
1 parent 8480b8b commit d5eaa03
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 143 deletions.
3 changes: 3 additions & 0 deletions qpp/prefetch/Source/Core/const_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ namespace Const_Core
// Argi: index
namespace Arg
{
// Assume index 0 is application.exe path
const int ArgBeginIndex = 1;
const int IniArgc = 2;
const int IniArgi = 1;
const QString BoolFlagBeginWith = "-";
const QString SkipStartup = "skipStartup";
const QString ShowMainWindow = "showMainWindow";
}
Expand Down
32 changes: 4 additions & 28 deletions qpp/prefetch/Source/Core/scan_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include "..\Setting\setting.h"
#include "const_core.h"
#include "ReadFile\read_file.h"
#include "..\Global\global.h"

QSettings *ScanCache::cache = NULL;
QString ScanCache::cacheFilePath;
QString ScanCache::cacheFilePath = NULL;
bool ScanCache::cacheFileExist = false;

bool ScanCache::init_cacheFileExist()
Expand All @@ -17,38 +18,13 @@ bool ScanCache::init_cacheFileExist()
return cacheFile.exists();
}

Setting::GetGenericResult<QString> init_getCacheFilePath(int argc, QStringList argv)
{
using namespace Const_Core;

Setting::GetGenericResult<QString> getCacheFilePath;

if (argc < Arg::IniArgc)
{
getCacheFilePath.success = false;
return getCacheFilePath;
}

auto settingFile = QFileInfo(argv[1]);
QString cacheFileName = settingFile.baseName() + Const_Cache::DefaultCacheFilePathSuffix;
auto cacheFile = QFileInfo(cacheFileName);
ScanCache::cacheFileExist = cacheFile.exists();

getCacheFilePath.result = cacheFileName;
return getCacheFilePath;
}

void ScanCache::init(int argc, QStringList argv)
void ScanCache::init()
{
// Cache file path default value
cacheFilePath = Const_Cache::DefaultCacheFilePath;

// Get cache file path
auto getCacheFilePath = init_getCacheFilePath(argc, argv);
if (getCacheFilePath.success)
{
cacheFilePath = getCacheFilePath.result;
}
cacheFilePath = Global::commandLineArgumentAddress->getScanCacheFilePath();
cacheFilePath = QApplication::applicationDirPath() + Const_Cache::PathSplitter + cacheFilePath;
cacheFileExist = init_cacheFileExist();

Expand Down
2 changes: 1 addition & 1 deletion qpp/prefetch/Source/Core/scan_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ScanCache
// Init code: check cache file exist
static bool init_cacheFileExist();
// Any init code
static void init(int argc, QStringList argv);
static void init();

// Snapshot current thread pool to cache file
static void saveScanCache(QList<QRunnable *> *readThreadQueueAddress, bool override = false);
Expand Down
6 changes: 4 additions & 2 deletions qpp/prefetch/Source/Core/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ using namespace Const_Setting::ConfigGroupName;

void (*Startup::startOnce)() = &_startOnce;

void Startup::init(int argc, QStringList argv)
void Startup::init()
{
using namespace Const_Core::Arg;

if (argv.contains(SkipStartup))
bool skipStartup = Global::commandLineArgumentAddress->getSkipStartup();

if (skipStartup)
{
startOnce_remove();
return;
Expand Down
2 changes: 1 addition & 1 deletion qpp/prefetch/Source/Core/startup.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Startup
{
public:
static void init(int argc, QStringList argv);
static void init();

// This function will only run once
static void (*startOnce)();
Expand Down
18 changes: 12 additions & 6 deletions qpp/prefetch/Source/Global/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ QApplication *Global::qGuiApplication = NULL;
MainWindow *Global::qMainWindow = NULL;
InputLoopThread *Global::inputLoopThreadAddress = NULL;
ReadFile *Global::readFileLoopThreadAddress = NULL;
TrayIcon *Global::trayIconInstanceAddress = new TrayIcon();
TrayIcon *Global::trayIconInstanceAddress = NULL;
CommandLineArgument *Global::commandLineArgumentAddress = NULL;

void Global::init(int argc, char *argv[])
{
Expand All @@ -27,20 +28,24 @@ void Global::init(int argc, char *argv[])

// QApplication
qGuiApplication = new QApplication(argc, argv);
auto commandLineArguments = QApplication::arguments();

// Command line argument
auto argvQStringList = QApplication::arguments();

#if LOG_ENABLED
Log::init(argc, commandLineArguments);
Log::init(argc, argvQStringList);
#endif
LAST_KNOWN_POSITION(0)

commandLineArgumentAddress = new CommandLineArgument(argc, argvQStringList);

StdIn::init();
LAST_KNOWN_POSITION(2)

StdOut::init();
LAST_KNOWN_POSITION(2)

Setting::init(argc, commandLineArguments);
Setting::init();
LAST_KNOWN_POSITION(2)

#if TRANSLATE_ENABLED
Expand All @@ -50,10 +55,10 @@ void Global::init(int argc, char *argv[])
LAST_KNOWN_POSITION(2)
#endif

Startup::init(argc, commandLineArguments);
Startup::init();
LAST_KNOWN_POSITION(2)

ScanCache::init(argc, commandLineArguments);
ScanCache::init();
LAST_KNOWN_POSITION(2)

Dpi::init();
Expand Down Expand Up @@ -84,6 +89,7 @@ void Global::init(int argc, char *argv[])
LAST_KNOWN_POSITION(2)

// Tray icon
trayIconInstanceAddress = new TrayIcon();
trayIconInstanceAddress->init();
LAST_KNOWN_POSITION(2)

Expand Down
3 changes: 3 additions & 0 deletions qpp/prefetch/Source/Global/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "..\Core\ReadFile\read_file.h"
#include "..\Interface\TrayIcon\tray_icon.h"
#include "..\Interface\MainWindow\mainwindow.h"
#include "..\Input\command_line_argument.h"

class Global
{
Expand All @@ -21,6 +22,8 @@ class Global

static TrayIcon *trayIconInstanceAddress;

static CommandLineArgument *commandLineArgumentAddress;

// Any init code
static void init(int argc, char *argv[]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
#include "..\..\input_loop_thread.h"
#include "..\..\..\..\Input\const_input.h"
#include "..\..\..\..\Global\const_global.h"
#include "..\..\..\..\Core\const_core.h"
#include "..\..\..\..\Output\stdout.h"
#include "..\..\..\..\Core\scan_cache.h"
#include "..\..\..\..\Core\ReadFile\read_file.h"
#include "..\..\..\..\Global\global.h"
#include "..\..\..\..\Setting\setting.h"

#define Parent InputLoopThread::ConsoleCommandFunction_Level1

void Parent::expiresc()
{
using namespace Const_Input;
using namespace Const_Input::Message;
using namespace Const_Global::CommonString;
using namespace Const_Core::Arg;

StdOut::printLine(TryingToExpireScanCache);

Expand All @@ -37,83 +32,15 @@ void Parent::expiresc()

// Open self again

// Get exist argument
// <.exe> <.ini> <other flag>
auto selfArgumentsStringList = Global::qGuiApplication->arguments();

// Get exe
// <.exe>
auto selfExePath = QFileInfo(selfArgumentsStringList[0]).absoluteFilePath();

// Add exe quote if not already
// "<.exe>"
bool quotedSelfExePath = selfExePath.startsWith(Quote);
if (!quotedSelfExePath)
{
selfExePath = Quote + selfExePath + Quote;
}

// Get ini
// <.ini>
// Remove quote if exist
auto settingFileName = QFileInfo(Setting::settingFilePath).fileName();
settingFileName.replace(Quote, EmptyString);

// Collect search result
bool settingFileNotInArgv = true;
bool skipStartupNotInArgv = true;
bool showMainWindowNotInArgv = true;
// Search start from 2nd element
QListIterator<QString> iterator(selfArgumentsStringList);
iterator.next();
while (iterator.hasNext())
{
auto value = iterator.next();

// Confirm <.ini>
if (value.contains(settingFileName))
{

settingFileNotInArgv = false;
}

// Confirm <other flag>
if (value == SkipStartup)
{
skipStartupNotInArgv = false;
}
if (value == ShowMainWindow)
{
showMainWindowNotInArgv = false;
}
}

// Add necessary arguments if not exist
if (settingFileNotInArgv)
{
// "<.ini>"
auto settingFilePath_arguments = Quote + settingFileName + Quote;
selfArgumentsStringList.insert(IniArgi, settingFilePath_arguments);
}
if (skipStartupNotInArgv)
{
// <other flag>
selfArgumentsStringList.append(SkipStartup);
}
if (showMainWindowNotInArgv)
{
selfArgumentsStringList.append(ShowMainWindow);
}

// Remove <.exe>
// Join rest element
selfArgumentsStringList.removeFirst();
// Add necessary arguments
Global::commandLineArgumentAddress->setSkipStartup(true);
Global::commandLineArgumentAddress->setShowMainWindow(true);

// Send run quiet command
// `run `
// + `"<.exe>"`
// + `"<.ini>" <other flag>`
auto self_command = Command_Level2::run_withSplitter + Quote + selfExePath + Quote + Space + selfArgumentsStringList.join(Space);
auto self_command = Global::commandLineArgumentAddress->toString();
Global::inputLoopThreadAddress->receiveText(self_command);

// Exit
Expand Down
97 changes: 97 additions & 0 deletions qpp/prefetch/Source/Input/cli_constructor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <QFileInfo>

#include "command_line_argument.h"
#include "..\Core\const_core.h"
#include "..\Setting\const_setting.h"

void CommandLineArgument::CommandLineArgument_parseSettingFileName(int *argIndex, QStringList *argv)
{
using namespace Const_Core;

{
// Assume this argument is the first argument, and not start with "-"
bool firstArgumentStartWithMinus = (*argv)[Arg::IniArgi].startsWith(Arg::BoolFlagBeginWith);

// If first argument start with "-", then setting file name is not provided
if (firstArgumentStartWithMinus)
{
// In this case, use default value
settingFilePath = Const_Setting::DefaultSettingFilePath;
scanCacheFilePath = Const_Cache::DefaultCacheFilePath;
return;
}
}

{
// Setting file name founded, other start from 2nd
*argIndex = *argIndex + 1;

// Save
settingFilePath = (*argv)[Arg::IniArgi];
}

{
// Generate scan cache file name together
auto settingFile = QFileInfo(settingFilePath);
scanCacheFilePath = settingFile.baseName() + Const_Cache::DefaultCacheFilePathSuffix;
}
}

bool CommandLineArgument_parseBoolFlag(QString match, int argIndex, QStringList *argv)
{
auto arg = (*argv)[argIndex];
return arg.contains(match);
}

void CommandLineArgument::CommandLineArgument_parseStartup(int argIndex, QStringList *argv)
{
using namespace Const_Core::Arg;

bool get = CommandLineArgument_parseBoolFlag(SkipStartup, argIndex, argv);
if (get)
{
skipStartup = true;
}
}

void CommandLineArgument::CommandLineArgument_parseShowMainWindow(int argIndex, QStringList *argv)
{
using namespace Const_Core::Arg;

bool get = CommandLineArgument_parseBoolFlag(ShowMainWindow, argIndex, argv);
if (get)
{
showMainWindow = true;
}
}

CommandLineArgument::CommandLineArgument(int argc, QStringList argv)
{
using namespace Const_Core;

// Create loop index
// This variable may modify by function before loop
int argIndex = Arg::ArgBeginIndex;

// Not found any possible argument
if (argc < Arg::IniArgc)
{
// Put default value
settingFilePath = Const_Setting::DefaultSettingFilePath;
scanCacheFilePath = Const_Cache::DefaultCacheFilePath;
skipStartup = false;
showMainWindow = false;

// Terminate
return;
}

CommandLineArgument_parseSettingFileName(&argIndex, &argv);

// Parse other argument
for (; argIndex < argc; argIndex++)
{
CommandLineArgument_parseStartup(argIndex, &argv);
CommandLineArgument_parseShowMainWindow(argIndex, &argv);
}
}
Loading

0 comments on commit d5eaa03

Please sign in to comment.