vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_tools/simple_command_line_parser.h" |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 16 | #include <string> |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | |
pbos@webrtc.org | a6f56ac | 2013-07-30 12:50:59 +0000 | [diff] [blame] | 21 | CommandLineParser::CommandLineParser() {} |
| 22 | CommandLineParser::~CommandLineParser() {} |
| 23 | |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 24 | void CommandLineParser::Init(int argc, char** argv) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 25 | args_ = std::vector<std::string>(argv + 1, argv + argc); |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | bool CommandLineParser::IsStandaloneFlag(std::string flag) { |
ehmaldonado | 1dffc62 | 2017-02-02 08:10:00 -0800 | [diff] [blame] | 29 | return flag.find("=") == std::string::npos; |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | bool CommandLineParser::IsFlagWellFormed(std::string flag) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 33 | size_t dash_pos = flag.find("--"); |
| 34 | size_t equal_pos = flag.find("="); |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 35 | 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.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 40 | size_t flag_length = flag.length() - 1; |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 41 | |
| 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.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 44 | // at least one symbol for the flag name. |
| 45 | if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) { |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 46 | 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 | |
| 53 | std::string CommandLineParser::GetCommandLineFlagName(std::string flag) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 54 | size_t dash_pos = flag.find("--"); |
| 55 | size_t equal_pos = flag.find("="); |
ehmaldonado | 1dffc62 | 2017-02-02 08:10:00 -0800 | [diff] [blame] | 56 | if (equal_pos == std::string::npos) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 57 | return flag.substr(dash_pos + 2); |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 58 | } else { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 59 | return flag.substr(dash_pos + 2, equal_pos - 2); |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 64 | size_t equal_pos = flag.find("="); |
ehmaldonado | 1dffc62 | 2017-02-02 08:10:00 -0800 | [diff] [blame] | 65 | if (equal_pos == std::string::npos) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 66 | return ""; |
| 67 | } else { |
| 68 | return flag.substr(equal_pos + 1); |
| 69 | } |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void 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 | |
| 84 | void 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 | |
| 107 | void CommandLineParser::SetUsageMessage(std::string usage_message) { |
| 108 | usage_message_ = usage_message; |
| 109 | } |
| 110 | |
| 111 | void CommandLineParser::PrintUsageMessage() { |
| 112 | fprintf(stdout, "%s", usage_message_.c_str()); |
| 113 | } |
| 114 | |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 115 | void CommandLineParser::SetFlag(std::string flag_name, |
| 116 | std::string default_flag_value) { |
| 117 | flags_[flag_name] = default_flag_value; |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | std::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.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 123 | // If no such flag. |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 124 | if (flag_iter == flags_.end()) { |
| 125 | return ""; |
| 126 | } |
| 127 | return flag_iter->second; |
| 128 | } |
| 129 | |
| 130 | } // namespace test |
| 131 | } // namespace webrtc |