2019-05-07 21:56:42 +02:00
|
|
|
//
|
|
|
|
// Copyright 2019 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "absl/flags/internal/usage.h"
|
|
|
|
|
2020-06-17 15:53:10 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-01-10 17:42:35 +01:00
|
|
|
#include <functional>
|
2019-05-07 21:56:42 +02:00
|
|
|
#include <map>
|
2020-01-10 17:42:35 +01:00
|
|
|
#include <ostream>
|
2019-05-07 21:56:42 +02:00
|
|
|
#include <string>
|
2020-01-10 17:42:35 +01:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-01-10 17:42:35 +01:00
|
|
|
#include "absl/base/config.h"
|
2020-06-17 15:53:10 +02:00
|
|
|
#include "absl/flags/commandlineflag.h"
|
2019-05-07 21:56:42 +02:00
|
|
|
#include "absl/flags/flag.h"
|
2020-01-10 17:42:35 +01:00
|
|
|
#include "absl/flags/internal/flag.h"
|
2019-05-07 21:56:42 +02:00
|
|
|
#include "absl/flags/internal/path_util.h"
|
2020-05-07 19:42:26 +02:00
|
|
|
#include "absl/flags/internal/private_handle_accessor.h"
|
2019-05-07 21:56:42 +02:00
|
|
|
#include "absl/flags/internal/program_name.h"
|
2020-01-10 17:42:35 +01:00
|
|
|
#include "absl/flags/internal/registry.h"
|
2019-05-07 21:56:42 +02:00
|
|
|
#include "absl/flags/usage_config.h"
|
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "absl/strings/str_split.h"
|
2019-07-17 22:35:47 +02:00
|
|
|
#include "absl/strings/string_view.h"
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
// Dummy global variables to prevent anyone else defining these.
|
|
|
|
bool FLAGS_help = false;
|
|
|
|
bool FLAGS_helpfull = false;
|
|
|
|
bool FLAGS_helpshort = false;
|
|
|
|
bool FLAGS_helppackage = false;
|
|
|
|
bool FLAGS_version = false;
|
|
|
|
bool FLAGS_only_check_args = false;
|
|
|
|
bool FLAGS_helpon = false;
|
|
|
|
bool FLAGS_helpmatch = false;
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
namespace absl {
|
2019-12-12 19:36:03 +01:00
|
|
|
ABSL_NAMESPACE_BEGIN
|
2019-05-07 21:56:42 +02:00
|
|
|
namespace flags_internal {
|
|
|
|
namespace {
|
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
using PerFlagFilter = std::function<bool(const absl::CommandLineFlag&)>;
|
|
|
|
|
|
|
|
// Maximum length size in a human readable format.
|
|
|
|
constexpr size_t kHrfMaxLineLength = 80;
|
|
|
|
|
2019-05-07 21:56:42 +02:00
|
|
|
// This class is used to emit an XML element with `tag` and `text`.
|
|
|
|
// It adds opening and closing tags and escapes special characters in the text.
|
|
|
|
// For example:
|
|
|
|
// std::cout << XMLElement("title", "Milk & Cookies");
|
|
|
|
// prints "<title>Milk & Cookies</title>"
|
|
|
|
class XMLElement {
|
|
|
|
public:
|
|
|
|
XMLElement(absl::string_view tag, absl::string_view txt)
|
|
|
|
: tag_(tag), txt_(txt) {}
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& out,
|
|
|
|
const XMLElement& xml_elem) {
|
|
|
|
out << "<" << xml_elem.tag_ << ">";
|
|
|
|
|
|
|
|
for (auto c : xml_elem.txt_) {
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
out << """;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
out << "'";
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
out << "&";
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
out << "<";
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
out << ">";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
out << c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out << "</" << xml_elem.tag_ << ">";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
absl::string_view tag_;
|
|
|
|
absl::string_view txt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Helper class to pretty-print info about a flag.
|
|
|
|
|
|
|
|
class FlagHelpPrettyPrinter {
|
|
|
|
public:
|
|
|
|
// Pretty printer holds on to the std::ostream& reference to direct an output
|
|
|
|
// to that stream.
|
2020-11-21 14:43:54 +01:00
|
|
|
FlagHelpPrettyPrinter(size_t max_line_len, size_t min_line_len,
|
|
|
|
size_t wrapped_line_indent, std::ostream& out)
|
2020-06-17 15:53:10 +02:00
|
|
|
: out_(out),
|
2019-05-07 21:56:42 +02:00
|
|
|
max_line_len_(max_line_len),
|
2020-11-21 14:43:54 +01:00
|
|
|
min_line_len_(min_line_len),
|
|
|
|
wrapped_line_indent_(wrapped_line_indent),
|
2019-05-07 21:56:42 +02:00
|
|
|
line_len_(0),
|
|
|
|
first_line_(true) {}
|
|
|
|
|
|
|
|
void Write(absl::string_view str, bool wrap_line = false) {
|
2020-03-10 17:28:06 +01:00
|
|
|
// Empty string - do nothing.
|
2019-05-07 21:56:42 +02:00
|
|
|
if (str.empty()) return;
|
|
|
|
|
|
|
|
std::vector<absl::string_view> tokens;
|
|
|
|
if (wrap_line) {
|
2019-08-07 00:11:42 +02:00
|
|
|
for (auto line : absl::StrSplit(str, absl::ByAnyChar("\n\r"))) {
|
|
|
|
if (!tokens.empty()) {
|
2020-03-10 17:28:06 +01:00
|
|
|
// Keep line separators in the input string.
|
2019-08-07 00:11:42 +02:00
|
|
|
tokens.push_back("\n");
|
|
|
|
}
|
|
|
|
for (auto token :
|
|
|
|
absl::StrSplit(line, absl::ByAnyChar(" \t"), absl::SkipEmpty())) {
|
|
|
|
tokens.push_back(token);
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
} else {
|
|
|
|
tokens.push_back(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto token : tokens) {
|
|
|
|
bool new_line = (line_len_ == 0);
|
|
|
|
|
2020-03-10 17:28:06 +01:00
|
|
|
// Respect line separators in the input string.
|
2019-08-07 00:11:42 +02:00
|
|
|
if (token == "\n") {
|
|
|
|
EndLine();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-10 17:28:06 +01:00
|
|
|
// Write the token, ending the string first if necessary/possible.
|
2020-11-21 14:43:54 +01:00
|
|
|
if (!new_line &&
|
|
|
|
(line_len_ + static_cast<int>(token.size()) >= max_line_len_)) {
|
2019-05-07 21:56:42 +02:00
|
|
|
EndLine();
|
|
|
|
new_line = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_line) {
|
|
|
|
StartLine();
|
|
|
|
} else {
|
|
|
|
out_ << ' ';
|
|
|
|
++line_len_;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_ << token;
|
|
|
|
line_len_ += token.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartLine() {
|
|
|
|
if (first_line_) {
|
2020-11-21 14:43:54 +01:00
|
|
|
line_len_ = min_line_len_;
|
2019-05-07 21:56:42 +02:00
|
|
|
first_line_ = false;
|
|
|
|
} else {
|
2020-11-21 14:43:54 +01:00
|
|
|
line_len_ = min_line_len_ + wrapped_line_indent_;
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
2020-11-21 14:43:54 +01:00
|
|
|
out_ << std::string(line_len_, ' ');
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
void EndLine() {
|
|
|
|
out_ << '\n';
|
|
|
|
line_len_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostream& out_;
|
2020-11-21 14:43:54 +01:00
|
|
|
const size_t max_line_len_;
|
|
|
|
const size_t min_line_len_;
|
|
|
|
const size_t wrapped_line_indent_;
|
|
|
|
size_t line_len_;
|
2019-05-07 21:56:42 +02:00
|
|
|
bool first_line_;
|
|
|
|
};
|
|
|
|
|
2020-06-17 15:53:10 +02:00
|
|
|
void FlagHelpHumanReadable(const CommandLineFlag& flag, std::ostream& out) {
|
2020-11-21 14:43:54 +01:00
|
|
|
FlagHelpPrettyPrinter printer(kHrfMaxLineLength, 4, 2, out);
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
// Flag name.
|
2019-12-10 18:03:32 +01:00
|
|
|
printer.Write(absl::StrCat("--", flag.Name()));
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
// Flag help.
|
|
|
|
printer.Write(absl::StrCat("(", flag.Help(), ");"), /*wrap_line=*/true);
|
|
|
|
|
|
|
|
// The listed default value will be the actual default from the flag
|
|
|
|
// definition in the originating source file, unless the value has
|
|
|
|
// subsequently been modified using SetCommandLineOption() with mode
|
|
|
|
// SET_FLAGS_DEFAULT.
|
|
|
|
std::string dflt_val = flag.DefaultValue();
|
2020-05-01 02:44:10 +02:00
|
|
|
std::string curr_val = flag.CurrentValue();
|
|
|
|
bool is_modified = curr_val != dflt_val;
|
|
|
|
|
2019-05-07 21:56:42 +02:00
|
|
|
if (flag.IsOfType<std::string>()) {
|
|
|
|
dflt_val = absl::StrCat("\"", dflt_val, "\"");
|
|
|
|
}
|
|
|
|
printer.Write(absl::StrCat("default: ", dflt_val, ";"));
|
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
if (is_modified) {
|
2019-05-07 21:56:42 +02:00
|
|
|
if (flag.IsOfType<std::string>()) {
|
|
|
|
curr_val = absl::StrCat("\"", curr_val, "\"");
|
|
|
|
}
|
|
|
|
printer.Write(absl::StrCat("currently: ", curr_val, ";"));
|
|
|
|
}
|
|
|
|
|
|
|
|
printer.EndLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shows help for every filename which matches any of the filters
|
|
|
|
// If filters are empty, shows help for every file.
|
|
|
|
// If a flag's help message has been stripped (e.g. by adding '#define
|
|
|
|
// STRIP_FLAG_HELP 1' then this flag will not be displayed by '--help'
|
|
|
|
// and its variants.
|
2020-11-21 14:43:54 +01:00
|
|
|
void FlagsHelpImpl(std::ostream& out, PerFlagFilter filter_cb,
|
2019-07-17 22:35:47 +02:00
|
|
|
HelpFormat format, absl::string_view program_usage_message) {
|
2019-05-07 21:56:42 +02:00
|
|
|
if (format == HelpFormat::kHumanReadable) {
|
|
|
|
out << flags_internal::ShortProgramInvocationName() << ": "
|
2019-07-17 22:35:47 +02:00
|
|
|
<< program_usage_message << "\n\n";
|
2019-05-07 21:56:42 +02:00
|
|
|
} else {
|
|
|
|
// XML schema is not a part of our public API for now.
|
|
|
|
out << "<?xml version=\"1.0\"?>\n"
|
2019-10-17 00:51:11 +02:00
|
|
|
<< "<!-- This output should be used with care. We do not report type "
|
|
|
|
"names for flags with user defined types -->\n"
|
|
|
|
<< "<!-- Prefer flag only_check_args for validating flag inputs -->\n"
|
2019-05-07 21:56:42 +02:00
|
|
|
// The document.
|
|
|
|
<< "<AllFlags>\n"
|
|
|
|
// The program name and usage.
|
|
|
|
<< XMLElement("program", flags_internal::ShortProgramInvocationName())
|
|
|
|
<< '\n'
|
2019-07-17 22:35:47 +02:00
|
|
|
<< XMLElement("usage", program_usage_message) << '\n';
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Map of package name to
|
|
|
|
// map of file name to
|
|
|
|
// vector of flags in the file.
|
|
|
|
// This map is used to output matching flags grouped by package and file
|
|
|
|
// name.
|
|
|
|
std::map<std::string,
|
2020-06-17 15:53:10 +02:00
|
|
|
std::map<std::string, std::vector<const absl::CommandLineFlag*>>>
|
2019-05-07 21:56:42 +02:00
|
|
|
matching_flags;
|
|
|
|
|
2020-06-17 15:53:10 +02:00
|
|
|
flags_internal::ForEachFlag([&](absl::CommandLineFlag& flag) {
|
2019-05-07 21:56:42 +02:00
|
|
|
// Ignore retired flags.
|
2020-06-17 15:53:10 +02:00
|
|
|
if (flag.IsRetired()) return;
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
// If the flag has been stripped, pretend that it doesn't exist.
|
2020-06-17 15:53:10 +02:00
|
|
|
if (flag.Help() == flags_internal::kStrippedFlagHelp) return;
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
// Make sure flag satisfies the filter
|
2020-11-21 14:43:54 +01:00
|
|
|
if (!filter_cb(flag)) return;
|
|
|
|
|
|
|
|
std::string flag_filename = flag.Filename();
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
matching_flags[std::string(flags_internal::Package(flag_filename))]
|
|
|
|
[flag_filename]
|
2020-06-17 15:53:10 +02:00
|
|
|
.push_back(&flag);
|
2019-05-07 21:56:42 +02:00
|
|
|
});
|
|
|
|
|
2020-06-17 15:53:10 +02:00
|
|
|
absl::string_view package_separator; // controls blank lines between packages
|
|
|
|
absl::string_view file_separator; // controls blank lines between files
|
2019-05-07 21:56:42 +02:00
|
|
|
for (const auto& package : matching_flags) {
|
|
|
|
if (format == HelpFormat::kHumanReadable) {
|
|
|
|
out << package_separator;
|
|
|
|
package_separator = "\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
file_separator = "";
|
|
|
|
for (const auto& flags_in_file : package.second) {
|
|
|
|
if (format == HelpFormat::kHumanReadable) {
|
|
|
|
out << file_separator << " Flags from " << flags_in_file.first
|
|
|
|
<< ":\n";
|
|
|
|
file_separator = "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto* flag : flags_in_file.second) {
|
|
|
|
flags_internal::FlagHelp(out, *flag, format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == HelpFormat::kHumanReadable) {
|
2020-11-21 14:43:54 +01:00
|
|
|
FlagHelpPrettyPrinter printer(kHrfMaxLineLength, 0, 0, out);
|
|
|
|
|
2019-05-07 21:56:42 +02:00
|
|
|
if (filter_cb && matching_flags.empty()) {
|
2020-11-21 14:43:54 +01:00
|
|
|
printer.Write("No flags matched.\n", true);
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
2020-11-21 14:43:54 +01:00
|
|
|
printer.EndLine();
|
|
|
|
printer.Write(
|
|
|
|
"Try --helpfull to get a list of all flags or --help=substring "
|
|
|
|
"shows help for flags which include specified substring in either "
|
|
|
|
"in the name, or description or path.\n",
|
|
|
|
true);
|
2019-05-07 21:56:42 +02:00
|
|
|
} else {
|
|
|
|
// The end of the document.
|
|
|
|
out << "</AllFlags>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
void FlagsHelpImpl(std::ostream& out,
|
|
|
|
flags_internal::FlagKindFilter filename_filter_cb,
|
|
|
|
HelpFormat format, absl::string_view program_usage_message) {
|
|
|
|
FlagsHelpImpl(
|
|
|
|
out,
|
|
|
|
[&](const absl::CommandLineFlag& flag) {
|
|
|
|
return filename_filter_cb && filename_filter_cb(flag.Filename());
|
|
|
|
},
|
|
|
|
format, program_usage_message);
|
|
|
|
}
|
|
|
|
|
2019-05-07 21:56:42 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Produces the help message describing specific flag.
|
2020-06-17 15:53:10 +02:00
|
|
|
void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
|
2019-05-07 21:56:42 +02:00
|
|
|
HelpFormat format) {
|
|
|
|
if (format == HelpFormat::kHumanReadable)
|
2020-06-17 15:53:10 +02:00
|
|
|
flags_internal::FlagHelpHumanReadable(flag, out);
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
2020-11-21 14:43:54 +01:00
|
|
|
// Produces the help messages for all flags matching the filename filter.
|
2019-05-07 21:56:42 +02:00
|
|
|
// If filter is empty produces help messages for all flags.
|
2019-07-17 22:35:47 +02:00
|
|
|
void FlagsHelp(std::ostream& out, absl::string_view filter, HelpFormat format,
|
|
|
|
absl::string_view program_usage_message) {
|
2019-05-07 21:56:42 +02:00
|
|
|
flags_internal::FlagKindFilter filter_cb = [&](absl::string_view filename) {
|
|
|
|
return filter.empty() || filename.find(filter) != absl::string_view::npos;
|
|
|
|
};
|
2019-07-17 22:35:47 +02:00
|
|
|
flags_internal::FlagsHelpImpl(out, filter_cb, format, program_usage_message);
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Checks all the 'usage' command line flags to see if any have been set.
|
|
|
|
// If so, handles them appropriately.
|
2019-07-17 22:35:47 +02:00
|
|
|
int HandleUsageFlags(std::ostream& out,
|
|
|
|
absl::string_view program_usage_message) {
|
2020-11-21 14:43:54 +01:00
|
|
|
switch (GetFlagsHelpMode()) {
|
|
|
|
case HelpMode::kNone:
|
|
|
|
break;
|
|
|
|
case HelpMode::kImportant:
|
|
|
|
flags_internal::FlagsHelpImpl(
|
|
|
|
out, flags_internal::GetUsageConfig().contains_help_flags,
|
|
|
|
GetFlagsHelpFormat(), program_usage_message);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case HelpMode::kShort:
|
|
|
|
flags_internal::FlagsHelpImpl(
|
|
|
|
out, flags_internal::GetUsageConfig().contains_helpshort_flags,
|
|
|
|
GetFlagsHelpFormat(), program_usage_message);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case HelpMode::kFull:
|
|
|
|
flags_internal::FlagsHelp(out, "", GetFlagsHelpFormat(),
|
|
|
|
program_usage_message);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case HelpMode::kPackage:
|
|
|
|
flags_internal::FlagsHelpImpl(
|
|
|
|
out, flags_internal::GetUsageConfig().contains_helppackage_flags,
|
|
|
|
GetFlagsHelpFormat(), program_usage_message);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case HelpMode::kMatch: {
|
|
|
|
std::string substr = GetFlagsHelpMatchSubstr();
|
|
|
|
if (substr.empty()) {
|
|
|
|
// show all options
|
|
|
|
flags_internal::FlagsHelp(out, substr, GetFlagsHelpFormat(),
|
|
|
|
program_usage_message);
|
|
|
|
} else {
|
|
|
|
auto filter_cb = [&substr](const absl::CommandLineFlag& flag) {
|
|
|
|
if (absl::StrContains(flag.Name(), substr)) return true;
|
|
|
|
if (absl::StrContains(flag.Filename(), substr)) return true;
|
|
|
|
if (absl::StrContains(flag.Help(), substr)) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
flags_internal::FlagsHelpImpl(
|
|
|
|
out, filter_cb, HelpFormat::kHumanReadable, program_usage_message);
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case HelpMode::kVersion:
|
|
|
|
if (flags_internal::GetUsageConfig().version_string)
|
|
|
|
out << flags_internal::GetUsageConfig().version_string();
|
|
|
|
// Unlike help, we may be asking for version in a script, so return 0
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case HelpMode::kOnlyCheckArgs:
|
|
|
|
return 0;
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Globals representing usage reporting flags
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
ABSL_CONST_INIT absl::Mutex help_attributes_guard(absl::kConstInit);
|
|
|
|
ABSL_CONST_INIT std::string* match_substr
|
|
|
|
ABSL_GUARDED_BY(help_attributes_guard) = nullptr;
|
|
|
|
ABSL_CONST_INIT HelpMode help_mode ABSL_GUARDED_BY(help_attributes_guard) =
|
|
|
|
HelpMode::kNone;
|
|
|
|
ABSL_CONST_INIT HelpFormat help_format ABSL_GUARDED_BY(help_attributes_guard) =
|
|
|
|
HelpFormat::kHumanReadable;
|
|
|
|
|
|
|
|
} // namespace
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
std::string GetFlagsHelpMatchSubstr() {
|
|
|
|
absl::MutexLock l(&help_attributes_guard);
|
|
|
|
if (match_substr == nullptr) return "";
|
|
|
|
return *match_substr;
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
void SetFlagsHelpMatchSubstr(absl::string_view substr) {
|
|
|
|
absl::MutexLock l(&help_attributes_guard);
|
|
|
|
if (match_substr == nullptr) match_substr = new std::string;
|
|
|
|
match_substr->assign(substr.data(), substr.size());
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
HelpMode GetFlagsHelpMode() {
|
|
|
|
absl::MutexLock l(&help_attributes_guard);
|
|
|
|
// Refer to dummy variales to prevent linker dropping them
|
|
|
|
if (FLAGS_help || FLAGS_helpfull || FLAGS_helpshort || FLAGS_helppackage ||
|
|
|
|
FLAGS_version || FLAGS_only_check_args || FLAGS_helpon ||
|
|
|
|
FLAGS_helpmatch) {
|
|
|
|
help_mode = HelpMode::kNone;
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
2020-11-21 14:43:54 +01:00
|
|
|
return help_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetFlagsHelpMode(HelpMode mode) {
|
|
|
|
absl::MutexLock l(&help_attributes_guard);
|
|
|
|
help_mode = mode;
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
HelpFormat GetFlagsHelpFormat() {
|
|
|
|
absl::MutexLock l(&help_attributes_guard);
|
|
|
|
return help_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetFlagsHelpFormat(HelpFormat format) {
|
|
|
|
absl::MutexLock l(&help_attributes_guard);
|
|
|
|
help_format = format;
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
// Deduces usage flags from the input argument in a form --name=value or
|
|
|
|
// --name. argument is already split into name and value before we call this
|
|
|
|
// function.
|
|
|
|
bool DeduceUsageFlags(absl::string_view name, absl::string_view value) {
|
|
|
|
if (absl::ConsumePrefix(&name, "help")) {
|
|
|
|
if (name == "") {
|
|
|
|
if (value.empty()) {
|
|
|
|
SetFlagsHelpMode(HelpMode::kImportant);
|
|
|
|
} else {
|
|
|
|
SetFlagsHelpMode(HelpMode::kMatch);
|
|
|
|
SetFlagsHelpMatchSubstr(value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-07 21:56:42 +02:00
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
if (name == "match") {
|
|
|
|
SetFlagsHelpMode(HelpMode::kMatch);
|
|
|
|
SetFlagsHelpMatchSubstr(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "on") {
|
|
|
|
SetFlagsHelpMode(HelpMode::kMatch);
|
|
|
|
SetFlagsHelpMatchSubstr(absl::StrCat("/", value, "."));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "full") {
|
|
|
|
SetFlagsHelpMode(HelpMode::kFull);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "short") {
|
|
|
|
SetFlagsHelpMode(HelpMode::kShort);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "package") {
|
|
|
|
SetFlagsHelpMode(HelpMode::kPackage);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
if (name == "version") {
|
|
|
|
SetFlagsHelpMode(HelpMode::kVersion);
|
|
|
|
return true;
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
if (name == "only_check_args") {
|
|
|
|
SetFlagsHelpMode(HelpMode::kOnlyCheckArgs);
|
|
|
|
return true;
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 14:43:54 +01:00
|
|
|
return false;
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace flags_internal
|
2019-12-12 19:36:03 +01:00
|
|
|
ABSL_NAMESPACE_END
|
2019-05-07 21:56:42 +02:00
|
|
|
} // namespace absl
|