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  auto it = ranges::find_if(images, [&](auto& p) { return p.first == id; });
34  string result;
35  if (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 'hdX' hard disk.
46  if (!images.empty()) {
47  string hd = "hdX";
48  hd[2] = 'a' + images.front().first;
49  throw MSXException("No hard disk named '", hd, "'.");
50  }
51 }
52 
54 {
55  return "Use hard disk image in argument for the IDE or SCSI extensions";
56 }
57 
58 } // namespace openmsx
Definition: span.hh:34
void parseOption(const std::string &option, span< std::string > &cmdLine) override
Definition: HDImageCLI.cc:22
static std::string getImageForId(int id)
Definition: HDImageCLI.cc:29
auto find_if(InputRange &&range, UnaryPredicate pred)
Definition: ranges.hh:113
HDImageCLI(CommandLineParser &parser)
Definition: HDImageCLI.cc:15
void parseDone() override
Definition: HDImageCLI.cc:42
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
string_view optionHelp() const override
Definition: HDImageCLI.cc:53
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
std::string getArgument(const std::string &option, span< std::string > &cmdLine) const
Definition: CLIOption.cc:11
auto end(const string_view &x)
Definition: string_view.hh:152