Remove Simple Command Line Parser.
All the usages of this flag library have been replaced by ABSL_FLAG. Bug: webrtc:10616 Change-Id: I7b50772654d68e80055d79f46ef5bc0633e75508 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/143482 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Cr-Commit-Position: refs/heads/master@{#28401}
This commit is contained in:
parent
4ba04b7740
commit
a3f3ab91dd
@ -15,7 +15,6 @@ group("rtc_tools") {
|
||||
testonly = true
|
||||
|
||||
deps = [
|
||||
":command_line_parser",
|
||||
":frame_analyzer",
|
||||
":video_file_reader",
|
||||
":video_quality_analysis",
|
||||
@ -49,17 +48,6 @@ group("rtc_tools") {
|
||||
}
|
||||
}
|
||||
|
||||
rtc_static_library("command_line_parser") {
|
||||
sources = [
|
||||
"simple_command_line_parser.cc",
|
||||
"simple_command_line_parser.h",
|
||||
]
|
||||
deps = [
|
||||
"../rtc_base:gtest_prod",
|
||||
"../rtc_base:rtc_base_approved",
|
||||
]
|
||||
}
|
||||
|
||||
rtc_static_library("video_file_reader") {
|
||||
sources = [
|
||||
"video_file_reader.cc",
|
||||
@ -419,13 +407,11 @@ if (rtc_include_tests) {
|
||||
"frame_analyzer/video_temporal_aligner_unittest.cc",
|
||||
"frame_editing/frame_editing_unittest.cc",
|
||||
"sanitizers_unittest.cc",
|
||||
"simple_command_line_parser_unittest.cc",
|
||||
"video_file_reader_unittest.cc",
|
||||
"video_file_writer_unittest.cc",
|
||||
]
|
||||
|
||||
deps = [
|
||||
":command_line_parser",
|
||||
":video_file_reader",
|
||||
":video_file_writer",
|
||||
":video_quality_analysis",
|
||||
|
||||
@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "rtc_tools/simple_command_line_parser.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
CommandLineParser::CommandLineParser() {}
|
||||
CommandLineParser::~CommandLineParser() {}
|
||||
|
||||
void CommandLineParser::Init(int argc, char** argv) {
|
||||
args_ = std::vector<std::string>(argv + 1, argv + argc);
|
||||
}
|
||||
|
||||
bool CommandLineParser::IsStandaloneFlag(std::string flag) {
|
||||
return flag.find('=') == std::string::npos;
|
||||
}
|
||||
|
||||
bool CommandLineParser::IsFlagWellFormed(std::string flag) {
|
||||
size_t dash_pos = flag.find("--");
|
||||
size_t equal_pos = flag.find('=');
|
||||
if (dash_pos != 0) {
|
||||
fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
|
||||
fprintf(stderr, "Flag doesn't start with --\n");
|
||||
return false;
|
||||
}
|
||||
size_t flag_length = flag.length() - 1;
|
||||
|
||||
// We use 3 here because we assume that the flags are in the format
|
||||
// --flag_name=flag_value, thus -- are at positions 0 and 1 and we should have
|
||||
// at least one symbol for the flag name.
|
||||
if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) {
|
||||
fprintf(stderr, "Wrong switch format: %s\n", flag.c_str());
|
||||
fprintf(stderr, "Wrong placement of =\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string CommandLineParser::GetCommandLineFlagName(std::string flag) {
|
||||
size_t dash_pos = flag.find("--");
|
||||
size_t equal_pos = flag.find('=');
|
||||
if (equal_pos == std::string::npos) {
|
||||
return flag.substr(dash_pos + 2);
|
||||
} else {
|
||||
return flag.substr(dash_pos + 2, equal_pos - 2);
|
||||
}
|
||||
}
|
||||
|
||||
std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) {
|
||||
size_t equal_pos = flag.find('=');
|
||||
if (equal_pos == std::string::npos) {
|
||||
return "";
|
||||
} else {
|
||||
return flag.substr(equal_pos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void CommandLineParser::PrintEnteredFlags() {
|
||||
std::map<std::string, std::string>::iterator flag_iter;
|
||||
fprintf(stdout, "You have entered:\n");
|
||||
for (flag_iter = flags_.begin(); flag_iter != flags_.end(); ++flag_iter) {
|
||||
if (flag_iter->first != "help") {
|
||||
fprintf(stdout, "%s=%s, ", flag_iter->first.c_str(),
|
||||
flag_iter->second.c_str());
|
||||
}
|
||||
}
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
|
||||
void CommandLineParser::ProcessFlags() {
|
||||
std::map<std::string, std::string>::iterator flag_iter;
|
||||
std::vector<std::string>::iterator iter;
|
||||
for (iter = args_.begin(); iter != args_.end(); ++iter) {
|
||||
if (!IsFlagWellFormed(*iter)) {
|
||||
// Ignore badly formated flags.
|
||||
continue;
|
||||
}
|
||||
std::string flag_name = GetCommandLineFlagName(*iter);
|
||||
flag_iter = flags_.find(flag_name);
|
||||
if (flag_iter == flags_.end()) {
|
||||
// Ignore unknown flags.
|
||||
fprintf(stdout, "Flag '%s' is not recognized\n", flag_name.c_str());
|
||||
continue;
|
||||
}
|
||||
if (IsStandaloneFlag(*iter)) {
|
||||
flags_[flag_name] = "true";
|
||||
} else {
|
||||
flags_[flag_name] = GetCommandLineFlagValue(*iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CommandLineParser::SetUsageMessage(std::string usage_message) {
|
||||
usage_message_ = usage_message;
|
||||
}
|
||||
|
||||
void CommandLineParser::PrintUsageMessage() {
|
||||
fprintf(stdout, "%s", usage_message_.c_str());
|
||||
}
|
||||
|
||||
void CommandLineParser::SetFlag(std::string flag_name,
|
||||
std::string default_flag_value) {
|
||||
flags_[flag_name] = default_flag_value;
|
||||
}
|
||||
|
||||
std::string CommandLineParser::GetFlag(std::string flag_name) {
|
||||
std::map<std::string, std::string>::iterator flag_iter;
|
||||
flag_iter = flags_.find(flag_name);
|
||||
// If no such flag.
|
||||
if (flag_iter == flags_.end()) {
|
||||
return "";
|
||||
}
|
||||
return flag_iter->second;
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
@ -1,100 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef RTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
|
||||
#define RTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "rtc_base/constructor_magic.h"
|
||||
#include "rtc_base/gtest_prod_util.h"
|
||||
|
||||
// This is a very basic command line parsing class. We pass the command line
|
||||
// arguments and their number and the class forms a vector out of these. Than we
|
||||
// should set up the flags - we provide a name and a string value and map these.
|
||||
//
|
||||
// Example use of this class:
|
||||
// 1. Create a CommandLineParser object.
|
||||
// 2. Configure the flags you want to support with SetFlag calls.
|
||||
// 3. Call Init with your program's argc+argv parameters.
|
||||
// 4. Parse the flags by calling ProcessFlags.
|
||||
// 5. Get the values of the flags using GetFlag.
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
class CommandLineParser {
|
||||
public:
|
||||
CommandLineParser();
|
||||
~CommandLineParser();
|
||||
|
||||
void Init(int argc, char** argv);
|
||||
|
||||
// Prints the entered flags and their values (without --help).
|
||||
void PrintEnteredFlags();
|
||||
|
||||
// Processes the vector of command line arguments and puts the value of each
|
||||
// flag in the corresponding map entry for this flag's name. We don't process
|
||||
// flags which haven't been defined in the map.
|
||||
void ProcessFlags();
|
||||
|
||||
// Sets the usage message to be shown if we pass --help.
|
||||
void SetUsageMessage(std::string usage_message);
|
||||
|
||||
// prints the usage message.
|
||||
void PrintUsageMessage();
|
||||
|
||||
// Set a flag into the map of flag names/values.
|
||||
// To set a boolean flag, use "false" as the default flag value.
|
||||
// The flag_name should not include the -- prefix.
|
||||
void SetFlag(std::string flag_name, std::string default_flag_value);
|
||||
|
||||
// Gets a flag when provided a flag name (name is without the -- prefix).
|
||||
// Returns "" if the flag is unknown and "true"/"false" if the flag is a
|
||||
// boolean flag.
|
||||
std::string GetFlag(std::string flag_name);
|
||||
|
||||
private:
|
||||
// The vector of passed command line arguments.
|
||||
std::vector<std::string> args_;
|
||||
// The map of the flag names/values.
|
||||
std::map<std::string, std::string> flags_;
|
||||
// The usage message.
|
||||
std::string usage_message_;
|
||||
|
||||
// Returns whether the passed flag is standalone or not. By standalone we
|
||||
// understand e.g. --standalone (in contrast to --non_standalone=1).
|
||||
bool IsStandaloneFlag(std::string flag);
|
||||
|
||||
// Checks whether the flag is in the format --flag_name=flag_value.
|
||||
// or just --flag_name.
|
||||
bool IsFlagWellFormed(std::string flag);
|
||||
|
||||
// Extracts the flag name from the flag, i.e. return foo for --foo=bar.
|
||||
std::string GetCommandLineFlagName(std::string flag);
|
||||
|
||||
// Extracts the flag value from the flag, i.e. return bar for --foo=bar.
|
||||
// If the flag has no value (i.e. no equals sign) an empty string is returned.
|
||||
std::string GetCommandLineFlagValue(std::string flag);
|
||||
|
||||
FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, IsStandaloneFlag);
|
||||
FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, IsFlagWellFormed);
|
||||
FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, GetCommandLineFlagName);
|
||||
FRIEND_TEST_ALL_PREFIXES(CommandLineParserTest, GetCommandLineFlagValue);
|
||||
|
||||
RTC_DISALLOW_COPY_AND_ASSIGN(CommandLineParser);
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // RTC_TOOLS_SIMPLE_COMMAND_LINE_PARSER_H_
|
||||
@ -1,89 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "rtc_tools/simple_command_line_parser.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "test/gtest.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
class CommandLineParserTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
parser_ = new CommandLineParser();
|
||||
|
||||
test_flags_length_ = 3;
|
||||
int flag_size = 32;
|
||||
test_flags_ = new char*[test_flags_length_];
|
||||
for (int i = 0; i < test_flags_length_; ++i) {
|
||||
test_flags_[i] = new char[flag_size];
|
||||
}
|
||||
strncpy(test_flags_[0], "tools_unittest", flag_size);
|
||||
strncpy(test_flags_[1], "--foo", flag_size);
|
||||
strncpy(test_flags_[2], "--bar=1", flag_size);
|
||||
}
|
||||
void TearDown() override {
|
||||
for (int i = 0; i < test_flags_length_; ++i) {
|
||||
delete[] test_flags_[i];
|
||||
}
|
||||
delete[] test_flags_;
|
||||
delete parser_;
|
||||
}
|
||||
CommandLineParser* parser_;
|
||||
// Test flags to emulate a program's argv arguments.
|
||||
char** test_flags_;
|
||||
int test_flags_length_;
|
||||
};
|
||||
|
||||
TEST_F(CommandLineParserTest, ProcessFlags) {
|
||||
// Setup supported flags to parse.
|
||||
parser_->SetFlag("foo", "false");
|
||||
parser_->SetFlag("foo-foo", "false"); // To test boolean flags defaults.
|
||||
parser_->SetFlag("bar", "222");
|
||||
parser_->SetFlag("baz", "333"); // To test the default value functionality.
|
||||
|
||||
parser_->Init(test_flags_length_, test_flags_);
|
||||
parser_->ProcessFlags();
|
||||
EXPECT_EQ("true", parser_->GetFlag("foo"));
|
||||
EXPECT_EQ("false", parser_->GetFlag("foo-foo"));
|
||||
EXPECT_EQ("1", parser_->GetFlag("bar"));
|
||||
EXPECT_EQ("333", parser_->GetFlag("baz"));
|
||||
EXPECT_EQ("", parser_->GetFlag("unknown"));
|
||||
}
|
||||
|
||||
TEST_F(CommandLineParserTest, IsStandaloneFlag) {
|
||||
EXPECT_TRUE(parser_->IsStandaloneFlag("--foo"));
|
||||
EXPECT_TRUE(parser_->IsStandaloneFlag("--foo-foo"));
|
||||
EXPECT_FALSE(parser_->IsStandaloneFlag("--foo=1"));
|
||||
}
|
||||
|
||||
TEST_F(CommandLineParserTest, IsFlagWellFormed) {
|
||||
EXPECT_TRUE(parser_->IsFlagWellFormed("--foo"));
|
||||
EXPECT_TRUE(parser_->IsFlagWellFormed("--foo-foo"));
|
||||
EXPECT_TRUE(parser_->IsFlagWellFormed("--bar=1"));
|
||||
}
|
||||
|
||||
TEST_F(CommandLineParserTest, GetCommandLineFlagName) {
|
||||
EXPECT_EQ("foo", parser_->GetCommandLineFlagName("--foo"));
|
||||
EXPECT_EQ("foo-foo", parser_->GetCommandLineFlagName("--foo-foo"));
|
||||
EXPECT_EQ("bar", parser_->GetCommandLineFlagName("--bar=1"));
|
||||
}
|
||||
|
||||
TEST_F(CommandLineParserTest, GetCommandLineFlagValue) {
|
||||
EXPECT_EQ("", parser_->GetCommandLineFlagValue("--foo"));
|
||||
EXPECT_EQ("", parser_->GetCommandLineFlagValue("--foo-foo"));
|
||||
EXPECT_EQ("1", parser_->GetCommandLineFlagValue("--bar=1"));
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
Loading…
x
Reference in New Issue
Block a user