2015-04-13 12:09:26 -04:00
|
|
|
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
|
// distributed with this work for additional information
|
|
|
|
|
// regarding copyright ownership. The SFC licenses this file
|
|
|
|
|
// to you under the Apache License, Version 2.0 (the "License");
|
2013-01-11 22:18:32 +01:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
#include "CommandLineArguments.h"
|
2013-04-10 00:02:15 +04:00
|
|
|
#include "logging.h"
|
2013-01-11 22:18:32 +01:00
|
|
|
|
|
|
|
|
CommandLineArguments::CommandLineArguments(int argc, _TCHAR* argv[]) {
|
|
|
|
|
this->ParseArguments(argc, argv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandLineArguments::~CommandLineArguments(void) {
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 10:59:48 -04:00
|
|
|
std::wstring CommandLineArguments::GetValue(std::wstring arg_name,
|
2013-04-10 00:02:15 +04:00
|
|
|
std::wstring default_value) {
|
2013-03-20 10:59:48 -04:00
|
|
|
std::map<std::wstring, std::wstring>::const_iterator it =
|
2013-01-11 22:18:32 +01:00
|
|
|
this->args_map_.find(arg_name);
|
|
|
|
|
if (it != this->args_map_.end()) {
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
return default_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandLineArguments::ParseArguments(int argc, _TCHAR* argv[]) {
|
|
|
|
|
this->is_help_requested_ = false;
|
2016-06-29 05:02:58 -07:00
|
|
|
this->is_version_requested_ = false;
|
2013-01-11 22:18:32 +01:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
|
std::wstring raw_arg(argv[i]);
|
|
|
|
|
int switch_delimiter_length = GetSwitchDelimiterLength(raw_arg);
|
|
|
|
|
std::wstring arg = raw_arg.substr(switch_delimiter_length);
|
|
|
|
|
size_t equal_pos = arg.find(L"=");
|
2013-03-20 10:59:48 -04:00
|
|
|
std::wstring arg_name = L"";
|
|
|
|
|
std::wstring arg_value = L"";
|
2013-01-11 22:18:32 +01:00
|
|
|
if (equal_pos != std::string::npos && equal_pos > 0) {
|
2013-03-20 10:59:48 -04:00
|
|
|
arg_name = arg.substr(0, equal_pos);
|
|
|
|
|
arg_value = arg.substr(equal_pos + 1);
|
2013-01-11 22:18:32 +01:00
|
|
|
} else {
|
2013-04-10 00:02:15 +04:00
|
|
|
arg_name = arg;
|
2013-01-11 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// coerce all argument names to lowercase, making argument names
|
|
|
|
|
// case-insensitive.
|
|
|
|
|
std::transform(arg_name.begin(), arg_name.end(), arg_name.begin(), tolower);
|
2013-04-10 00:02:15 +04:00
|
|
|
|
|
|
|
|
// trim single and double quotes from argument value begin and end
|
|
|
|
|
size_t startpos = arg_value.find_first_not_of(L"'\"");
|
|
|
|
|
if (startpos != std::string::npos) {
|
|
|
|
|
arg_value = arg_value.substr(startpos);
|
|
|
|
|
}
|
|
|
|
|
size_t endpos = arg_value.find_last_not_of(L"'\"");
|
|
|
|
|
if (endpos != std::string::npos) {
|
|
|
|
|
arg_value = arg_value.substr(0, endpos + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 10:59:48 -04:00
|
|
|
if (arg_name == L"?" || arg_name == L"h" || arg_name == L"help") {
|
2013-01-11 22:18:32 +01:00
|
|
|
this->is_help_requested_ = true;
|
|
|
|
|
}
|
2013-04-10 00:02:15 +04:00
|
|
|
|
2016-06-29 05:02:58 -07:00
|
|
|
if (arg_name == L"v" || arg_name == L"version") {
|
|
|
|
|
this->is_version_requested_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-11 22:18:32 +01:00
|
|
|
this->args_map_[arg_name] = arg_value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CommandLineArguments::GetSwitchDelimiterLength(std::wstring arg) {
|
|
|
|
|
if (arg.find(L"--") == 0) {
|
|
|
|
|
return 2;
|
|
|
|
|
} else if (arg.find(L"-") == 0 || arg.find(L"/") == 0) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|