blob: d6a8c1b7957539c8c981122a8791bf38467404bf [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
pbos@webrtc.orga6f56ac2013-07-30 12:50:59 +000022CommandLineParser::CommandLineParser() {}
23CommandLineParser::~CommandLineParser() {}
24
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000025void CommandLineParser::Init(int argc, char** argv) {
26 args_ = std::vector<std::string> (argv + 1, argv + argc);
27}
28
29bool CommandLineParser::IsStandaloneFlag(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000030 return flag.find("=") == string::npos;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000031}
32
33bool CommandLineParser::IsFlagWellFormed(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000034 size_t dash_pos = flag.find("--");
35 size_t equal_pos = flag.find("=");
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000036 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 }
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000041 size_t flag_length = flag.length() - 1;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000042
43 // We use 3 here because we assume that the flags are in the format
44 // --flag_name=flag_value, thus -- are at positions 0 and 1 and we should have
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000045 // at least one symbol for the flag name.
46 if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) {
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000047 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
48 fprintf(stderr, "Wrong placement of =\n");
49 return false;
50 }
51 return true;
52}
53
54std::string CommandLineParser::GetCommandLineFlagName(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000055 size_t dash_pos = flag.find("--");
56 size_t equal_pos = flag.find("=");
57 if (equal_pos == string::npos) {
58 return flag.substr(dash_pos + 2);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000059 } else {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000060 return flag.substr(dash_pos + 2, equal_pos - 2);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000061 }
62}
63
64std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000065 size_t equal_pos = flag.find("=");
66 if (equal_pos == string::npos) {
67 return "";
68 } else {
69 return flag.substr(equal_pos + 1);
70 }
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000071}
72
73void CommandLineParser::PrintEnteredFlags() {
74 std::map<std::string, std::string>::iterator flag_iter;
75 fprintf(stdout, "You have entered:\n");
76 for (flag_iter = flags_.begin(); flag_iter != flags_.end(); ++flag_iter) {
77 if (flag_iter->first != "help") {
78 fprintf(stdout, "%s=%s, ", flag_iter->first.c_str(),
79 flag_iter->second.c_str());
80 }
81 }
82 fprintf(stdout, "\n");
83}
84
85void CommandLineParser::ProcessFlags() {
86 std::map<std::string, std::string>::iterator flag_iter;
87 std::vector<std::string>::iterator iter;
88 for (iter = args_.begin(); iter != args_.end(); ++iter) {
89 if (!IsFlagWellFormed(*iter)) {
90 // Ignore badly formated flags.
91 continue;
92 }
93 std::string flag_name = GetCommandLineFlagName(*iter);
94 flag_iter = flags_.find(flag_name);
95 if (flag_iter == flags_.end()) {
96 // Ignore unknown flags.
97 fprintf(stdout, "Flag '%s' is not recognized\n", flag_name.c_str());
98 continue;
99 }
100 if (IsStandaloneFlag(*iter)) {
101 flags_[flag_name] = "true";
102 } else {
103 flags_[flag_name] = GetCommandLineFlagValue(*iter);
104 }
105 }
106}
107
108void CommandLineParser::SetUsageMessage(std::string usage_message) {
109 usage_message_ = usage_message;
110}
111
112void CommandLineParser::PrintUsageMessage() {
113 fprintf(stdout, "%s", usage_message_.c_str());
114}
115
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +0000116void CommandLineParser::SetFlag(std::string flag_name,
117 std::string default_flag_value) {
118 flags_[flag_name] = default_flag_value;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000119}
120
121std::string CommandLineParser::GetFlag(std::string flag_name) {
122 std::map<std::string, std::string>::iterator flag_iter;
123 flag_iter = flags_.find(flag_name);
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +0000124 // If no such flag.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000125 if (flag_iter == flags_.end()) {
126 return "";
127 }
128 return flag_iter->second;
129}
130
131} // namespace test
132} // namespace webrtc