summaryrefslogtreecommitdiffhomepage
path: root/windows/winfw/src/extras/cli/modules/module.cpp
blob: 80739d4a42547203b2ac911dc75b9ee03ca7b69b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "stdafx.h"
#include "module.h"
#include "cli/util.h"
#include <libcommon/string.h>
#include <libcommon/error.h>
#include <sstream>
#include <utility>

namespace modules
{

PropertyList Module::commands()
{
	PropertyList c;

	for (auto &command : m_commands)
	{
		c.add(common::string::Lower(command.second->name()), command.second->description());
	}

	return c;
}

void Module::handleRequest(const std::vector<std::wstring> &request)
{
	//
	// The request has the form of:
	//
	// [0] command
	// [1] arg1
	// [2] arg2
	// ...
	//

	if (request.empty())
	{
		std::wstringstream ss;

		ss << L"Command missing. Try 'help " << m_name << "'.";

		THROW_ERROR(common::string::ToAnsi(ss.str()).c_str());
	}

	auto wanted = common::string::Lower(request[0]);
	auto found = m_commands.find(wanted);

	if (found == m_commands.end())
	{
		std::wstringstream ss;

		ss << L"Module '" << m_name << "' doesn't support the command '" << request[0] << "'.";

		THROW_ERROR(common::string::ToAnsi(ss.str()).c_str());
	}

	auto args = request;

	args.erase(args.begin());

	found->second->handleRequest(args);
}

void Module::addCommand(std::unique_ptr<commands::ICommand> command)
{
	m_commands.insert(std::make_pair(
		common::string::Lower(command->name()),
		std::move(command)
	));
}

}