Path: blob/trunk/cpp/iedriverserver/CommandLineArguments.cpp
2867 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you 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 at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// 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 and14// limitations under the License.1516#include "CommandLineArguments.h"17#include "logging.h"1819CommandLineArguments::CommandLineArguments(int argc, _TCHAR* argv[]) {20this->ParseArguments(argc, argv);21}2223CommandLineArguments::~CommandLineArguments(void) {24}2526std::wstring CommandLineArguments::GetValue(std::wstring arg_name,27std::wstring default_value) {28std::map<std::wstring, std::wstring>::const_iterator it =29this->args_map_.find(arg_name);30if (it != this->args_map_.end()) {31return it->second;32}33return default_value;34}3536void CommandLineArguments::ParseArguments(int argc, _TCHAR* argv[]) {37this->is_help_requested_ = false;38this->is_version_requested_ = false;39for (int i = 1; i < argc; ++i) {40std::wstring raw_arg(argv[i]);41int switch_delimiter_length = GetSwitchDelimiterLength(raw_arg);42std::wstring arg = raw_arg.substr(switch_delimiter_length);43size_t equal_pos = arg.find(L"=");44std::wstring arg_name = L"";45std::wstring arg_value = L"";46if (equal_pos != std::string::npos && equal_pos > 0) {47arg_name = arg.substr(0, equal_pos);48arg_value = arg.substr(equal_pos + 1);49} else {50arg_name = arg;51}5253// coerce all argument names to lowercase, making argument names54// case-insensitive.55std::transform(arg_name.begin(), arg_name.end(), arg_name.begin(), tolower);5657// trim single and double quotes from argument value begin and end58size_t startpos = arg_value.find_first_not_of(L"'\"");59if (startpos != std::string::npos) {60arg_value = arg_value.substr(startpos);61}62size_t endpos = arg_value.find_last_not_of(L"'\"");63if (endpos != std::string::npos) {64arg_value = arg_value.substr(0, endpos + 1);65}6667if (arg_name == L"?" || arg_name == L"h" || arg_name == L"help") {68this->is_help_requested_ = true;69}7071if (arg_name == L"v" || arg_name == L"version") {72this->is_version_requested_ = true;73}7475this->args_map_[arg_name] = arg_value;76}77}7879int CommandLineArguments::GetSwitchDelimiterLength(std::wstring arg) {80if (arg.find(L"--") == 0) {81return 2;82} else if (arg.find(L"-") == 0 || arg.find(L"/") == 0) {83return 1;84}8586return 0;87}888990