• 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 #ifndef WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
12 #define WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 #include "webrtc/base/constructormagic.h"
19 #include "webrtc/test/testsupport/gtest_prod_util.h"
20 
21 // This is a very basic command line parsing class. We pass the command line
22 // arguments and their number and the class forms a vector out of these. Than we
23 // should set up the flags - we provide a name and a string value and map these.
24 //
25 // Example use of this class:
26 // 1. Create a CommandLineParser object.
27 // 2. Configure the flags you want to support with SetFlag calls.
28 // 3. Call Init with your program's argc+argv parameters.
29 // 4. Parse the flags by calling ProcessFlags.
30 // 5. Get the values of the flags using GetFlag.
31 
32 namespace webrtc {
33 namespace test {
34 
35 class CommandLineParser {
36  public:
37   CommandLineParser();
38   ~CommandLineParser();
39 
40   void Init(int argc, char** argv);
41 
42   // Prints the entered flags and their values (without --help).
43   void PrintEnteredFlags();
44 
45   // Processes the vector of command line arguments and puts the value of each
46   // flag in the corresponding map entry for this flag's name. We don't process
47   // flags which haven't been defined in the map.
48   void ProcessFlags();
49 
50   // Sets the usage message to be shown if we pass --help.
51   void SetUsageMessage(std::string usage_message);
52 
53   // prints the usage message.
54   void PrintUsageMessage();
55 
56   // Set a flag into the map of flag names/values.
57   // To set a boolean flag, use "false" as the default flag value.
58   // The flag_name should not include the -- prefix.
59   void SetFlag(std::string flag_name, std::string default_flag_value);
60 
61   // Gets a flag when provided a flag name (name is without the -- prefix).
62   // Returns "" if the flag is unknown and "true"/"false" if the flag is a
63   // boolean flag.
64   std::string GetFlag(std::string flag_name);
65 
66  private:
67   // The vector of passed command line arguments.
68   std::vector<std::string> args_;
69   // The map of the flag names/values.
70   std::map<std::string, std::string> flags_;
71   // The usage message.
72   std::string usage_message_;
73 
74   // Returns whether the passed flag is standalone or not. By standalone we
75   // understand e.g. --standalone (in contrast to --non_standalone=1).
76   bool IsStandaloneFlag(std::string flag);
77 
78   // Checks whether the flag is in the format --flag_name=flag_value.
79   // or just --flag_name.
80   bool IsFlagWellFormed(std::string flag);
81 
82   // Extracts the flag name from the flag, i.e. return foo for --foo=bar.
83   std::string GetCommandLineFlagName(std::string flag);
84 
85   // Extracts the flag value from the flag, i.e. return bar for --foo=bar.
86   // If the flag has no value (i.e. no equals sign) an empty string is returned.
87   std::string GetCommandLineFlagValue(std::string flag);
88 
89   FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, IsStandaloneFlag);
90   FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, IsFlagWellFormed);
91   FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, GetCommandLineFlagName);
92   FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, GetCommandLineFlagValue);
93 
94   RTC_DISALLOW_COPY_AND_ASSIGN(CommandLineParser);
95 };
96 
97 }  // namespace test
98 }  // namespace webrtc
99 
100 #endif  // WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
101