openMSX
commands/TclArgParser.cc
Go to the documentation of this file.
1 #include "TclArgParser.hh"
2 #include "CommandException.hh"
3 #include "join.hh"
4 #include "ranges.hh"
5 #include "stl.hh"
6 #include "StringOp.hh"
7 #include "view.hh"
8 
9 namespace openmsx {
10 
12 {
13  std::vector<TclObject> outArgs;
14  outArgs.reserve(inArgs.size());
15 
16  while (!inArgs.empty()) {
17  auto arg = inArgs.front();
18  auto argStr = arg.getString();
19  inArgs = inArgs.subspan<1>();
20  if (StringOp::startsWith(argStr, '-')) {
21  if (argStr == "--") {
22  append(outArgs, inArgs);
23  break;
24  }
25  auto it = ranges::find_if(table, [&](const auto& info) { return info.name == argStr; });
26  if (it == table.end()) {
27  throw CommandException(
28  "Invalid option: '", argStr, "'. Must be one of ",
29  join(view::transform(table, [](auto& info) {
30  return strCat('\'', info.name, '\'');
31  }), ", "), '.');
32  }
33  auto consumed = it->func(interp, inArgs);
34  inArgs = inArgs.subspan(consumed);
35  } else {
36  outArgs.push_back(arg);
37  }
38  }
39 
40  return outArgs;
41 }
42 
43 } // namespace openmsx
auto transform(Range &&range, UnaryOp op)
Definition: view.hh:306
Definition: span.hh:34
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:71
auto find_if(InputRange &&range, UnaryPredicate pred)
Definition: ranges.hh:113
void append(Result &)
Definition: stl.hh:340
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
constexpr Table table
Definition: CPUCore.cc:263
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
Definition: join.hh:60
std::vector< TclObject > parseTclArgs(Interpreter &interp, span< const TclObject > inArgs, span< const ArgsInfo > table)