HDImageCLI.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "HDImageCLI.hh"
  2. #include "CommandLineParser.hh"
  3. #include "MSXException.hh"
  4. #include "ranges.hh"
  5. #include <utility>
  6. #include <vector>
  7. using std::pair;
  8. using std::string;
  9. namespace openmsx {
  10. static std::vector<pair<int, string>> images;
  11. HDImageCLI::HDImageCLI(CommandLineParser& parser_)
  12. : parser(parser_)
  13. {
  14. parser.registerOption("-hda", *this, CommandLineParser::PHASE_BEFORE_MACHINE);
  15. // TODO: offer more options in case you want to specify 2 hard disk images?
  16. }
  17. void HDImageCLI::parseOption(const string& option, span<string>& cmdLine)
  18. {
  19. // Machine has not been loaded yet. Only remember the image.
  20. int id = option[3] - 'a';
  21. images.emplace_back(id, getArgument(option, cmdLine));
  22. }
  23. string HDImageCLI::getImageForId(int id)
  24. {
  25. // HD queries image. Return (and clear) the remembered value, or return
  26. // an empty string.
  27. auto it = ranges::find_if(images, [&](auto& p) { return p.first == id; });
  28. string result;
  29. if (it != end(images)) {
  30. result = std::move(it->second);
  31. images.erase(it);
  32. }
  33. return result;
  34. }
  35. void HDImageCLI::parseDone()
  36. {
  37. // After parsing all remembered values should be cleared. If not there
  38. // was no hard disk as specified.
  39. if (!images.empty()) {
  40. char hd = char(::toupper('a' + images.front().first));
  41. throw MSXException("No hard disk ", hd, " present.");
  42. }
  43. }
  44. std::string_view HDImageCLI::optionHelp() const
  45. {
  46. return "Use hard disk image in argument for the IDE or SCSI extensions";
  47. }
  48. } // namespace openmsx