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