OXYGENHUB
◆ main openmsx / src / commands / TclArgParser.cc
1.0 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "TclArgParser.hh"
2
3
#include "CommandException.hh"
4
5
#include "join.hh"
6
#include "stl.hh"
7
8
#include <algorithm>
9
#include <ranges>
10
11
namespace openmsx {
12
13
std::vector<TclObject> parseTclArgs(Interpreter& interp, std::span<const TclObject> inArgs, std::span<const ArgsInfo> table)
14
{
15
	std::vector<TclObject> outArgs;
16
	outArgs.reserve(inArgs.size());
17
18
	while (!inArgs.empty()) {
19
		auto arg = inArgs.front();
20
		auto argStr = arg.getString();
21
		inArgs = inArgs.subspan<1>();
22
		if (argStr.starts_with('-')) {
23
			if (argStr == "--") {
24
				append(outArgs, inArgs);
25
				break;
26
			}
27
			auto it = std::ranges::find(table, argStr, &ArgsInfo::name);
28
			if (it == table.end()) {
29
				throw CommandException(
30
					"Invalid option: '", argStr, "'. Must be one of ",
31
					join(std::views::transform(table, [](auto& info) {
32
					             return strCat('\'', info.name, '\'');
33
					     }), ", "), '.');
34
			}
35
			auto consumed = it->func(interp, inArgs);
36
			inArgs = inArgs.subspan(consumed);
37
		} else {
38
			outArgs.push_back(arg);
39
		}
40
	}
41
42
	return outArgs;
43
}
44
45
} // namespace openmsx