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 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 14 | #include <map> |
pbos@webrtc.org | cff5c03 | 2013-06-04 08:14:10 +0000 | [diff] [blame] | 15 | #include <string> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
henrike@webrtc.org | 774b3d3 | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 18 | #include "webrtc/base/constructormagic.h" |
pbos@webrtc.org | cff5c03 | 2013-06-04 08:14:10 +0000 | [diff] [blame] | 19 | #include "webrtc/test/testsupport/gtest_prod_util.h" |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 20 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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. |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 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. |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | namespace test { |
| 34 | |
| 35 | class CommandLineParser { |
| 36 | public: |
pbos@webrtc.org | 08a3b0d | 2013-07-30 12:50:59 +0000 | [diff] [blame] | 37 | CommandLineParser(); |
| 38 | ~CommandLineParser(); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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. |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 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); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 60 | |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 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. |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 78 | // Checks whether the flag is in the format --flag_name=flag_value. |
| 79 | // or just --flag_name. |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 80 | bool IsFlagWellFormed(std::string flag); |
| 81 | |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 82 | // Extracts the flag name from the flag, i.e. return foo for --foo=bar. |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 83 | std::string GetCommandLineFlagName(std::string flag); |
| 84 | |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 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. |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 87 | std::string GetCommandLineFlagValue(std::string flag); |
kjellander@webrtc.org | f6a2efa | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 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 | DISALLOW_COPY_AND_ASSIGN(CommandLineParser); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } // namespace test |
| 98 | } // namespace webrtc |
| 99 | |
| 100 | #endif // WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_ |