andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +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 | |
| 11 | #ifndef WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_ |
| 12 | #define WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_ |
| 13 | |
| 14 | #include <string> |
| 15 | #include <map> |
| 16 | #include <vector> |
| 17 | |
| 18 | // This is a very basic command line parsing class. We pass the command line |
| 19 | // arguments and their number and the class forms a vector out of these. Than we |
| 20 | // should set up the flags - we provide a name and a string value and map these. |
| 21 | |
| 22 | namespace webrtc { |
| 23 | namespace test { |
| 24 | |
| 25 | class CommandLineParser { |
| 26 | public: |
| 27 | CommandLineParser() {} |
| 28 | ~CommandLineParser() {} |
| 29 | |
| 30 | void Init(int argc, char** argv); |
| 31 | |
| 32 | // Prints the entered flags and their values (without --help). |
| 33 | void PrintEnteredFlags(); |
| 34 | |
| 35 | // Processes the vector of command line arguments and puts the value of each |
| 36 | // flag in the corresponding map entry for this flag's name. We don't process |
| 37 | // flags which haven't been defined in the map. |
| 38 | void ProcessFlags(); |
| 39 | |
| 40 | // Sets the usage message to be shown if we pass --help. |
| 41 | void SetUsageMessage(std::string usage_message); |
| 42 | |
| 43 | // prints the usage message. |
| 44 | void PrintUsageMessage(); |
| 45 | |
| 46 | // Set a flag into the map of flag names/values. |
| 47 | void SetFlag(std::string flag_name, std::string flag_value); |
| 48 | |
| 49 | // Gets a flag when provided a flag name. Returns "" if the flag is unknown. |
| 50 | std::string GetFlag(std::string flag_name); |
| 51 | |
| 52 | private: |
| 53 | // The vector of passed command line arguments. |
| 54 | std::vector<std::string> args_; |
| 55 | // The map of the flag names/values. |
| 56 | std::map<std::string, std::string> flags_; |
| 57 | // The usage message. |
| 58 | std::string usage_message_; |
| 59 | |
| 60 | // Returns whether the passed flag is standalone or not. By standalone we |
| 61 | // understand e.g. --standalone (in contrast to --non_standalone=1). |
| 62 | bool IsStandaloneFlag(std::string flag); |
| 63 | |
| 64 | // Checks weather the flag is in the format --flag_name=flag_value. |
| 65 | bool IsFlagWellFormed(std::string flag); |
| 66 | |
| 67 | // Extracts the flag name from the flag. |
| 68 | std::string GetCommandLineFlagName(std::string flag); |
| 69 | |
| 70 | // Extracts the falg value from the flag. |
| 71 | std::string GetCommandLineFlagValue(std::string flag); |
| 72 | }; |
| 73 | |
| 74 | } // namespace test |
| 75 | } // namespace webrtc |
| 76 | |
| 77 | #endif // WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_ |