Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriverserver/CommandLineArguments.cpp
2867 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "CommandLineArguments.h"
18
#include "logging.h"
19
20
CommandLineArguments::CommandLineArguments(int argc, _TCHAR* argv[]) {
21
this->ParseArguments(argc, argv);
22
}
23
24
CommandLineArguments::~CommandLineArguments(void) {
25
}
26
27
std::wstring CommandLineArguments::GetValue(std::wstring arg_name,
28
std::wstring default_value) {
29
std::map<std::wstring, std::wstring>::const_iterator it =
30
this->args_map_.find(arg_name);
31
if (it != this->args_map_.end()) {
32
return it->second;
33
}
34
return default_value;
35
}
36
37
void CommandLineArguments::ParseArguments(int argc, _TCHAR* argv[]) {
38
this->is_help_requested_ = false;
39
this->is_version_requested_ = false;
40
for (int i = 1; i < argc; ++i) {
41
std::wstring raw_arg(argv[i]);
42
int switch_delimiter_length = GetSwitchDelimiterLength(raw_arg);
43
std::wstring arg = raw_arg.substr(switch_delimiter_length);
44
size_t equal_pos = arg.find(L"=");
45
std::wstring arg_name = L"";
46
std::wstring arg_value = L"";
47
if (equal_pos != std::string::npos && equal_pos > 0) {
48
arg_name = arg.substr(0, equal_pos);
49
arg_value = arg.substr(equal_pos + 1);
50
} else {
51
arg_name = arg;
52
}
53
54
// coerce all argument names to lowercase, making argument names
55
// case-insensitive.
56
std::transform(arg_name.begin(), arg_name.end(), arg_name.begin(), tolower);
57
58
// trim single and double quotes from argument value begin and end
59
size_t startpos = arg_value.find_first_not_of(L"'\"");
60
if (startpos != std::string::npos) {
61
arg_value = arg_value.substr(startpos);
62
}
63
size_t endpos = arg_value.find_last_not_of(L"'\"");
64
if (endpos != std::string::npos) {
65
arg_value = arg_value.substr(0, endpos + 1);
66
}
67
68
if (arg_name == L"?" || arg_name == L"h" || arg_name == L"help") {
69
this->is_help_requested_ = true;
70
}
71
72
if (arg_name == L"v" || arg_name == L"version") {
73
this->is_version_requested_ = true;
74
}
75
76
this->args_map_[arg_name] = arg_value;
77
}
78
}
79
80
int CommandLineArguments::GetSwitchDelimiterLength(std::wstring arg) {
81
if (arg.find(L"--") == 0) {
82
return 2;
83
} else if (arg.find(L"-") == 0 || arg.find(L"/") == 0) {
84
return 1;
85
}
86
87
return 0;
88
}
89
90