blob: 62aba3d04f744abb35fba5e497f3fe5f368e26b1 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +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.orgf6a2efa2013-01-26 16:36:40 +000011#include "webrtc/tools/simple_command_line_parser.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000012
pbos@webrtc.org3f45c2e2013-08-05 16:22:53 +000013#include <stdio.h>
14#include <stdlib.h>
15
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000016#include <string>
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000017
18namespace webrtc {
19namespace test {
20
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000021using std::string;
22
pbos@webrtc.org08a3b0d2013-07-30 12:50:59 +000023CommandLineParser::CommandLineParser() {}
24CommandLineParser::~CommandLineParser() {}
25
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000026void CommandLineParser::Init(int argc, char** argv) {
27 args_ = std::vector<std::string> (argv + 1, argv + argc);
28}
29
30bool CommandLineParser::IsStandaloneFlag(std::string flag) {
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000031 return flag.find("=") == string::npos;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000032}
33
34bool CommandLineParser::IsFlagWellFormed(std::string flag) {
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000035 size_t dash_pos = flag.find("--");
36 size_t equal_pos = flag.find("=");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000037 if (dash_pos != 0) {
38 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
39 fprintf(stderr, "Flag doesn't start with --\n");
40 return false;
41 }
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000042 size_t flag_length = flag.length() - 1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000043
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
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000046 // at least one symbol for the flag name.
47 if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000048 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) {
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000056 size_t dash_pos = flag.find("--");
57 size_t equal_pos = flag.find("=");
58 if (equal_pos == string::npos) {
59 return flag.substr(dash_pos + 2);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000060 } else {
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000061 return flag.substr(dash_pos + 2, equal_pos - 2);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000062 }
63}
64
65std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) {
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000066 size_t equal_pos = flag.find("=");
67 if (equal_pos == string::npos) {
68 return "";
69 } else {
70 return flag.substr(equal_pos + 1);
71 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000072}
73
74void CommandLineParser::PrintEnteredFlags() {
75 std::map<std::string, std::string>::iterator flag_iter;
76 fprintf(stdout, "You have entered:\n");
77 for (flag_iter = flags_.begin(); flag_iter != flags_.end(); ++flag_iter) {
78 if (flag_iter->first != "help") {
79 fprintf(stdout, "%s=%s, ", flag_iter->first.c_str(),
80 flag_iter->second.c_str());
81 }
82 }
83 fprintf(stdout, "\n");
84}
85
86void CommandLineParser::ProcessFlags() {
87 std::map<std::string, std::string>::iterator flag_iter;
88 std::vector<std::string>::iterator iter;
89 for (iter = args_.begin(); iter != args_.end(); ++iter) {
90 if (!IsFlagWellFormed(*iter)) {
91 // Ignore badly formated flags.
92 continue;
93 }
94 std::string flag_name = GetCommandLineFlagName(*iter);
95 flag_iter = flags_.find(flag_name);
96 if (flag_iter == flags_.end()) {
97 // Ignore unknown flags.
98 fprintf(stdout, "Flag '%s' is not recognized\n", flag_name.c_str());
99 continue;
100 }
101 if (IsStandaloneFlag(*iter)) {
102 flags_[flag_name] = "true";
103 } else {
104 flags_[flag_name] = GetCommandLineFlagValue(*iter);
105 }
106 }
107}
108
109void CommandLineParser::SetUsageMessage(std::string usage_message) {
110 usage_message_ = usage_message;
111}
112
113void CommandLineParser::PrintUsageMessage() {
114 fprintf(stdout, "%s", usage_message_.c_str());
115}
116
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +0000117void CommandLineParser::SetFlag(std::string flag_name,
118 std::string default_flag_value) {
119 flags_[flag_name] = default_flag_value;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000120}
121
122std::string CommandLineParser::GetFlag(std::string flag_name) {
123 std::map<std::string, std::string>::iterator flag_iter;
124 flag_iter = flags_.find(flag_name);
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +0000125 // If no such flag.
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000126 if (flag_iter == flags_.end()) {
127 return "";
128 }
129 return flag_iter->second;
130}
131
132} // namespace test
133} // namespace webrtc