| OLD | NEW |
| (Empty) |
| 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 | |
| 11 #include "webrtc/tools/simple_command_line_parser.h" | |
| 12 | |
| 13 #include <stdio.h> | |
| 14 #include <stdlib.h> | |
| 15 | |
| 16 #include <string> | |
| 17 | |
| 18 namespace webrtc { | |
| 19 namespace test { | |
| 20 | |
| 21 CommandLineParser::CommandLineParser() {} | |
| 22 CommandLineParser::~CommandLineParser() {} | |
| 23 | |
| 24 void CommandLineParser::Init(int argc, char** argv) { | |
| 25 args_ = std::vector<std::string> (argv + 1, argv + argc); | |
| 26 } | |
| 27 | |
| 28 bool CommandLineParser::IsStandaloneFlag(std::string flag) { | |
| 29 return flag.find("=") == std::string::npos; | |
| 30 } | |
| 31 | |
| 32 bool CommandLineParser::IsFlagWellFormed(std::string flag) { | |
| 33 size_t dash_pos = flag.find("--"); | |
| 34 size_t equal_pos = flag.find("="); | |
| 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 } | |
| 40 size_t flag_length = flag.length() - 1; | |
| 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 | |
| 44 // at least one symbol for the flag name. | |
| 45 if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) { | |
| 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) { | |
| 54 size_t dash_pos = flag.find("--"); | |
| 55 size_t equal_pos = flag.find("="); | |
| 56 if (equal_pos == std::string::npos) { | |
| 57 return flag.substr(dash_pos + 2); | |
| 58 } else { | |
| 59 return flag.substr(dash_pos + 2, equal_pos - 2); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) { | |
| 64 size_t equal_pos = flag.find("="); | |
| 65 if (equal_pos == std::string::npos) { | |
| 66 return ""; | |
| 67 } else { | |
| 68 return flag.substr(equal_pos + 1); | |
| 69 } | |
| 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 | |
| 115 void CommandLineParser::SetFlag(std::string flag_name, | |
| 116 std::string default_flag_value) { | |
| 117 flags_[flag_name] = default_flag_value; | |
| 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); | |
| 123 // If no such flag. | |
| 124 if (flag_iter == flags_.end()) { | |
| 125 return ""; | |
| 126 } | |
| 127 return flag_iter->second; | |
| 128 } | |
| 129 | |
| 130 } // namespace test | |
| 131 } // namespace webrtc | |
| OLD | NEW |