blob: b20ba6481f68fc30e710c0b7241f4c7edea9d842 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_tools/simple_command_line_parser.h"
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdio.h>
14#include <stdlib.h>
15
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000016#include <string>
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000017
18namespace webrtc {
19namespace test {
20
pbos@webrtc.orga6f56ac2013-07-30 12:50:59 +000021CommandLineParser::CommandLineParser() {}
22CommandLineParser::~CommandLineParser() {}
23
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000024void CommandLineParser::Init(int argc, char** argv) {
25 args_ = std::vector<std::string> (argv + 1, argv + argc);
26}
27
28bool CommandLineParser::IsStandaloneFlag(std::string flag) {
ehmaldonado1dffc622017-02-02 08:10:00 -080029 return flag.find("=") == std::string::npos;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000030}
31
32bool CommandLineParser::IsFlagWellFormed(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000033 size_t dash_pos = flag.find("--");
34 size_t equal_pos = flag.find("=");
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000035 if (dash_pos != 0) {
36 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
37 fprintf(stderr, "Flag doesn't start with --\n");
38 return false;
39 }
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000040 size_t flag_length = flag.length() - 1;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000041
42 // We use 3 here because we assume that the flags are in the format
43 // --flag_name=flag_value, thus -- are at positions 0 and 1 and we should have
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000044 // at least one symbol for the flag name.
45 if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) {
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000046 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
47 fprintf(stderr, "Wrong placement of =\n");
48 return false;
49 }
50 return true;
51}
52
53std::string CommandLineParser::GetCommandLineFlagName(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000054 size_t dash_pos = flag.find("--");
55 size_t equal_pos = flag.find("=");
ehmaldonado1dffc622017-02-02 08:10:00 -080056 if (equal_pos == std::string::npos) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000057 return flag.substr(dash_pos + 2);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000058 } else {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000059 return flag.substr(dash_pos + 2, equal_pos - 2);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000060 }
61}
62
63std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000064 size_t equal_pos = flag.find("=");
ehmaldonado1dffc622017-02-02 08:10:00 -080065 if (equal_pos == std::string::npos) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000066 return "";
67 } else {
68 return flag.substr(equal_pos + 1);
69 }
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000070}
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
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +0000115void CommandLineParser::SetFlag(std::string flag_name,
116 std::string default_flag_value) {
117 flags_[flag_name] = default_flag_value;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000118}
119
120std::string CommandLineParser::GetFlag(std::string flag_name) {
121 std::map<std::string, std::string>::iterator flag_iter;
122 flag_iter = flags_.find(flag_name);
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +0000123 // If no such flag.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000124 if (flag_iter == flags_.end()) {
125 return "";
126 }
127 return flag_iter->second;
128}
129
130} // namespace test
131} // namespace webrtc