blob: 0acaf9eb87ba2537886812723b1e09c3ef7a5f29 [file] [log] [blame]
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
12#define RTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000013
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000014#include <map>
pbos@webrtc.orgba7f6a82013-06-04 08:14:10 +000015#include <string>
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000016#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/constructormagic.h"
19#include "rtc_base/gtest_prod_util.h"
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000020
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +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.orgb2d74972013-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.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000031
32namespace webrtc {
33namespace test {
34
35class CommandLineParser {
36 public:
pbos@webrtc.orga6f56ac2013-07-30 12:50:59 +000037 CommandLineParser();
38 ~CommandLineParser();
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +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.orgb2d74972013-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);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000060
kjellander@webrtc.orgb2d74972013-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.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +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.orgb2d74972013-01-26 16:36:40 +000078 // Checks whether the flag is in the format --flag_name=flag_value.
79 // or just --flag_name.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000080 bool IsFlagWellFormed(std::string flag);
81
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000082 // Extracts the flag name from the flag, i.e. return foo for --foo=bar.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000083 std::string GetCommandLineFlagName(std::string flag);
84
kjellander@webrtc.orgb2d74972013-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.
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000087 std::string GetCommandLineFlagValue(std::string flag);
kjellander@webrtc.orgb2d74972013-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
henrikg3c089d72015-09-16 05:37:44 -070094 RTC_DISALLOW_COPY_AND_ASSIGN(CommandLineParser);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000095};
96
97} // namespace test
98} // namespace webrtc
99
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200100#endif // RTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_