blob: d7757ed38e345e6948138fc3710be39f5f1ddcc8 [file] [log] [blame]
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000011#include "webrtc/tools/simple_command_line_parser.h"
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000012
13#include <cstdio>
14#include <cstdlib>
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000015#include <string>
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000016
17namespace webrtc {
18namespace test {
19
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000020using std::string;
21
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000022void CommandLineParser::Init(int argc, char** argv) {
23 args_ = std::vector<std::string> (argv + 1, argv + argc);
24}
25
26bool CommandLineParser::IsStandaloneFlag(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000027 return flag.find("=") == string::npos;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000028}
29
30bool CommandLineParser::IsFlagWellFormed(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000031 size_t dash_pos = flag.find("--");
32 size_t equal_pos = flag.find("=");
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000033 if (dash_pos != 0) {
34 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
35 fprintf(stderr, "Flag doesn't start with --\n");
36 return false;
37 }
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000038 size_t flag_length = flag.length() - 1;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000039
40 // We use 3 here because we assume that the flags are in the format
41 // --flag_name=flag_value, thus -- are at positions 0 and 1 and we should have
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000042 // at least one symbol for the flag name.
43 if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) {
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000044 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
45 fprintf(stderr, "Wrong placement of =\n");
46 return false;
47 }
48 return true;
49}
50
51std::string CommandLineParser::GetCommandLineFlagName(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000052 size_t dash_pos = flag.find("--");
53 size_t equal_pos = flag.find("=");
54 if (equal_pos == string::npos) {
55 return flag.substr(dash_pos + 2);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000056 } else {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000057 return flag.substr(dash_pos + 2, equal_pos - 2);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000058 }
59}
60
61std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000062 size_t equal_pos = flag.find("=");
63 if (equal_pos == string::npos) {
64 return "";
65 } else {
66 return flag.substr(equal_pos + 1);
67 }
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000068}
69
70void CommandLineParser::PrintEnteredFlags() {
71 std::map<std::string, std::string>::iterator flag_iter;
72 fprintf(stdout, "You have entered:\n");
73 for (flag_iter = flags_.begin(); flag_iter != flags_.end(); ++flag_iter) {
74 if (flag_iter->first != "help") {
75 fprintf(stdout, "%s=%s, ", flag_iter->first.c_str(),
76 flag_iter->second.c_str());
77 }
78 }
79 fprintf(stdout, "\n");
80}
81
82void CommandLineParser::ProcessFlags() {
83 std::map<std::string, std::string>::iterator flag_iter;
84 std::vector<std::string>::iterator iter;
85 for (iter = args_.begin(); iter != args_.end(); ++iter) {
86 if (!IsFlagWellFormed(*iter)) {
87 // Ignore badly formated flags.
88 continue;
89 }
90 std::string flag_name = GetCommandLineFlagName(*iter);
91 flag_iter = flags_.find(flag_name);
92 if (flag_iter == flags_.end()) {
93 // Ignore unknown flags.
94 fprintf(stdout, "Flag '%s' is not recognized\n", flag_name.c_str());
95 continue;
96 }
97 if (IsStandaloneFlag(*iter)) {
98 flags_[flag_name] = "true";
99 } else {
100 flags_[flag_name] = GetCommandLineFlagValue(*iter);
101 }
102 }
103}
104
105void CommandLineParser::SetUsageMessage(std::string usage_message) {
106 usage_message_ = usage_message;
107}
108
109void CommandLineParser::PrintUsageMessage() {
110 fprintf(stdout, "%s", usage_message_.c_str());
111}
112
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +0000113void CommandLineParser::SetFlag(std::string flag_name,
114 std::string default_flag_value) {
115 flags_[flag_name] = default_flag_value;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000116}
117
118std::string CommandLineParser::GetFlag(std::string flag_name) {
119 std::map<std::string, std::string>::iterator flag_iter;
120 flag_iter = flags_.find(flag_name);
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +0000121 // If no such flag.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000122 if (flag_iter == flags_.end()) {
123 return "";
124 }
125 return flag_iter->second;
126}
127
128} // namespace test
129} // namespace webrtc