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"
9 #include "CassettePlayerCLI.hh"
10 #include "DiskImageCLI.hh"
11 #include "HDImageCLI.hh"
12 #include "CDImageCLI.hh"
13 #include "InfoTopic.hh"
14 #include "span.hh"
15 #include "components.hh"
16 #include <initializer_list>
17 #include <memory>
18 #include <optional>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22 
23 #if COMPONENT_LASERDISC
24 #include "LaserdiscPlayerCLI.hh"
25 #endif
26 
27 namespace openmsx {
28 
29 class Reactor;
30 class MSXMotherBoard;
31 class GlobalCommandController;
32 class Interpreter;
33 
35 {
36 public:
38  enum ParsePhase {
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::initializer_list<std::string_view> extensions,
53  CLIFileType& cliFileType);
54  void parse(int argc, 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 
72 private:
73  struct OptionData {
74  std::string_view name;
75  CLIOption* option;
76  ParsePhase phase;
77  unsigned length; // length in parameters
78  };
79  struct FileTypeData {
80  std::string_view extension;
81  CLIFileType* fileType;
82  };
83 
84  [[nodiscard]] bool parseFileName(const std::string& arg,
85  span<std::string>& cmdLine);
86  [[nodiscard]] CLIFileType* getFileTypeHandlerForFileName(std::string_view filename) const;
87  [[nodiscard]] bool parseOption(const std::string& arg,
88  span<std::string>& cmdLine, ParsePhase phase);
89  void createMachineSetting();
90 
91 private:
92  std::vector<OptionData> options;
93  std::vector<FileTypeData> fileTypes;
94 
95  Reactor& reactor;
96 
97  struct HelpOption final : CLIOption {
98  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
99  [[nodiscard]] std::string_view optionHelp() const override;
100  } helpOption;
101 
102  struct VersionOption final : CLIOption {
103  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
104  [[nodiscard]] std::string_view optionHelp() const override;
105  } versionOption;
106 
107  struct ControlOption final : CLIOption {
108  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
109  [[nodiscard]] std::string_view optionHelp() const override;
110  } controlOption;
111 
112  struct ScriptOption final : CLIOption, CLIFileType {
113  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
114  [[nodiscard]] std::string_view optionHelp() const override;
115  void parseFileType(const std::string& filename,
116  span<std::string>& cmdLine) override;
117  [[nodiscard]] std::string_view fileTypeCategoryName() const override;
118  [[nodiscard]] std::string_view fileTypeHelp() const override;
119 
120  std::vector<std::string> scripts;
121  } scriptOption;
122 
123  struct CommandOption final : CLIOption {
124  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
125  [[nodiscard]] std::string_view optionHelp() const override;
126 
127  std::vector<std::string> commands;
128  } commandOption;
129 
130  struct MachineOption final : CLIOption {
131  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
132  [[nodiscard]] std::string_view optionHelp() const override;
133  } machineOption;
134 
135  struct SettingOption final : CLIOption {
136  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
137  [[nodiscard]] std::string_view optionHelp() const override;
138  } settingOption;
139 
140  struct TestConfigOption final : CLIOption {
141  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
142  [[nodiscard]] std::string_view optionHelp() const override;
143  } testConfigOption;
144 
145  struct BashOption final : CLIOption {
146  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
147  [[nodiscard]] std::string_view optionHelp() const override;
148  } bashOption;
149 
150  struct FileTypeCategoryInfoTopic final : InfoTopic {
151  FileTypeCategoryInfoTopic(InfoCommand& openMSXInfoCommand, const CommandLineParser& parser);
152  void execute(span<const TclObject> tokens, TclObject& result) const override;
153  [[nodiscard]] std::string help(span<const TclObject> tokens) const override;
154  private:
155  const CommandLineParser& parser;
156  };
157  std::optional<FileTypeCategoryInfoTopic> fileTypeCategoryInfo;
158 
159  MSXRomCLI msxRomCLI;
160  CliExtension cliExtension;
161  ReplayCLI replayCLI;
162  SaveStateCLI saveStateCLI;
163  CassettePlayerCLI cassettePlayerCLI;
164 #if COMPONENT_LASERDISC
165  LaserdiscPlayerCLI laserdiscPlayerCLI;
166 #endif
167  DiskImageCLI diskImageCLI;
168  HDImageCLI hdImageCLI;
169  CDImageCLI cdImageCLI;
170  ParseStatus parseStatus;
171  bool haveConfig;
172  bool haveSettings;
173 };
174 
175 } // namespace openmsx
176 
177 #endif
CommandLineParser(Reactor &reactor)
bool isHiddenStartup() const
Need to suppress renderer window on startup?
void registerOption(const char *str, CLIOption &cliOption, ParsePhase phase=PHASE_LAST, unsigned length=2)
void parse(int argc, char **argv)
void registerFileType(std::initializer_list< std::string_view > extensions, CLIFileType &cliFileType)
ParseStatus getParseStatus() const
const auto & getStartupCommands() const
const auto & getStartupScripts() const
GlobalCommandController & getGlobalCommandController() const
Interpreter & getInterpreter() const
MSXMotherBoard * getMotherBoard() const
Contains the main loop of openMSX.
Definition: Reactor.hh:68
Definition: span.hh:126
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename