OXYGENHUB
◆ main openmsx / src / CommandLineParser.hh
5.7 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef COMMANDLINEPARSER_HH
2
#define COMMANDLINEPARSER_HH
3
4
#include "CDImageCLI.hh"
5
#include "CLIOption.hh"
6
#include "CassettePlayerCLI.hh"
7
#include "CliExtension.hh"
8
#include "DiskImageCLI.hh"
9
#include "HDImageCLI.hh"
10
#include "InfoTopic.hh"
11
#include "MSXRomCLI.hh"
12
#include "ReplayCLI.hh"
13
#include "SaveStateCLI.hh"
14
15
#include "components.hh"
16
17
#include <cstdint>
18
#include <initializer_list>
19
#include <memory>
20
#include <optional>
21
#include <span>
22
#include <string>
23
#include <string_view>
24
#include <vector>
25
26
#if COMPONENT_LASERDISC
27
#include "LaserdiscPlayerCLI.hh"
28
#endif
29
30
namespace openmsx {
31
32
class Reactor;
33
class MSXMotherBoard;
34
class GlobalCommandController;
35
class Interpreter;
36
37
class CommandLineParser
38
{
39
public:
40
	enum class Status : uint8_t { UNPARSED, RUN, CONTROL, TEST, EXIT };
41
	enum class Phase : uint8_t {
42
		BEFORE_INIT,       // --help, --version, -bash
43
		INIT,              // calls Reactor::init()
44
		BEFORE_SETTINGS,   // -setting, ...
45
		LOAD_SETTINGS,     // loads settings.xml
46
		BEFORE_MACHINE,    // before -machine
47
		LOAD_MACHINE,      // -machine
48
		DEFAULT_MACHINE,   // default machine
49
		LAST,              // all the rest
50
	};
51
52
	explicit CommandLineParser(Reactor& reactor);
53
	void registerOption(std::string_view str, CLIOption& cliOption,
54
		Phase phase = Phase::LAST, unsigned length = 2);
55
	void registerFileType(std::span<const std::string_view> extensions,
56
	                      CLIFileType& cliFileType);
57
	void parse(std::span<char*> argv);
58
	[[nodiscard]] Status getParseStatus() const;
59
60
	[[nodiscard]] const auto& getStartupScripts() const {
61
		return scriptOption.scripts;
62
	}
63
	[[nodiscard]] const auto& getStartupCommands() const {
64
		return commandOption.commands;
65
	}
66
67
	[[nodiscard]] MSXMotherBoard* getMotherBoard() const;
68
	[[nodiscard]] GlobalCommandController& getGlobalCommandController() const;
69
	[[nodiscard]] Interpreter& getInterpreter() const;
70
71
private:
72
	struct OptionData {
73
		std::string_view name;
74
		CLIOption* option;
75
		Phase phase;
76
		unsigned length; // length in parameters
77
	};
78
	struct FileTypeData {
79
		std::string_view extension;
80
		CLIFileType* fileType;
81
	};
82
83
	[[nodiscard]] bool parseFileName(const std::string& arg,
84
	                   std::span<std::string>& cmdLine);
85
	[[nodiscard]] CLIFileType* getFileTypeHandlerForFileName(std::string_view filename) const;
86
	[[nodiscard]] bool parseOption(const std::string& arg,
87
	                 std::span<std::string>& cmdLine, Phase phase);
88
	void createMachineSetting();
89
90
private:
91
	std::vector<OptionData> options;
92
	std::vector<FileTypeData> fileTypes;
93
94
	Reactor& reactor;
95
96
	struct HelpOption final : CLIOption {
97
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
98
		[[nodiscard]] std::string_view optionHelp() const override;
99
	} helpOption;
100
101
	struct VersionOption final : CLIOption {
102
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
103
		[[nodiscard]] std::string_view optionHelp() const override;
104
	} versionOption;
105
106
	struct ControlOption final : CLIOption {
107
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
108
		[[nodiscard]] std::string_view optionHelp() const override;
109
	} controlOption;
110
111
	struct ScriptOption final : CLIOption, CLIFileType {
112
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
113
		[[nodiscard]] std::string_view optionHelp() const override;
114
		void parseFileType(const std::string& filename,
115
				   std::span<std::string>& cmdLine) override;
116
		[[nodiscard]] std::string_view fileTypeCategoryName() const override;
117
		[[nodiscard]] std::string_view fileTypeHelp() const override;
118
119
		std::vector<std::string> scripts;
120
	} scriptOption;
121
122
	struct CommandOption final : CLIOption {
123
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
124
		[[nodiscard]] std::string_view optionHelp() const override;
125
126
		std::vector<std::string> commands;
127
	} commandOption;
128
129
	struct MachineOption final : CLIOption {
130
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
131
		[[nodiscard]] std::string_view optionHelp() const override;
132
	} machineOption;
133
134
	struct SetupOption final : CLIOption {
135
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
136
		[[nodiscard]] std::string_view optionHelp() const override;
137
	} setupOption;
138
139
	struct SettingOption final : CLIOption {
140
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
141
		[[nodiscard]] std::string_view optionHelp() const override;
142
	} settingOption;
143
144
	struct TestConfigOption final : CLIOption {
145
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
146
		[[nodiscard]] std::string_view optionHelp() const override;
147
	} testConfigOption;
148
149
	struct BashOption final : CLIOption {
150
		void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
151
		[[nodiscard]] std::string_view optionHelp() const override;
152
	} bashOption;
153
154
	struct FileTypeCategoryInfoTopic final : InfoTopic {
155
		FileTypeCategoryInfoTopic(InfoCommand& openMSXInfoCommand, const CommandLineParser& parser);
156
		void execute(std::span<const TclObject> tokens, TclObject& result) const override;
157
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
158
	private:
159
		const CommandLineParser& parser;
160
	};
161
	std::optional<FileTypeCategoryInfoTopic> fileTypeCategoryInfo;
162
163
	MSXRomCLI msxRomCLI;
164
	CliExtension cliExtension;
165
	ReplayCLI replayCLI;
166
	SaveStateCLI saveStateCLI;
167
	CassettePlayerCLI cassettePlayerCLI;
168
#if COMPONENT_LASERDISC
169
	LaserdiscPlayerCLI laserdiscPlayerCLI;
170
#endif
171
	DiskImageCLI diskImageCLI;
172
	HDImageCLI hdImageCLI;
173
	CDImageCLI cdImageCLI;
174
	Status parseStatus = Status::UNPARSED;
175
	bool haveConfig = false;
176
	bool haveSettings = false;
177
};
178
179
} // namespace openmsx
180
181
#endif