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, -nommx, ...
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);
56 
57  const std::vector<std::string>& getStartupScripts() const {
58  return scriptOption.scripts;
59  }
60  const std::vector<std::string>& getStartupCommands() const {
61  return commandOption.commands;
62  }
63 
66  Interpreter& getInterpreter() const;
67 
70  bool isHiddenStartup() const;
71 
72 private:
73  struct OptionData {
74  CLIOption* option;
75  ParsePhase phase;
76  unsigned length; // length in parameters
77  };
78 
79  bool parseFileName(const std::string& arg,
80  span<std::string>& cmdLine);
81  CLIFileType* getFileTypeHandlerForFileName(std::string_view filename) const;
82  bool parseOption(const std::string& arg,
83  span<std::string>& cmdLine, ParsePhase phase);
84  void createMachineSetting();
85 
86  std::vector<std::pair<std::string_view, OptionData>> options;
87  std::vector<std::pair<std::string_view, CLIFileType*>> fileTypes;
88 
89  Reactor& reactor;
90 
91  struct HelpOption final : CLIOption {
92  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
93  std::string_view optionHelp() const override;
94  } helpOption;
95 
96  struct VersionOption final : CLIOption {
97  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
98  std::string_view optionHelp() const override;
99  } versionOption;
100 
101  struct ControlOption final : CLIOption {
102  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
103  std::string_view optionHelp() const override;
104  } controlOption;
105 
106  struct ScriptOption final : CLIOption, CLIFileType {
107  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
108  std::string_view optionHelp() const override;
109  void parseFileType(const std::string& filename,
110  span<std::string>& cmdLine) override;
111  std::string_view fileTypeCategoryName() const override;
112  std::string_view fileTypeHelp() const override;
113 
114  std::vector<std::string> scripts;
115  } scriptOption;
116 
117  struct CommandOption final : CLIOption {
118  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
119  std::string_view optionHelp() const override;
120 
121  std::vector<std::string> commands;
122  } commandOption;
123 
124  struct MachineOption final : CLIOption {
125  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
126  std::string_view optionHelp() const override;
127  } machineOption;
128 
129  struct SettingOption final : CLIOption {
130  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
131  std::string_view optionHelp() const override;
132  } settingOption;
133 
134  struct TestConfigOption final : CLIOption {
135  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
136  std::string_view optionHelp() const override;
137  } testConfigOption;
138 
139  struct BashOption final : CLIOption {
140  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
141  std::string_view optionHelp() const override;
142  } bashOption;
143 
144  struct FileTypeCategoryInfoTopic final : InfoTopic {
145  FileTypeCategoryInfoTopic(InfoCommand& openMSXInfoCommand, const CommandLineParser& parser);
146  void execute(span<const TclObject> tokens, TclObject& result) const override;
147  std::string help(const std::vector<std::string>& tokens) const override;
148  private:
149  const CommandLineParser& parser;
150  };
151  std::unique_ptr<FileTypeCategoryInfoTopic> fileTypeCategoryInfo;
152 
153  MSXRomCLI msxRomCLI;
154  CliExtension cliExtension;
155  ReplayCLI replayCLI;
156  SaveStateCLI saveStateCLI;
157  CassettePlayerCLI cassettePlayerCLI;
158 #if COMPONENT_LASERDISC
159  LaserdiscPlayerCLI laserdiscPlayerCLI;
160 #endif
161  DiskImageCLI diskImageCLI;
162  HDImageCLI hdImageCLI;
163  CDImageCLI cdImageCLI;
164  ParseStatus parseStatus;
165  bool haveConfig;
166  bool haveSettings;
167 };
168 
169 } // namespace openmsx
170 
171 #endif
openmsx::CommandLineParser::PHASE_LOAD_MACHINE
@ PHASE_LOAD_MACHINE
Definition: CommandLineParser.hh:44
LaserdiscPlayerCLI.hh
openmsx::CLIOption
Definition: CLIOption.hh:10
MSXRomCLI.hh
openmsx::CommandLineParser::registerOption
void registerOption(const char *str, CLIOption &cliOption, ParsePhase phase=PHASE_LAST, unsigned length=2)
Definition: CommandLineParser.cc:84
openmsx::CommandLineParser::getInterpreter
Interpreter & getInterpreter() const
Definition: CommandLineParser.cc:306
openmsx::CommandLineParser::getMotherBoard
MSXMotherBoard * getMotherBoard() const
Definition: CommandLineParser.cc:296
openmsx::CommandLineParser::ParsePhase
ParsePhase
Definition: CommandLineParser.hh:38
gl::length
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:348
openmsx::CLIFileType
Definition: CLIOption.hh:25
openmsx::CommandLineParser::isHiddenStartup
bool isHiddenStartup() const
Need to suppress renderer window on startup?
Definition: CommandLineParser.cc:285
openmsx::CommandLineParser::parse
void parse(int argc, char **argv)
Definition: CommandLineParser.cc:163
SaveStateCLI.hh
openmsx::CommandLineParser::PHASE_LOAD_SETTINGS
@ PHASE_LOAD_SETTINGS
Definition: CommandLineParser.hh:42
openmsx::CommandLineParser::PHASE_BEFORE_SETTINGS
@ PHASE_BEFORE_SETTINGS
Definition: CommandLineParser.hh:41
ReplayCLI.hh
openmsx::CommandLineParser::PHASE_DEFAULT_MACHINE
@ PHASE_DEFAULT_MACHINE
Definition: CommandLineParser.hh:45
span
Definition: span.hh:34
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:67
DiskImageCLI.hh
openmsx::CommandLineParser::PHASE_INIT
@ PHASE_INIT
Definition: CommandLineParser.hh:40
openmsx::CommandLineParser::TEST
@ TEST
Definition: CommandLineParser.hh:37
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:60
openmsx::CommandLineParser::RUN
@ RUN
Definition: CommandLineParser.hh:37
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::CommandLineParser::UNPARSED
@ UNPARSED
Definition: CommandLineParser.hh:37
openmsx::CommandLineParser::CommandLineParser
CommandLineParser(Reactor &reactor)
Definition: CommandLineParser.cc:45
span.hh
components.hh
openmsx::CommandLineParser
Definition: CommandLineParser.hh:35
openmsx::CommandLineParser::ParseStatus
ParseStatus
Definition: CommandLineParser.hh:37
CLIOption.hh
openmsx::CommandLineParser::getStartupCommands
const std::vector< std::string > & getStartupCommands() const
Definition: CommandLineParser.hh:60
CassettePlayerCLI.hh
CDImageCLI.hh
InfoTopic.hh
HDImageCLI.hh
CliExtension.hh
openmsx::CommandLineParser::getStartupScripts
const std::vector< std::string > & getStartupScripts() const
Definition: CommandLineParser.hh:57
openmsx::CommandLineParser::EXIT
@ EXIT
Definition: CommandLineParser.hh:37
openmsx::CommandLineParser::getParseStatus
ParseStatus getParseStatus() const
Definition: CommandLineParser.cc:290
openmsx::CommandLineParser::getGlobalCommandController
GlobalCommandController & getGlobalCommandController() const
Definition: CommandLineParser.cc:301
openmsx::CommandLineParser::PHASE_BEFORE_INIT
@ PHASE_BEFORE_INIT
Definition: CommandLineParser.hh:39
openmsx::CommandLineParser::registerFileType
void registerFileType(std::initializer_list< std::string_view > extensions, CLIFileType &cliFileType)
Definition: CommandLineParser.cc:90
openmsx::CommandLineParser::PHASE_LAST
@ PHASE_LAST
Definition: CommandLineParser.hh:46
openmsx::Interpreter
Definition: Interpreter.hh:17
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:37
openmsx::CommandLineParser::CONTROL
@ CONTROL
Definition: CommandLineParser.hh:37
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::CommandLineParser::PHASE_BEFORE_MACHINE
@ PHASE_BEFORE_MACHINE
Definition: CommandLineParser.hh:43