OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/dbus/dbus_command_line_helper.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 bool GetOptionArgs(const std::string& option, |
| 15 std::string* arg0, |
| 16 std::string* arg1) { |
| 17 std::vector<std::string> args; |
| 18 base::SplitString(option, '=', &args); |
| 19 if (args.size() < 1) { |
| 20 LOG(WARNING) << " Invalid option: " << option; |
| 21 arg0->clear(); |
| 22 arg1->clear(); |
| 23 return false; |
| 24 } |
| 25 *arg0 = args[0]; |
| 26 if (args.size() >= 2) |
| 27 *arg1 = args[1]; |
| 28 else |
| 29 arg1->clear(); |
| 30 VLOG(2) << " Option: " << *arg0 << " = " << *arg1; |
| 31 return true; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 namespace dbus_command_line_helper { |
| 37 |
| 38 bool ParseOptions(const std::string& switch_name, |
| 39 const ParseOptionCallback& callback) { |
| 40 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 41 if (!command_line->HasSwitch(switch_name)) |
| 42 return false; |
| 43 |
| 44 std::string option_str = command_line->GetSwitchValueASCII(switch_name); |
| 45 VLOG(1) << "ParseOption: " << option_str; |
| 46 std::vector<std::string> tokens; |
| 47 base::SplitString(option_str, ',', &tokens); |
| 48 for (std::vector<std::string>::iterator iter = tokens.begin(); |
| 49 iter != tokens.end(); ++iter) { |
| 50 std::string arg0, arg1; |
| 51 if (!GetOptionArgs(*iter, &arg0, &arg1)) |
| 52 continue; |
| 53 if (!callback.Run(arg0, arg1)) |
| 54 LOG(WARNING) << "Unrecognized option: " << *iter; |
| 55 } |
| 56 return true; |
| 57 } |
| 58 |
| 59 } // namespace dbus_command_line_helper |
OLD | NEW |