blob: 6bb33137f50f890483528cad3ad1e092b3e795cd [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
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.orgb015cbe2012-10-22 18:19:23 +000014#include <map>
pbos@webrtc.orgcff5c032013-06-04 08:14:10 +000015#include <string>
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000016#include <vector>
17
henrike@webrtc.org774b3d32014-05-21 21:18:46 +000018#include "webrtc/base/constructormagic.h"
pbos@webrtc.orgcff5c032013-06-04 08:14:10 +000019#include "webrtc/test/testsupport/gtest_prod_util.h"
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000020
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000021// 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.orgf6a2efa2013-01-26 16:36:40 +000024//
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.orgb015cbe2012-10-22 18:19:23 +000031
32namespace webrtc {
33namespace test {
34
35class CommandLineParser {
36 public:
pbos@webrtc.org08a3b0d2013-07-30 12:50:59 +000037 CommandLineParser();
38 ~CommandLineParser();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000039
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.orgf6a2efa2013-01-26 16:36:40 +000057 // 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.orgb015cbe2012-10-22 18:19:23 +000060
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000061 // 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.orgb015cbe2012-10-22 18:19:23 +000064 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.orgf6a2efa2013-01-26 16:36:40 +000078 // Checks whether the flag is in the format --flag_name=flag_value.
79 // or just --flag_name.
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000080 bool IsFlagWellFormed(std::string flag);
81
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000082 // Extracts the flag name from the flag, i.e. return foo for --foo=bar.
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000083 std::string GetCommandLineFlagName(std::string flag);
84
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000085 // 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.orgb015cbe2012-10-22 18:19:23 +000087 std::string GetCommandLineFlagValue(std::string flag);
kjellander@webrtc.orgf6a2efa2013-01-26 16:36:40 +000088
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.orgb015cbe2012-10-22 18:19:23 +000095};
96
97} // namespace test
98} // namespace webrtc
99
100#endif // WEBRTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_