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 "span.hh"
14 #include "components.hh"
15 #include <initializer_list>
16 #include <string>
17 #include <string_view>
18 #include <vector>
19 #include <utility>
20 
21 #if COMPONENT_LASERDISC
22 #include "LaserdiscPlayerCLI.hh"
23 #endif
24 
25 namespace openmsx {
26 
27 class Reactor;
28 class MSXMotherBoard;
29 class GlobalCommandController;
30 class Interpreter;
31 
33 {
34 public:
36  enum ParsePhase {
37  PHASE_BEFORE_INIT, // --help, --version, -bash
38  PHASE_INIT, // calls Reactor::init()
39  PHASE_BEFORE_SETTINGS, // -setting, -nommx, ...
40  PHASE_LOAD_SETTINGS, // loads settings.xml
41  PHASE_BEFORE_MACHINE, // before -machine
42  PHASE_LOAD_MACHINE, // -machine
43  PHASE_DEFAULT_MACHINE, // default machine
44  PHASE_LAST, // all the rest
45  };
46 
47  explicit CommandLineParser(Reactor& reactor);
48  void registerOption(const char* str, CLIOption& cliOption,
49  ParsePhase phase = PHASE_LAST, unsigned length = 2);
50  void registerFileType(std::initializer_list<std::string_view> extensions,
51  CLIFileType& cliFileType);
52  void parse(int argc, char** argv);
54 
55  const std::vector<std::string>& getStartupScripts() const {
56  return scriptOption.scripts;
57  }
58  const std::vector<std::string>& getStartupCommands() const {
59  return commandOption.commands;
60  }
61 
64  Interpreter& getInterpreter() const;
65 
68  bool isHiddenStartup() const;
69 
70 private:
71  struct OptionData {
72  CLIOption* option;
73  ParsePhase phase;
74  unsigned length; // length in parameters
75  };
76 
77  bool parseFileName(const std::string& arg,
78  span<std::string>& cmdLine);
79  bool parseFileNameInner(const std::string& arg, const std::string&
80  originalPath, span<std::string>& cmdLine);
81  bool parseOption(const std::string& arg,
82  span<std::string>& cmdLine, ParsePhase phase);
83  void createMachineSetting();
84 
85  std::vector<std::pair<std::string_view, OptionData>> options;
86  std::vector<std::pair<std::string_view, CLIFileType*>> fileTypes;
87 
88  Reactor& reactor;
89 
90  struct HelpOption final : CLIOption {
91  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
92  std::string_view optionHelp() const override;
93  } helpOption;
94 
95  struct VersionOption final : CLIOption {
96  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
97  std::string_view optionHelp() const override;
98  } versionOption;
99 
100  struct ControlOption final : CLIOption {
101  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
102  std::string_view optionHelp() const override;
103  } controlOption;
104 
105  struct ScriptOption final : CLIOption, CLIFileType {
106  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
107  std::string_view optionHelp() const override;
108  void parseFileType(const std::string& filename,
109  span<std::string>& cmdLine) override;
110  std::string_view fileTypeHelp() const override;
111 
112  std::vector<std::string> scripts;
113  } scriptOption;
114 
115  struct CommandOption final : CLIOption {
116  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
117  std::string_view optionHelp() const override;
118 
119  std::vector<std::string> commands;
120  } commandOption;
121 
122  struct MachineOption final : CLIOption {
123  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
124  std::string_view optionHelp() const override;
125  } machineOption;
126 
127  struct SettingOption final : CLIOption {
128  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
129  std::string_view optionHelp() const override;
130  } settingOption;
131 
132  struct TestConfigOption final : CLIOption {
133  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
134  std::string_view optionHelp() const override;
135  } testConfigOption;
136 
137  struct BashOption final : CLIOption {
138  void parseOption(const std::string& option, span<std::string>& cmdLine) override;
139  std::string_view optionHelp() const override;
140  } bashOption;
141 
142  MSXRomCLI msxRomCLI;
143  CliExtension cliExtension;
144  ReplayCLI replayCLI;
145  SaveStateCLI saveStateCLI;
146  CassettePlayerCLI cassettePlayerCLI;
147 #if COMPONENT_LASERDISC
148  LaserdiscPlayerCLI laserdiscPlayerCLI;
149 #endif
150  DiskImageCLI diskImageCLI;
151  HDImageCLI hdImageCLI;
152  CDImageCLI cdImageCLI;
153  ParseStatus parseStatus;
154  bool haveConfig;
155  bool haveSettings;
156 };
157 
158 } // namespace openmsx
159 
160 #endif
openmsx::CommandLineParser::PHASE_LOAD_MACHINE
Definition: CommandLineParser.hh:42
LaserdiscPlayerCLI.hh
openmsx::CLIOption
Definition: CLIOption.hh:9
MSXRomCLI.hh
openmsx::CommandLineParser::registerOption
void registerOption(const char *str, CLIOption &cliOption, ParsePhase phase=PHASE_LAST, unsigned length=2)
Definition: CommandLineParser.cc:83
openmsx::CommandLineParser::getInterpreter
Interpreter & getInterpreter() const
Definition: CommandLineParser.cc:298
openmsx::CommandLineParser::getMotherBoard
MSXMotherBoard * getMotherBoard() const
Definition: CommandLineParser.cc:288
openmsx::CommandLineParser::ParsePhase
ParsePhase
Definition: CommandLineParser.hh:36
gl::length
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:348
openmsx::CLIFileType
Definition: CLIOption.hh:24
openmsx::CommandLineParser::isHiddenStartup
bool isHiddenStartup() const
Need to suppress renderer window on startup?
Definition: CommandLineParser.cc:277
openmsx::CommandLineParser::parse
void parse(int argc, char **argv)
Definition: CommandLineParser.cc:157
SaveStateCLI.hh
openmsx::CommandLineParser::PHASE_LOAD_SETTINGS
Definition: CommandLineParser.hh:40
openmsx::CommandLineParser::PHASE_BEFORE_SETTINGS
Definition: CommandLineParser.hh:39
ReplayCLI.hh
openmsx::CommandLineParser::PHASE_DEFAULT_MACHINE
Definition: CommandLineParser.hh:43
span
Definition: span.hh:34
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:66
DiskImageCLI.hh
openmsx::CommandLineParser::PHASE_INIT
Definition: CommandLineParser.hh:38
openmsx::CommandLineParser::TEST
Definition: CommandLineParser.hh:35
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:59
openmsx::CommandLineParser::RUN
Definition: CommandLineParser.hh:35
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::CommandLineParser::UNPARSED
Definition: CommandLineParser.hh:35
openmsx::CommandLineParser::CommandLineParser
CommandLineParser(Reactor &reactor)
Definition: CommandLineParser.cc:44
span.hh
components.hh
openmsx::CommandLineParser
Definition: CommandLineParser.hh:32
openmsx::CommandLineParser::ParseStatus
ParseStatus
Definition: CommandLineParser.hh:35
CLIOption.hh
openmsx::CommandLineParser::getStartupCommands
const std::vector< std::string > & getStartupCommands() const
Definition: CommandLineParser.hh:58
CassettePlayerCLI.hh
CDImageCLI.hh
HDImageCLI.hh
CliExtension.hh
openmsx::CommandLineParser::getStartupScripts
const std::vector< std::string > & getStartupScripts() const
Definition: CommandLineParser.hh:55
openmsx::CommandLineParser::EXIT
Definition: CommandLineParser.hh:35
openmsx::CommandLineParser::getParseStatus
ParseStatus getParseStatus() const
Definition: CommandLineParser.cc:282
openmsx::CommandLineParser::getGlobalCommandController
GlobalCommandController & getGlobalCommandController() const
Definition: CommandLineParser.cc:293
openmsx::CommandLineParser::PHASE_BEFORE_INIT
Definition: CommandLineParser.hh:37
openmsx::CommandLineParser::registerFileType
void registerFileType(std::initializer_list< std::string_view > extensions, CLIFileType &cliFileType)
Definition: CommandLineParser.cc:89
openmsx::CommandLineParser::PHASE_LAST
Definition: CommandLineParser.hh:44
openmsx::Interpreter
Definition: Interpreter.hh:16
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:35
openmsx::CommandLineParser::CONTROL
Definition: CommandLineParser.hh:35
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::CommandLineParser::PHASE_BEFORE_MACHINE
Definition: CommandLineParser.hh:41