CommandLineParser.hh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef COMMANDLINEPARSER_HH
  2. #define COMMANDLINEPARSER_HH
  3. #include "CLIOption.hh"
  4. #include "MSXRomCLI.hh"
  5. #include "CliExtension.hh"
  6. #include "ReplayCLI.hh"
  7. #include "SaveStateCLI.hh"
  8. #include "CassettePlayerCLI.hh"
  9. #include "DiskImageCLI.hh"
  10. #include "HDImageCLI.hh"
  11. #include "CDImageCLI.hh"
  12. #include "InfoTopic.hh"
  13. #include "span.hh"
  14. #include "components.hh"
  15. #include <memory>
  16. #include <initializer_list>
  17. #include <string>
  18. #include <string_view>
  19. #include <vector>
  20. #include <utility>
  21. #if COMPONENT_LASERDISC
  22. #include "LaserdiscPlayerCLI.hh"
  23. #endif
  24. namespace openmsx {
  25. class Reactor;
  26. class MSXMotherBoard;
  27. class GlobalCommandController;
  28. class Interpreter;
  29. class CommandLineParser
  30. {
  31. public:
  32. enum ParseStatus { UNPARSED, RUN, CONTROL, TEST, EXIT };
  33. enum ParsePhase {
  34. PHASE_BEFORE_INIT, // --help, --version, -bash
  35. PHASE_INIT, // calls Reactor::init()
  36. PHASE_BEFORE_SETTINGS, // -setting, -nommx, ...
  37. PHASE_LOAD_SETTINGS, // loads settings.xml
  38. PHASE_BEFORE_MACHINE, // before -machine
  39. PHASE_LOAD_MACHINE, // -machine
  40. PHASE_DEFAULT_MACHINE, // default machine
  41. PHASE_LAST, // all the rest
  42. };
  43. explicit CommandLineParser(Reactor& reactor);
  44. void registerOption(const char* str, CLIOption& cliOption,
  45. ParsePhase phase = PHASE_LAST, unsigned length = 2);
  46. void registerFileType(std::initializer_list<std::string_view> extensions,
  47. CLIFileType& cliFileType);
  48. void parse(int argc, char** argv);
  49. ParseStatus getParseStatus() const;
  50. const std::vector<std::string>& getStartupScripts() const {
  51. return scriptOption.scripts;
  52. }
  53. const std::vector<std::string>& getStartupCommands() const {
  54. return commandOption.commands;
  55. }
  56. MSXMotherBoard* getMotherBoard() const;
  57. GlobalCommandController& getGlobalCommandController() const;
  58. Interpreter& getInterpreter() const;
  59. /** Need to suppress renderer window on startup?
  60. */
  61. bool isHiddenStartup() const;
  62. private:
  63. struct OptionData {
  64. CLIOption* option;
  65. ParsePhase phase;
  66. unsigned length; // length in parameters
  67. };
  68. bool parseFileName(const std::string& arg,
  69. span<std::string>& cmdLine);
  70. CLIFileType* getFileTypeHandlerForFileName(std::string_view filename) const;
  71. bool parseOption(const std::string& arg,
  72. span<std::string>& cmdLine, ParsePhase phase);
  73. void createMachineSetting();
  74. std::vector<std::pair<std::string_view, OptionData>> options;
  75. std::vector<std::pair<std::string_view, CLIFileType*>> fileTypes;
  76. Reactor& reactor;
  77. struct HelpOption final : CLIOption {
  78. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  79. std::string_view optionHelp() const override;
  80. } helpOption;
  81. struct VersionOption final : CLIOption {
  82. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  83. std::string_view optionHelp() const override;
  84. } versionOption;
  85. struct ControlOption final : CLIOption {
  86. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  87. std::string_view optionHelp() const override;
  88. } controlOption;
  89. struct ScriptOption final : CLIOption, CLIFileType {
  90. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  91. std::string_view optionHelp() const override;
  92. void parseFileType(const std::string& filename,
  93. span<std::string>& cmdLine) override;
  94. std::string_view fileTypeCategoryName() const override;
  95. std::string_view fileTypeHelp() const override;
  96. std::vector<std::string> scripts;
  97. } scriptOption;
  98. struct CommandOption final : CLIOption {
  99. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  100. std::string_view optionHelp() const override;
  101. std::vector<std::string> commands;
  102. } commandOption;
  103. struct MachineOption final : CLIOption {
  104. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  105. std::string_view optionHelp() const override;
  106. } machineOption;
  107. struct SettingOption final : CLIOption {
  108. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  109. std::string_view optionHelp() const override;
  110. } settingOption;
  111. struct TestConfigOption final : CLIOption {
  112. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  113. std::string_view optionHelp() const override;
  114. } testConfigOption;
  115. struct BashOption final : CLIOption {
  116. void parseOption(const std::string& option, span<std::string>& cmdLine) override;
  117. std::string_view optionHelp() const override;
  118. } bashOption;
  119. struct FileTypeCategoryInfoTopic final : InfoTopic {
  120. FileTypeCategoryInfoTopic(InfoCommand& openMSXInfoCommand, const CommandLineParser& parser);
  121. void execute(span<const TclObject> tokens, TclObject& result) const override;
  122. std::string help(const std::vector<std::string>& tokens) const override;
  123. private:
  124. const CommandLineParser& parser;
  125. };
  126. std::unique_ptr<FileTypeCategoryInfoTopic> fileTypeCategoryInfo;
  127. MSXRomCLI msxRomCLI;
  128. CliExtension cliExtension;
  129. ReplayCLI replayCLI;
  130. SaveStateCLI saveStateCLI;
  131. CassettePlayerCLI cassettePlayerCLI;
  132. #if COMPONENT_LASERDISC
  133. LaserdiscPlayerCLI laserdiscPlayerCLI;
  134. #endif
  135. DiskImageCLI diskImageCLI;
  136. HDImageCLI hdImageCLI;
  137. CDImageCLI cdImageCLI;
  138. ParseStatus parseStatus;
  139. bool haveConfig;
  140. bool haveSettings;
  141. };
  142. } // namespace openmsx
  143. #endif