Skip to content

Commit 98db48b

Browse files
committed
🔧 Added class for parsing command line parameters
1 parent 9da56a2 commit 98db48b

File tree

9 files changed

+2139
-30
lines changed

9 files changed

+2139
-30
lines changed

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
@@ -13,6 +13,7 @@ set(SOURCE_FILES
1313
util/SignalMediator.hxx
1414
util/Singleton.hxx
1515
util/Meta.hxx
16+
util/ParseCli.{hxx,cxx}
1617
util/Exception.{hxx,cxx}
1718
util/OSystem.{hxx,cxx}
1819
services/Randomizer.{hxx,cxx}

src/Game.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void Game::initialize()
7979
LOG(LOG_DEBUG) << "Initialized Game Object";
8080
}
8181

82-
void Game::run(bool SkipMenu)
82+
void Game::run()
8383
{
8484
Camera::instance().centerScreenOnMapCenter();
8585

src/Game.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public:
4444
* @details starts running the game
4545
* @param SkipMenu if the main menu should be skipped or not
4646
*/
47-
virtual void run(bool SkipMenu = false);
47+
virtual void run();
4848

4949
/// ends the game
5050
virtual void shutdown();

src/engine/basics/Settings.hxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ 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;
169178
};
170179

171180
/**

src/main.cxx

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
#include "LOG.hxx"
77
#include "engine/WindowManager.hxx"
88
#include <UIManager.hxx>
9+
#include "SimpleOpt.h"
10+
#include "ParseCli.hxx"
911

1012
#include <SDL.h>
1113
#include <SDL_ttf.h>
1214

13-
bool initialize(const char *videoDriver)
15+
bool initialize()
1416
{
1517
if (SDL_Init(0) != 0)
1618
{
@@ -19,6 +21,10 @@ bool initialize(const char *videoDriver)
1921
return false;
2022
}
2123

24+
const char *videoDriver = nullptr;
25+
if (Settings::instance().videoDriver != "Default")
26+
videoDriver = Settings::instance().videoDriver.c_str();
27+
2228
if (SDL_VideoInit(videoDriver) != 0)
2329
{
2430
LOG(LOG_ERROR) << "Unknown video driver " << videoDriver;
@@ -47,51 +53,31 @@ bool initialize(const char *videoDriver)
4753

4854
int protected_main(int argc, char **argv)
4955
{
50-
(void)argc;
51-
(void)argv;
52-
53-
LOG(LOG_INFO) << VERSION;
54-
55-
// add commandline parameter to skipMenu
56-
auto has_args = [argv, argc](const std::string &param)
57-
{
58-
for (int i = 1; i < argc; ++i)
59-
if (param == argv[i])
60-
return i;
61-
62-
LOG(LOG_DEBUG) << "Unknown game option " << param;
63-
return 0;
64-
};
65-
66-
bool skipMenu = has_args("--skipMenu");
67-
uint32_t videoOpt = has_args("--video");
68-
const char *videoDriver = nullptr;
69-
if (videoOpt)
70-
{
71-
videoDriver = argv[videoOpt + 1];
72-
}
56+
ParseCli cli;
57+
if (!cli.ProcessCommandLine(argc, argv))
58+
return EXIT_FAILURE;
7359

7460
LOG(LOG_DEBUG) << "Launching Cytopia";
7561

7662
Cytopia::Game game;
7763

7864
LOG(LOG_DEBUG) << "Initializing Cytopia";
7965

80-
if (!initialize(videoDriver))
66+
if (!initialize())
8167
return EXIT_FAILURE;
8268
else
8369
LOG(LOG_DEBUG) << "DONE Cytopia";
8470

8571
bool startGame = true;
86-
if (!skipMenu)
72+
if (!Settings::instance().skipMenu)
8773
{
8874
startGame = mainMenu();
8975
}
9076

9177
if (startGame)
9278
{
9379
LOG(LOG_DEBUG) << "Running the Game";
94-
game.run(skipMenu);
80+
game.run();
9581
}
9682

9783
LOG(LOG_DEBUG) << "Closing the Game";
@@ -118,4 +104,4 @@ int main(int argc, char **argv)
118104
}
119105

120106
return EXIT_FAILURE;
121-
}
107+
}

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)