| Index: chromeos/dbus/dbus_command_line_helper.cc
|
| diff --git a/chromeos/dbus/dbus_command_line_helper.cc b/chromeos/dbus/dbus_command_line_helper.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..018b7382f7d79685bd4d917d3a319c69729eaf7f
|
| --- /dev/null
|
| +++ b/chromeos/dbus/dbus_command_line_helper.cc
|
| @@ -0,0 +1,59 @@
|
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chromeos/dbus/dbus_command_line_helper.h"
|
| +
|
| +#include "base/command_line.h"
|
| +#include "base/logging.h"
|
| +#include "base/strings/string_split.h"
|
| +#include "base/strings/string_util.h"
|
| +
|
| +namespace {
|
| +
|
| +bool GetOptionArgs(const std::string& option,
|
| + std::string* arg0,
|
| + std::string* arg1) {
|
| + std::vector<std::string> args;
|
| + base::SplitString(option, '=', &args);
|
| + if (args.size() < 1) {
|
| + LOG(WARNING) << " Invalid option: " << option;
|
| + arg0->clear();
|
| + arg1->clear();
|
| + return false;
|
| + }
|
| + *arg0 = args[0];
|
| + if (args.size() >= 2)
|
| + *arg1 = args[1];
|
| + else
|
| + arg1->clear();
|
| + VLOG(2) << " Option: " << *arg0 << " = " << *arg1;
|
| + return true;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace dbus_command_line_helper {
|
| +
|
| +bool ParseOptions(const std::string& switch_name,
|
| + const ParseOptionCallback& callback) {
|
| + CommandLine* command_line = CommandLine::ForCurrentProcess();
|
| + if (!command_line->HasSwitch(switch_name))
|
| + return false;
|
| +
|
| + std::string option_str = command_line->GetSwitchValueASCII(switch_name);
|
| + VLOG(1) << "ParseOption: " << option_str;
|
| + std::vector<std::string> tokens;
|
| + base::SplitString(option_str, ',', &tokens);
|
| + for (std::vector<std::string>::iterator iter = tokens.begin();
|
| + iter != tokens.end(); ++iter) {
|
| + std::string arg0, arg1;
|
| + if (!GetOptionArgs(*iter, &arg0, &arg1))
|
| + continue;
|
| + if (!callback.Run(arg0, arg1))
|
| + LOG(WARNING) << "Unrecognized option: " << *iter;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +} // namespace dbus_command_line_helper
|
|
|