Skip to content

Commit 1cfe8e1

Browse files
committed
🔧 Added class for parsing command line parameters
1 parent b7a0d4c commit 1cfe8e1

9 files changed

Lines changed: 2144 additions & 33 deletions

File tree

external/header_only/SimpleGlob.h

Lines changed: 959 additions & 0 deletions
Large diffs are not rendered by default.

external/header_only/SimpleOpt.h

Lines changed: 1046 additions & 0 deletions
Large diffs are not rendered by default.

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(SOURCE_FILES
1212
util/SignalMediator.hxx
1313
util/Singleton.hxx
1414
util/Meta.hxx
15+
util/ParseCli.{hxx,cxx}
1516
util/Exception.{hxx,cxx}
1617
util/OSystem.{hxx,cxx}
1718
services/Randomizer.{hxx,cxx}

src/Game.cxx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@ void Game::quit()
4747
#endif // USE_AUDIO
4848
}
4949

50-
bool Game::initialize(const char *videoDriver)
50+
bool Game::initialize()
5151
{
5252
if (SDL_Init(0) != 0)
5353
{
5454
LOG(LOG_ERROR) << "Failed to Init SDL";
5555
LOG(LOG_ERROR) << "SDL Error: " << SDL_GetError();
5656
return false;
5757
}
58+
const char *vd = nullptr;
59+
if(Settings::instance().videoDriver != "Default")
60+
vd = Settings::instance().videoDriver.c_str();
5861

59-
if (SDL_VideoInit(videoDriver) != 0)
62+
if (SDL_VideoInit(vd) != 0)
6063
{
61-
LOG(LOG_ERROR) << "Unknown video driver " << videoDriver;
64+
LOG(LOG_ERROR) << "Unknown video driver " << vd;
6265
int nbDriver = SDL_GetNumRenderDrivers();
6366
for (int i = 0; i < nbDriver; i++)
6467
{
@@ -282,7 +285,7 @@ bool Game::mainMenu()
282285
return quitGame;
283286
}
284287

285-
void Game::run(bool SkipMenu)
288+
void Game::run()
286289
{
287290
LOG(LOG_INFO) << VERSION;
288291

src/Game.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public:
4242
/** @brief starts setting up the game
4343
* starts game initialization.
4444
*/
45-
virtual bool initialize(const char *videoDriver);
45+
virtual bool initialize();
4646

4747
/** @brief begins the game
4848
* starts running the game
4949
* @param SkipMenu if the main menu should be skipped or not
5050
*/
51-
virtual void run(bool SkipMenu = false);
51+
virtual void run();
5252

5353
/** @brief ends the game
5454
* shuts down the game

src/engine/basics/Settings.hxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ struct SettingsData
166166

167167
/// Write errors to a log file
168168
bool writeErrorLogFile;
169+
170+
// ==================================
171+
// Command line options
172+
// ==================================
173+
174+
/// Sets a different video driver
175+
std::string videoDriver = "Default";
176+
177+
bool skipMenu = false;
178+
bool quitGame = false;
169179
};
170180

171181
/**

src/main.cxx

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,35 @@
33
#include "Game.hxx"
44
#include "Exception.hxx"
55
#include "LOG.hxx"
6+
#include "SimpleOpt.h"
7+
#include "ParseCli.hxx"
68

79
int protected_main(int argc, char **argv)
810
{
9-
(void)argc;
10-
(void)argv;
11-
12-
bool quitGame = false;
13-
14-
// add commandline parameter to skipMenu
15-
auto has_args = [argv, argc](const std::string &param)
16-
{
17-
for (int i = 1; i < argc; ++i)
18-
if (param == argv[i])
19-
return i;
20-
21-
LOG(LOG_DEBUG) << "Unknown game option " << param;
22-
return 0;
23-
};
24-
25-
bool skipMenu = has_args("--skipMenu");
26-
uint32_t videoOpt = has_args("--video");
27-
const char *videoDriver = nullptr;
28-
if (videoOpt)
29-
{
30-
videoDriver = argv[videoOpt + 1];
31-
}
11+
ParseCli cli;
12+
if (!cli.ProcessCommandLine(argc,argv))
13+
return EXIT_FAILURE;
3214

3315
LOG(LOG_DEBUG) << "Launching Cytopia";
3416

3517
Cytopia::Game game;
3618

3719
LOG(LOG_DEBUG) << "Initializing Cytopia";
3820

39-
if (!game.initialize(videoDriver))
21+
if (!game.initialize())
4022
return EXIT_FAILURE;
4123

42-
if (!skipMenu)
24+
bool quitGame = Settings::instance().quitGame;
25+
26+
if (!Settings::instance().skipMenu)
4327
{
4428
quitGame = game.mainMenu();
4529
}
4630

4731
if (!quitGame)
4832
{
4933
LOG(LOG_DEBUG) << "Running the Game";
50-
game.run(skipMenu);
34+
game.run();
5135
}
5236

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

7660
return EXIT_FAILURE;
77-
}
61+
}

src/util/ParseCli.cxx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "ParseCli.hxx"
2+
#include "LOG.hxx"
3+
#include "Singleton.hxx"
4+
#include "Settings.hxx"
5+
6+
// option identifiers
7+
enum
8+
{
9+
OPT_HELP,
10+
OPT_SKIPMENU,
11+
OPT_VIDEODRIVER
12+
};
13+
14+
// option array
15+
CSimpleOpt::SOption cmdline_options[] = {{OPT_SKIPMENU, ("--skipMenu"), SO_NONE},
16+
{OPT_VIDEODRIVER, ("--video"), SO_REQ_SEP},
17+
{OPT_HELP, ("--help"), SO_NONE},
18+
{OPT_HELP, ("-h"), SO_NONE},
19+
SO_END_OF_OPTIONS};
20+
21+
bool ParseCli::ProcessCommandLine(int argc, char *argv[])
22+
{
23+
CSimpleOpt args(argc, argv, cmdline_options);
24+
bool success = true;
25+
while (args.Next())
26+
{
27+
if (args.LastError() != SO_SUCCESS)
28+
{
29+
LOG(LOG_ERROR) << GetLastErrorText(args.LastError()) << " " << args.OptionText();
30+
success = false;
31+
}
32+
33+
switch (args.OptionId())
34+
{
35+
case OPT_HELP:
36+
ShowUsage();
37+
return false;
38+
case OPT_SKIPMENU:
39+
Settings::instance().skipMenu = true;
40+
break;
41+
case OPT_VIDEODRIVER:
42+
if (args.OptionArg())
43+
{
44+
Settings::instance().videoDriver = args.OptionArg();
45+
}
46+
else
47+
{
48+
LOG(LOG_ERROR) << "videoDriver not set";
49+
ShowUsage();
50+
return false;
51+
}
52+
break;
53+
default:
54+
ShowUsage();
55+
}
56+
}
57+
58+
return success;
59+
}
60+
61+
void ParseCli::ShowUsage()
62+
{
63+
LOG(LOG_INFO) << "Usage: Cytopia [OPTIONS]";
64+
LOG(LOG_INFO) << "\t--help (this)";
65+
LOG(LOG_INFO) << "\t--skipMenu (Skips the main menu)";
66+
LOG(LOG_INFO) << "\t--video <videoDriver> (Sets a different video driver)";
67+
}
68+
69+
std::string ParseCli::GetLastErrorText(int a_nError)
70+
{
71+
switch (a_nError)
72+
{
73+
case SO_SUCCESS:
74+
return "Success";
75+
case SO_OPT_INVALID:
76+
return "Unrecognized option";
77+
case SO_OPT_MULTIPLE:
78+
return "Option matched multiple strings";
79+
case SO_ARG_INVALID:
80+
return "Option does not accept argument";
81+
case SO_ARG_INVALID_TYPE:
82+
return "Invalid argument format";
83+
case SO_ARG_MISSING:
84+
return "Required argument is missing";
85+
case SO_ARG_INVALID_DATA:
86+
return "Invalid argument data";
87+
default:
88+
return "Unknown error";
89+
}
90+
}

src/util/ParseCli.hxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CYTOPIA_PARSECLI_HXX
2+
#define CYTOPIA_PARSECLI_HXX
3+
4+
#include "SimpleOpt.h"
5+
#include <string>
6+
7+
class ParseCli
8+
{
9+
public:
10+
bool ProcessCommandLine(int argc, char **argv);
11+
12+
private:
13+
std::string GetLastErrorText(int a_nError);
14+
15+
void ShowUsage();
16+
};
17+
18+
#endif //CYTOPIA_PARSECLI_HXX

0 commit comments

Comments
 (0)