Skip to content

Commit

Permalink
🔧 Added class for parsing command line parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherFoxGuy committed May 26, 2022
1 parent b7a0d4c commit 1cfe8e1
Show file tree
Hide file tree
Showing 9 changed files with 2,144 additions and 33 deletions.
959 changes: 959 additions & 0 deletions external/header_only/SimpleGlob.h

Large diffs are not rendered by default.

1,046 changes: 1,046 additions & 0 deletions external/header_only/SimpleOpt.h

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(SOURCE_FILES
util/SignalMediator.hxx
util/Singleton.hxx
util/Meta.hxx
util/ParseCli.{hxx,cxx}
util/Exception.{hxx,cxx}
util/OSystem.{hxx,cxx}
services/Randomizer.{hxx,cxx}
Expand Down
11 changes: 7 additions & 4 deletions src/Game.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,21 @@ void Game::quit()
#endif // USE_AUDIO
}

bool Game::initialize(const char *videoDriver)
bool Game::initialize()
{
if (SDL_Init(0) != 0)
{
LOG(LOG_ERROR) << "Failed to Init SDL";
LOG(LOG_ERROR) << "SDL Error: " << SDL_GetError();
return false;
}
const char *vd = nullptr;
if(Settings::instance().videoDriver != "Default")
vd = Settings::instance().videoDriver.c_str();

if (SDL_VideoInit(videoDriver) != 0)
if (SDL_VideoInit(vd) != 0)
{
LOG(LOG_ERROR) << "Unknown video driver " << videoDriver;
LOG(LOG_ERROR) << "Unknown video driver " << vd;
int nbDriver = SDL_GetNumRenderDrivers();
for (int i = 0; i < nbDriver; i++)
{
Expand Down Expand Up @@ -282,7 +285,7 @@ bool Game::mainMenu()
return quitGame;
}

void Game::run(bool SkipMenu)
void Game::run()
{
LOG(LOG_INFO) << VERSION;

Expand Down
4 changes: 2 additions & 2 deletions src/Game.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public:
/** @brief starts setting up the game
* starts game initialization.
*/
virtual bool initialize(const char *videoDriver);
virtual bool initialize();

/** @brief begins the game
* starts running the game
* @param SkipMenu if the main menu should be skipped or not
*/
virtual void run(bool SkipMenu = false);
virtual void run();

/** @brief ends the game
* shuts down the game
Expand Down
10 changes: 10 additions & 0 deletions src/engine/basics/Settings.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ struct SettingsData

/// Write errors to a log file
bool writeErrorLogFile;

// ==================================
// Command line options
// ==================================

/// Sets a different video driver
std::string videoDriver = "Default";

bool skipMenu = false;
bool quitGame = false;
};

/**
Expand Down
38 changes: 11 additions & 27 deletions src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,35 @@
#include "Game.hxx"
#include "Exception.hxx"
#include "LOG.hxx"
#include "SimpleOpt.h"
#include "ParseCli.hxx"

int protected_main(int argc, char **argv)
{
(void)argc;
(void)argv;

bool quitGame = false;

// add commandline parameter to skipMenu
auto has_args = [argv, argc](const std::string &param)
{
for (int i = 1; i < argc; ++i)
if (param == argv[i])
return i;

LOG(LOG_DEBUG) << "Unknown game option " << param;
return 0;
};

bool skipMenu = has_args("--skipMenu");
uint32_t videoOpt = has_args("--video");
const char *videoDriver = nullptr;
if (videoOpt)
{
videoDriver = argv[videoOpt + 1];
}
ParseCli cli;
if (!cli.ProcessCommandLine(argc,argv))
return EXIT_FAILURE;

LOG(LOG_DEBUG) << "Launching Cytopia";

Cytopia::Game game;

LOG(LOG_DEBUG) << "Initializing Cytopia";

if (!game.initialize(videoDriver))
if (!game.initialize())
return EXIT_FAILURE;

if (!skipMenu)
bool quitGame = Settings::instance().quitGame;

if (!Settings::instance().skipMenu)
{
quitGame = game.mainMenu();
}

if (!quitGame)
{
LOG(LOG_DEBUG) << "Running the Game";
game.run(skipMenu);
game.run();
}

LOG(LOG_DEBUG) << "Closing the Game";
Expand All @@ -74,4 +58,4 @@ int main(int argc, char **argv)
}

return EXIT_FAILURE;
}
}
90 changes: 90 additions & 0 deletions src/util/ParseCli.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "ParseCli.hxx"
#include "LOG.hxx"
#include "Singleton.hxx"
#include "Settings.hxx"

// option identifiers
enum
{
OPT_HELP,
OPT_SKIPMENU,
OPT_VIDEODRIVER
};

// option array
CSimpleOpt::SOption cmdline_options[] = {{OPT_SKIPMENU, ("--skipMenu"), SO_NONE},
{OPT_VIDEODRIVER, ("--video"), SO_REQ_SEP},
{OPT_HELP, ("--help"), SO_NONE},
{OPT_HELP, ("-h"), SO_NONE},
SO_END_OF_OPTIONS};

bool ParseCli::ProcessCommandLine(int argc, char *argv[])
{
CSimpleOpt args(argc, argv, cmdline_options);
bool success = true;
while (args.Next())
{
if (args.LastError() != SO_SUCCESS)
{
LOG(LOG_ERROR) << GetLastErrorText(args.LastError()) << " " << args.OptionText();
success = false;
}

switch (args.OptionId())
{
case OPT_HELP:
ShowUsage();
return false;
case OPT_SKIPMENU:
Settings::instance().skipMenu = true;
break;
case OPT_VIDEODRIVER:
if (args.OptionArg())
{
Settings::instance().videoDriver = args.OptionArg();
}
else
{
LOG(LOG_ERROR) << "videoDriver not set";
ShowUsage();
return false;
}
break;
default:
ShowUsage();
}
}

return success;
}

void ParseCli::ShowUsage()
{
LOG(LOG_INFO) << "Usage: Cytopia [OPTIONS]";
LOG(LOG_INFO) << "\t--help (this)";
LOG(LOG_INFO) << "\t--skipMenu (Skips the main menu)";
LOG(LOG_INFO) << "\t--video <videoDriver> (Sets a different video driver)";
}

std::string ParseCli::GetLastErrorText(int a_nError)
{
switch (a_nError)
{
case SO_SUCCESS:
return "Success";
case SO_OPT_INVALID:
return "Unrecognized option";
case SO_OPT_MULTIPLE:
return "Option matched multiple strings";
case SO_ARG_INVALID:
return "Option does not accept argument";
case SO_ARG_INVALID_TYPE:
return "Invalid argument format";
case SO_ARG_MISSING:
return "Required argument is missing";
case SO_ARG_INVALID_DATA:
return "Invalid argument data";
default:
return "Unknown error";
}
}
18 changes: 18 additions & 0 deletions src/util/ParseCli.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CYTOPIA_PARSECLI_HXX
#define CYTOPIA_PARSECLI_HXX

#include "SimpleOpt.h"
#include <string>

class ParseCli
{
public:
bool ProcessCommandLine(int argc, char **argv);

private:
std::string GetLastErrorText(int a_nError);

void ShowUsage();
};

#endif //CYTOPIA_PARSECLI_HXX

0 comments on commit 1cfe8e1

Please sign in to comment.