openMSX
CommandLineParser.hh
Go to the documentation of this file.
1#ifndef COMMANDLINEPARSER_HH
2#define COMMANDLINEPARSER_HH
3
4#include "CLIOption.hh"
5#include "MSXRomCLI.hh"
6#include "CliExtension.hh"
7#include "ReplayCLI.hh"
8#include "SaveStateCLI.hh"
10#include "DiskImageCLI.hh"
11#include "HDImageCLI.hh"
12#include "CDImageCLI.hh"
13#include "InfoTopic.hh"
14#include "components.hh"
15#include <initializer_list>
16#include <memory>
17#include <optional>
18#include <span>
19#include <string>
20#include <string_view>
21#include <vector>
22
23#if COMPONENT_LASERDISC
24#include "LaserdiscPlayerCLI.hh"
25#endif
26
27namespace openmsx {
28
29class Reactor;
30class MSXMotherBoard;
31class GlobalCommandController;
32class Interpreter;
33
35{
36public:
39 PHASE_BEFORE_INIT, // --help, --version, -bash
40 PHASE_INIT, // calls Reactor::init()
41 PHASE_BEFORE_SETTINGS, // -setting, ...
42 PHASE_LOAD_SETTINGS, // loads settings.xml
43 PHASE_BEFORE_MACHINE, // before -machine
44 PHASE_LOAD_MACHINE, // -machine
45 PHASE_DEFAULT_MACHINE, // default machine
46 PHASE_LAST, // all the rest
47 };
48
49 explicit CommandLineParser(Reactor& reactor);
50 void registerOption(const char* str, CLIOption& cliOption,
51 ParsePhase phase = PHASE_LAST, unsigned length = 2);
52 void registerFileType(std::span<const std::string_view> extensions,
53 CLIFileType& cliFileType);
54 void parse(std::span<char*> argv);
55 [[nodiscard]] ParseStatus getParseStatus() const;
56
57 [[nodiscard]] const auto& getStartupScripts() const {
58 return scriptOption.scripts;
59 }
60 [[nodiscard]] const auto& getStartupCommands() const {
61 return commandOption.commands;
62 }
63
64 [[nodiscard]] MSXMotherBoard* getMotherBoard() const;
66 [[nodiscard]] Interpreter& getInterpreter() const;
67
70 [[nodiscard]] bool isHiddenStartup() const;
71
72private:
73 struct OptionData {
74 OptionData(std::string_view n, CLIOption* o, ParsePhase p, unsigned l)
75 : name(n), option(o), phase(p), length(l) {} // clang-15 workaround
76
77 std::string_view name;
78 CLIOption* option;
79 ParsePhase phase;
80 unsigned length; // length in parameters
81 };
82 struct FileTypeData {
83 std::string_view extension;
84 CLIFileType* fileType;
85 };
86
87 [[nodiscard]] bool parseFileName(const std::string& arg,
88 std::span<std::string>& cmdLine);
89 [[nodiscard]] CLIFileType* getFileTypeHandlerForFileName(std::string_view filename) const;
90 [[nodiscard]] bool parseOption(const std::string& arg,
91 std::span<std::string>& cmdLine, ParsePhase phase);
92 void createMachineSetting();
93
94private:
95 std::vector<OptionData> options;
96 std::vector<FileTypeData> fileTypes;
97
98 Reactor& reactor;
99
100 struct HelpOption final : CLIOption {
101 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
102 [[nodiscard]] std::string_view optionHelp() const override;
103 } helpOption;
104
105 struct VersionOption final : CLIOption {
106 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
107 [[nodiscard]] std::string_view optionHelp() const override;
108 } versionOption;
109
110 struct ControlOption final : CLIOption {
111 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
112 [[nodiscard]] std::string_view optionHelp() const override;
113 } controlOption;
114
115 struct ScriptOption final : CLIOption, CLIFileType {
116 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
117 [[nodiscard]] std::string_view optionHelp() const override;
118 void parseFileType(const std::string& filename,
119 std::span<std::string>& cmdLine) override;
120 [[nodiscard]] std::string_view fileTypeCategoryName() const override;
121 [[nodiscard]] std::string_view fileTypeHelp() const override;
122
123 std::vector<std::string> scripts;
124 } scriptOption;
125
126 struct CommandOption final : CLIOption {
127 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
128 [[nodiscard]] std::string_view optionHelp() const override;
129
130 std::vector<std::string> commands;
131 } commandOption;
132
133 struct MachineOption final : CLIOption {
134 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
135 [[nodiscard]] std::string_view optionHelp() const override;
136 } machineOption;
137
138 struct SettingOption final : CLIOption {
139 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
140 [[nodiscard]] std::string_view optionHelp() const override;
141 } settingOption;
142
143 struct TestConfigOption final : CLIOption {
144 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
145 [[nodiscard]] std::string_view optionHelp() const override;
146 } testConfigOption;
147
148 struct BashOption final : CLIOption {
149 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
150 [[nodiscard]] std::string_view optionHelp() const override;
151 } bashOption;
152
153 struct FileTypeCategoryInfoTopic final : InfoTopic {
154 FileTypeCategoryInfoTopic(InfoCommand& openMSXInfoCommand, const CommandLineParser& parser);
155 void execute(std::span<const TclObject> tokens, TclObject& result) const override;
156 [[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
157 private:
158 const CommandLineParser& parser;
159 };
160 std::optional<FileTypeCategoryInfoTopic> fileTypeCategoryInfo;
161
162 MSXRomCLI msxRomCLI;
163 CliExtension cliExtension;
164 ReplayCLI replayCLI;
165 SaveStateCLI saveStateCLI;
166 CassettePlayerCLI cassettePlayerCLI;
167#if COMPONENT_LASERDISC
168 LaserdiscPlayerCLI laserdiscPlayerCLI;
169#endif
170 DiskImageCLI diskImageCLI;
171 HDImageCLI hdImageCLI;
172 CDImageCLI cdImageCLI;
173 ParseStatus parseStatus = UNPARSED;
174 bool haveConfig = false;
175 bool haveSettings = false;
176};
177
178} // namespace openmsx
179
180#endif
bool isHiddenStartup() const
Need to suppress renderer window on startup?
void registerOption(const char *str, CLIOption &cliOption, ParsePhase phase=PHASE_LAST, unsigned length=2)
const auto & getStartupScripts() const
ParseStatus getParseStatus() const
void registerFileType(std::span< const std::string_view > extensions, CLIFileType &cliFileType)
void parse(std::span< char * > argv)
const auto & getStartupCommands() const
GlobalCommandController & getGlobalCommandController() const
Interpreter & getInterpreter() const
MSXMotherBoard * getMotherBoard() const
Contains the main loop of openMSX.
Definition Reactor.hh:75
This file implemented 3 utility functions:
Definition Autofire.cc:11