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 19 #include <string> 20 #include <vector> 21 22 #include <android-base/logging.h> 23 24 #include "command.h" 25 #include "utils.h" 26 27 int main(int argc, char** argv) { 28 InitLogging(argv, android::base::StderrLogger); 29 std::vector<std::string> args; 30 android::base::LogSeverity log_severity = android::base::WARNING; 31 32 for (int i = 1; i < argc; ++i) { 33 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { 34 args.insert(args.begin(), "help"); 35 } else if (strcmp(argv[i], "--log") == 0) { 36 if (i + 1 < argc) { 37 ++i; 38 if (!GetLogSeverity(argv[i], &log_severity)) { 39 LOG(ERROR) << "Unknown log severity: " << argv[i]; 40 return 1; 41 } 42 } else { 43 LOG(ERROR) << "Missing argument for --log option.\n"; 44 return 1; 45 } 46 } else { 47 args.push_back(argv[i]); 48 } 49 } 50 android::base::ScopedLogSeverity severity(log_severity); 51 52 if (args.empty()) { 53 args.push_back("help"); 54 } 55 std::unique_ptr<Command> command = CreateCommandInstance(args[0]); 56 if (command == nullptr) { 57 LOG(ERROR) << "malformed command line: unknown command " << args[0]; 58 return 1; 59 } 60 std::string command_name = args[0]; 61 args.erase(args.begin()); 62 63 LOG(DEBUG) << "command '" << command_name << "' starts running"; 64 bool result = command->Run(args); 65 LOG(DEBUG) << "command '" << command_name << "' " 66 << (result ? "finished successfully" : "failed"); 67 return result ? 0 : 1; 68 } 69