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
openmsx::CommandException
Definition: CommandException.hh:9
span::empty
constexpr bool empty() const noexcept
Definition: span.hh:300
StringOp::startsWith
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:71
ranges.hh
join.hh
span::subspan
constexpr subspan_return_t< Offset, Count > subspan() const
Definition: span.hh:266
span::front
constexpr reference front() const
Definition: span.hh:310
span
Definition: span.hh:126
TclArgParser.hh
view::transform
auto transform(Range &&range, UnaryOp op)
Definition: view.hh:306
openmsx::table
constexpr Table table
Definition: CPUCore.cc:263
view.hh
span::size
constexpr index_type size() const noexcept
Definition: span.hh:296
StringOp.hh
openmsx::parseTclArgs
std::vector< TclObject > parseTclArgs(Interpreter &interp, span< const TclObject > inArgs, span< const ArgsInfo > table)
Definition: commands/TclArgParser.cc:11
detail::append
void append(Result &)
Definition: stl.hh:335
stl.hh
join
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
Definition: join.hh:60
ranges::find_if
auto find_if(InputRange &&range, UnaryPredicate pred)
Definition: ranges.hh:113
strCat
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
openmsx::Interpreter
Definition: Interpreter.hh:17
CommandException.hh
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5