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 <memory>
17 #include <initializer_list>
18 #include <string>
19 #include <string_view>
20 #include <vector>
21 #include <utility>
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 std::vector<std::string>& getStartupScripts() const {
58  return scriptOption.scripts;
59  }
60  [[nodiscard]] const std::vector<std::string>& 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  CLIOption* option;
75  ParsePhase phase;
76  unsigned length; // length in parameters
77  };
78 
79  [[nodiscard]] bool parseFileName(const std::string& arg,
80  span<std::string>& cmdLine);
81  [[nodiscard]] CLIFileType* getFileTypeHandlerForFileName(std::string_view filename) const;
82  [[nodiscard]] bool parseOption(const std::string& arg,
83  span<std::string>& cmdLine, ParsePhase phase);
84  void createMachineSetting();
85 
86 private:
87  std::vector<std::pair<std::string_view, OptionData>> options;
88  std::vector<std::pair<std::string_view, CLIFileType*>> fileTypes;
89 
90  Reactor& reactor;
91 
92  struct HelpOption final : CLIOption {
93  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
94  [[nodiscard]] std::string_view optionHelp() const override;
95  } helpOption;
96 
97  struct VersionOption final : CLIOption {
98  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
99  [[nodiscard]] std::string_view optionHelp() const override;
100  } versionOption;
101 
102  struct ControlOption final : CLIOption {
103  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
104  [[nodiscard]] std::string_view optionHelp() const override;
105  } controlOption;
106 
107  struct ScriptOption final : CLIOption, CLIFileType {
108  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
109  [[nodiscard]] std::string_view optionHelp() const override;
110  void parseFileType(const std::string& filename,
111  span<std::string>& cmdLine) override;
112  [[nodiscard]] std::string_view fileTypeCategoryName() const override;
113  [[nodiscard]] std::string_view fileTypeHelp() const override;
114 
115  std::vector<std::string> scripts;
116  } scriptOption;
117 
118  struct CommandOption final : CLIOption {
119  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
120  [[nodiscard]] std::string_view optionHelp() const override;
121 
122  std::vector<std::string> commands;
123  } commandOption;
124 
125  struct MachineOption final : CLIOption {
126  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
127  [[nodiscard]] std::string_view optionHelp() const override;
128  } machineOption;
129 
130  struct SettingOption final : CLIOption {
131  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
132  [[nodiscard]] std::string_view optionHelp() const override;
133  } settingOption;
134 
135  struct TestConfigOption final : CLIOption {
136  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
137  [[nodiscard]] std::string_view optionHelp() const override;
138  } testConfigOption;
139 
140  struct BashOption final : CLIOption {
141  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
142  [[nodiscard]] std::string_view optionHelp() const override;
143  } bashOption;
144 
145  struct FileTypeCategoryInfoTopic final : InfoTopic {
146  FileTypeCategoryInfoTopic(InfoCommand& openMSXInfoCommand, const CommandLineParser& parser);
147  void execute(span<const TclObject> tokens, TclObject& result) const override;
148  [[nodiscard]] std::string help(const std::vector<std::string>& tokens) const override;
149  private:
150  const CommandLineParser& parser;
151  };
152  std::unique_ptr<FileTypeCategoryInfoTopic> fileTypeCategoryInfo;
153 
154  MSXRomCLI msxRomCLI;
155  CliExtension cliExtension;
156  ReplayCLI replayCLI;
157  SaveStateCLI saveStateCLI;
158  CassettePlayerCLI cassettePlayerCLI;
159 #if COMPONENT_LASERDISC
160  LaserdiscPlayerCLI laserdiscPlayerCLI;
161 #endif
162  DiskImageCLI diskImageCLI;
163  HDImageCLI hdImageCLI;
164  CDImageCLI cdImageCLI;
165  ParseStatus parseStatus;
166  bool haveConfig;
167  bool haveSettings;
168 };
169 
170 } // namespace openmsx
171 
172 #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)
const std::vector< std::string > & getStartupScripts() const
void registerFileType(std::initializer_list< std::string_view > extensions, CLIFileType &cliFileType)
ParseStatus getParseStatus() const
const std::vector< std::string > & getStartupCommands() const
GlobalCommandController & getGlobalCommandController() const
Interpreter & getInterpreter() const
MSXMotherBoard * getMotherBoard() const
Contains the main loop of openMSX.
Definition: Reactor.hh:67
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