summaryrefslogtreecommitdiffhomepage
path: root/wfpctl/src/extras/cli/subcommanddispatcher.cpp
diff options
context:
space:
mode:
authorOdd Stranne <odd@mullvad.net>2018-04-11 10:36:43 +0200
committerOdd Stranne <odd@mullvad.net>2018-04-11 10:36:43 +0200
commit1b97cdcd94e549decfe1d6081155c8efefe9b110 (patch)
tree66bc668bfe1868d925e32044373af83a3175bd2d /wfpctl/src/extras/cli/subcommanddispatcher.cpp
parenta5f7cf5592003093caaa658516758fb3c45206d1 (diff)
parentf53d90c5d69e81b09eec67a406825c96ea19d072 (diff)
downloadmullvadvpn-1b97cdcd94e549decfe1d6081155c8efefe9b110.tar.xz
mullvadvpn-1b97cdcd94e549decfe1d6081155c8efefe9b110.zip
Merge branch 'migrate-wfpctl'
Diffstat (limited to 'wfpctl/src/extras/cli/subcommanddispatcher.cpp')
-rw-r--r--wfpctl/src/extras/cli/subcommanddispatcher.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/wfpctl/src/extras/cli/subcommanddispatcher.cpp b/wfpctl/src/extras/cli/subcommanddispatcher.cpp
new file mode 100644
index 0000000000..4bb7868cd7
--- /dev/null
+++ b/wfpctl/src/extras/cli/subcommanddispatcher.cpp
@@ -0,0 +1,27 @@
+#include "stdafx.h"
+#include "subcommanddispatcher.h"
+#include "libcommon/string.h"
+#include <sstream>
+#include <stdexcept>
+#include <utility>
+
+void SubcommandDispatcher::addSubcommand(const std::wstring &command, Handler handler)
+{
+ m_commands.insert(std::make_pair(command, handler));
+}
+
+void SubcommandDispatcher::dispatch(const std::wstring &command, const std::vector<std::wstring> &arguments)
+{
+ auto selectedCommand = m_commands.find(command);
+
+ if (m_commands.end() == selectedCommand)
+ {
+ std::wstringstream ss;
+
+ ss << L"Unsupported subcommand '" << command << "'. Cannot complete request.";
+
+ throw std::runtime_error(common::string::ToAnsi(ss.str()).c_str());
+ }
+
+ selectedCommand->second(common::string::SplitKeyValuePairs(arguments));
+}