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
68private:
69 struct OptionData {
70 OptionData(std::string_view n, CLIOption* o, ParsePhase p, unsigned l)
71 : name(n), option(o), phase(p), length(l) {} // clang-15 workaround
72
73 std::string_view name;
74 CLIOption* option;
75 ParsePhase 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, ParsePhase phase);
88 void createMachineSetting();
89
90private:
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 SettingOption final : CLIOption {
135 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
136 [[nodiscard]] std::string_view optionHelp() const override;
137 } settingOption;
138
139 struct TestConfigOption final : CLIOption {
140 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
141 [[nodiscard]] std::string_view optionHelp() const override;
142 } testConfigOption;
143
144 struct BashOption final : CLIOption {
145 void parseOption(const std::string& option, std::span<std::string>& cmdLine) override;
146 [[nodiscard]] std::string_view optionHelp() const override;
147 } bashOption;
148
149 struct FileTypeCategoryInfoTopic final : InfoTopic {
150 FileTypeCategoryInfoTopic(InfoCommand& openMSXInfoCommand, const CommandLineParser& parser);
151 void execute(std::span<const TclObject> tokens, TclObject& result) const override;
152 [[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
153 private:
154 const CommandLineParser& parser;
155 };
156 std::optional<FileTypeCategoryInfoTopic> fileTypeCategoryInfo;
157
158 MSXRomCLI msxRomCLI;
159 CliExtension cliExtension;
160 ReplayCLI replayCLI;
161 SaveStateCLI saveStateCLI;
162 CassettePlayerCLI cassettePlayerCLI;
163#if COMPONENT_LASERDISC
164 LaserdiscPlayerCLI laserdiscPlayerCLI;
165#endif
166 DiskImageCLI diskImageCLI;
167 HDImageCLI hdImageCLI;
168 CDImageCLI cdImageCLI;
169 ParseStatus parseStatus = UNPARSED;
170 bool haveConfig = false;
171 bool haveSettings = false;
172};
173
174} // namespace openmsx
175
176#endif
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