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
8namespace openmsx {
9
10std::vector<TclObject> parseTclArgs(Interpreter& interp, std::span<const TclObject> inArgs, std::span<const ArgsInfo> table)
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
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
Definition join.hh:60
This file implemented 3 utility functions:
Definition Autofire.cc:11
std::vector< TclObject > parseTclArgs(Interpreter &interp, std::span< const TclObject > inArgs, std::span< const ArgsInfo > table)
auto find(InputRange &&range, const T &value)
Definition ranges.hh:162
constexpr auto transform(Range &&range, UnaryOp op)
Definition view.hh:520
std::string strCat()
Definition strCat.hh:703
std::string_view name