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 "view.hh"
7 
8 namespace openmsx {
9 
11 {
12  std::vector<TclObject> outArgs;
13  outArgs.reserve(inArgs.size());
14 
15  while (!inArgs.empty()) {
16  auto arg = inArgs.front();
17  auto argStr = arg.getString();
18  inArgs = inArgs.subspan<1>();
19  if (argStr.starts_with('-')) {
20  if (argStr == "--") {
21  append(outArgs, inArgs);
22  break;
23  }
24  auto it = ranges::find(table, argStr, &ArgsInfo::name);
25  if (it == table.end()) {
26  throw CommandException(
27  "Invalid option: '", argStr, "'. Must be one of ",
28  join(view::transform(table, [](auto& info) {
29  return strCat('\'', info.name, '\'');
30  }), ", "), '.');
31  }
32  auto consumed = it->func(interp, inArgs);
33  inArgs = inArgs.subspan(consumed);
34  } else {
35  outArgs.push_back(arg);
36  }
37  }
38 
39  return outArgs;
40 }
41 
42 } // namespace openmsx
Definition: span.hh:126
constexpr subspan_return_t< Offset, Count > subspan() const
Definition: span.hh:266
constexpr reference front() const
Definition: span.hh:310
constexpr index_type size() const noexcept
Definition: span.hh:296
constexpr bool empty() const noexcept
Definition: span.hh:300
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
Definition: join.hh:60
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr Table table
Definition: CPUCore.cc:260
std::vector< TclObject > parseTclArgs(Interpreter &interp, span< const TclObject > inArgs, span< const ArgsInfo > table)
auto find(InputRange &&range, const T &value)
Definition: ranges.hh:145
constexpr auto transform(Range &&range, UnaryOp op)
Definition: view.hh:399
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591
std::string_view name
Definition: TclArgParser.hh:67