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