blob: 0ad5e602e8d1007adcacb5e59d5e84de1615d075 [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
11#include "tools/simple_command_line_parser.h"
12
13#include <cstdio>
14#include <cstdlib>
15
16namespace webrtc {
17namespace test {
18
19void CommandLineParser::Init(int argc, char** argv) {
20 args_ = std::vector<std::string> (argv + 1, argv + argc);
21}
22
23bool CommandLineParser::IsStandaloneFlag(std::string flag) {
24 int equal_pos = flag.find("=");
25
26 if (equal_pos < 0) {
27 return true;
28 }
29 return false;
30}
31
32bool CommandLineParser::IsFlagWellFormed(std::string flag) {
33 int dash_pos = flag.find("--");
34 int equal_pos = flag.find("=");
35
36 if (dash_pos != 0) {
37 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
38 fprintf(stderr, "Flag doesn't start with --\n");
39 return false;
40 }
41
42 int flag_length = flag.length() - 1;
43
44 // We use 3 here because we assume that the flags are in the format
45 // --flag_name=flag_value, thus -- are at positions 0 and 1 and we should have
46 // at least one symbor for the flag name.
47 if (equal_pos >= 0 && (equal_pos < 3 || equal_pos == flag_length)) {
48 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
49 fprintf(stderr, "Wrong placement of =\n");
50 return false;
51 }
52 return true;
53}
54
55std::string CommandLineParser::GetCommandLineFlagName(std::string flag) {
56 int dash_pos = flag.find("--");
57 int equal_pos = flag.find("=");
58
59 if (equal_pos < 0) {
60 return flag.substr(dash_pos+2);
61 } else {
62 return flag.substr(dash_pos+2, equal_pos-2);
63 }
64}
65
66std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) {
67 int equal_pos = flag.find("=");
68
69 return flag.substr(equal_pos+1);
70}
71
72void CommandLineParser::PrintEnteredFlags() {
73 std::map<std::string, std::string>::iterator flag_iter;
74 fprintf(stdout, "You have entered:\n");
75 for (flag_iter = flags_.begin(); flag_iter != flags_.end(); ++flag_iter) {
76 if (flag_iter->first != "help") {
77 fprintf(stdout, "%s=%s, ", flag_iter->first.c_str(),
78 flag_iter->second.c_str());
79 }
80 }
81 fprintf(stdout, "\n");
82}
83
84void CommandLineParser::ProcessFlags() {
85 std::map<std::string, std::string>::iterator flag_iter;
86 std::vector<std::string>::iterator iter;
87 for (iter = args_.begin(); iter != args_.end(); ++iter) {
88 if (!IsFlagWellFormed(*iter)) {
89 // Ignore badly formated flags.
90 continue;
91 }
92 std::string flag_name = GetCommandLineFlagName(*iter);
93 flag_iter = flags_.find(flag_name);
94 if (flag_iter == flags_.end()) {
95 // Ignore unknown flags.
96 fprintf(stdout, "Flag '%s' is not recognized\n", flag_name.c_str());
97 continue;
98 }
99 if (IsStandaloneFlag(*iter)) {
100 flags_[flag_name] = "true";
101 } else {
102 flags_[flag_name] = GetCommandLineFlagValue(*iter);
103 }
104 }
105}
106
107void CommandLineParser::SetUsageMessage(std::string usage_message) {
108 usage_message_ = usage_message;
109}
110
111void CommandLineParser::PrintUsageMessage() {
112 fprintf(stdout, "%s", usage_message_.c_str());
113}
114
115void CommandLineParser::SetFlag(std::string flag_name, std::string flag_value) {
116 flags_[flag_name] = flag_value;
117}
118
119std::string CommandLineParser::GetFlag(std::string flag_name) {
120 std::map<std::string, std::string>::iterator flag_iter;
121 flag_iter = flags_.find(flag_name);
122 // If no such file.
123 if (flag_iter == flags_.end()) {
124 return "";
125 }
126 return flag_iter->second;
127}
128
129} // namespace test
130} // namespace webrtc