1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "utils/command_utils.h"
18 
19 #include <cstdio>
20 
21 #include "command_executors/diff_executor.h"
22 #include "command_executors/header_executor.h"
23 #include "command_executors/help_executor.h"
24 #include "command_executors/info_executor.h"
25 #include "command_executors/makedict_executor.h"
26 
27 namespace latinime {
28 namespace dicttoolkit {
29 
getCommandType(const std::string & commandName)30 /* static */ CommandType CommandUtils::getCommandType(const std::string &commandName) {
31     if (commandName == InfoExecutor::COMMAND_NAME) {
32         return CommandType::Info;
33     } else if (commandName == DiffExecutor::COMMAND_NAME) {
34         return CommandType::Diff;
35     } else if (commandName == MakedictExecutor::COMMAND_NAME) {
36         return CommandType::Makedict;
37     } else if (commandName == HeaderExecutor::COMMAND_NAME) {
38         return CommandType::Header;
39     } else if (commandName == HelpExecutor::COMMAND_NAME) {
40         return CommandType::Help;
41     } else {
42         return CommandType::Unknown;
43     }
44 }
45 
printCommandUnknownMessage(const std::string & programName,const std::string & commandName)46 /* static */ void CommandUtils::printCommandUnknownMessage(const std::string &programName,
47         const std::string &commandName) {
48     fprintf(stderr, "Command '%s' is unknown. Try '%s %s' for more information.\n",
49             commandName.c_str(), programName.c_str(), HelpExecutor::COMMAND_NAME);
50 }
51 
getCommandExecutor(const CommandType commandType)52 /* static */ std::function<int(int, char **)> CommandUtils::getCommandExecutor(
53         const CommandType commandType) {
54     switch (commandType) {
55         case CommandType::Info:
56             return InfoExecutor::run;
57         case CommandType::Diff:
58             return DiffExecutor::run;
59         case CommandType::Makedict:
60             return MakedictExecutor::run;
61         case CommandType::Header:
62             return HeaderExecutor::run;
63         case CommandType::Help:
64             return HelpExecutor::run;
65         default:
66             return [] (int, char **) -> int {
67                 printf("Command executor not found.");
68                 return 1;
69             };
70     }
71 }
72 
73 } // namespace dicttoolkit
74 } // namespace latinime
75