• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using std::string;
22 
CommandLineParser()23 CommandLineParser::CommandLineParser() {}
~CommandLineParser()24 CommandLineParser::~CommandLineParser() {}
25 
Init(int argc,char ** argv)26 void CommandLineParser::Init(int argc, char** argv) {
27   args_ = std::vector<std::string> (argv + 1, argv + argc);
28 }
29 
IsStandaloneFlag(std::string flag)30 bool CommandLineParser::IsStandaloneFlag(std::string flag) {
31   return flag.find("=") == string::npos;
32 }
33 
IsFlagWellFormed(std::string flag)34 bool CommandLineParser::IsFlagWellFormed(std::string flag) {
35   size_t dash_pos = flag.find("--");
36   size_t equal_pos = flag.find("=");
37   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   }
42   size_t flag_length = flag.length() - 1;
43 
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
46   // at least one symbol for the flag name.
47   if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) {
48     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 
GetCommandLineFlagName(std::string flag)55 std::string CommandLineParser::GetCommandLineFlagName(std::string flag) {
56   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);
60   } else {
61     return flag.substr(dash_pos + 2, equal_pos - 2);
62   }
63 }
64 
GetCommandLineFlagValue(std::string flag)65 std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) {
66   size_t equal_pos = flag.find("=");
67   if (equal_pos == string::npos) {
68     return "";
69   } else {
70     return flag.substr(equal_pos + 1);
71   }
72 }
73 
PrintEnteredFlags()74 void 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 
ProcessFlags()86 void 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 
SetUsageMessage(std::string usage_message)109 void CommandLineParser::SetUsageMessage(std::string usage_message) {
110   usage_message_ = usage_message;
111 }
112 
PrintUsageMessage()113 void CommandLineParser::PrintUsageMessage() {
114   fprintf(stdout, "%s", usage_message_.c_str());
115 }
116 
SetFlag(std::string flag_name,std::string default_flag_value)117 void CommandLineParser::SetFlag(std::string flag_name,
118                                 std::string default_flag_value) {
119   flags_[flag_name] = default_flag_value;
120 }
121 
GetFlag(std::string flag_name)122 std::string CommandLineParser::GetFlag(std::string flag_name) {
123   std::map<std::string, std::string>::iterator flag_iter;
124   flag_iter = flags_.find(flag_name);
125   // If no such flag.
126   if (flag_iter == flags_.end()) {
127     return "";
128   }
129   return flag_iter->second;
130 }
131 
132 }  // namespace test
133 }  // namespace webrtc
134