Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 <string.h> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <base/logging.h> |
| 22 | |
| 23 | #include "command.h" |
| 24 | |
| 25 | int main(int argc, char** argv) { |
| 26 | InitLogging(argv, android::base::StderrLogger); |
| 27 | std::vector<std::string> args; |
| 28 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 29 | if (argc == 1) { |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 30 | args.push_back("help"); |
| 31 | } else { |
| 32 | for (int i = 1; i < argc; ++i) { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 33 | if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { |
| 34 | args.insert(args.begin(), "help"); |
| 35 | } else { |
| 36 | args.push_back(argv[i]); |
| 37 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | Command* command = Command::FindCommandByName(args[0]); |
| 42 | if (command == nullptr) { |
| 43 | LOG(ERROR) << "malformed command line: unknown command " << args[0]; |
| 44 | return 1; |
| 45 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 46 | std::string command_name = args[0]; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 47 | |
| 48 | LOG(DEBUG) << "command '" << command_name << "' starts running"; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 49 | bool result = command->Run(args); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 50 | LOG(DEBUG) << "command '" << command_name << "' " |
| 51 | << (result ? "finished successfully" : "failed"); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 52 | return result ? 0 : 1; |
| 53 | } |