usage.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. //
  2. // Copyright 2019 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "y_absl/flags/internal/usage.h"
  16. #include <stdint.h>
  17. #include <algorithm>
  18. #include <cstdlib>
  19. #include <functional>
  20. #include <iterator>
  21. #include <map>
  22. #include <ostream>
  23. #include <util/generic/string.h>
  24. #include <utility>
  25. #include <vector>
  26. #include "y_absl/base/config.h"
  27. #include "y_absl/flags/commandlineflag.h"
  28. #include "y_absl/flags/flag.h"
  29. #include "y_absl/flags/internal/flag.h"
  30. #include "y_absl/flags/internal/path_util.h"
  31. #include "y_absl/flags/internal/private_handle_accessor.h"
  32. #include "y_absl/flags/internal/program_name.h"
  33. #include "y_absl/flags/internal/registry.h"
  34. #include "y_absl/flags/usage_config.h"
  35. #include "y_absl/strings/match.h"
  36. #include "y_absl/strings/str_cat.h"
  37. #include "y_absl/strings/str_split.h"
  38. #include "y_absl/strings/string_view.h"
  39. // Dummy global variables to prevent anyone else defining these.
  40. bool FLAGS_help = false;
  41. bool FLAGS_helpfull = false;
  42. bool FLAGS_helpshort = false;
  43. bool FLAGS_helppackage = false;
  44. bool FLAGS_version = false;
  45. bool FLAGS_only_check_args = false;
  46. bool FLAGS_helpon = false;
  47. bool FLAGS_helpmatch = false;
  48. namespace y_absl {
  49. Y_ABSL_NAMESPACE_BEGIN
  50. namespace flags_internal {
  51. namespace {
  52. using PerFlagFilter = std::function<bool(const y_absl::CommandLineFlag&)>;
  53. // Maximum length size in a human readable format.
  54. constexpr size_t kHrfMaxLineLength = 80;
  55. // This class is used to emit an XML element with `tag` and `text`.
  56. // It adds opening and closing tags and escapes special characters in the text.
  57. // For example:
  58. // std::cout << XMLElement("title", "Milk & Cookies");
  59. // prints "<title>Milk &amp; Cookies</title>"
  60. class XMLElement {
  61. public:
  62. XMLElement(y_absl::string_view tag, y_absl::string_view txt)
  63. : tag_(tag), txt_(txt) {}
  64. friend std::ostream& operator<<(std::ostream& out,
  65. const XMLElement& xml_elem) {
  66. out << "<" << xml_elem.tag_ << ">";
  67. for (auto c : xml_elem.txt_) {
  68. switch (c) {
  69. case '"':
  70. out << "&quot;";
  71. break;
  72. case '\'':
  73. out << "&apos;";
  74. break;
  75. case '&':
  76. out << "&amp;";
  77. break;
  78. case '<':
  79. out << "&lt;";
  80. break;
  81. case '>':
  82. out << "&gt;";
  83. break;
  84. case '\n':
  85. case '\v':
  86. case '\f':
  87. case '\t':
  88. out << " ";
  89. break;
  90. default:
  91. if (IsValidXmlCharacter(static_cast<unsigned char>(c))) {
  92. out << c;
  93. }
  94. break;
  95. }
  96. }
  97. return out << "</" << xml_elem.tag_ << ">";
  98. }
  99. private:
  100. static bool IsValidXmlCharacter(unsigned char c) { return c >= 0x20; }
  101. y_absl::string_view tag_;
  102. y_absl::string_view txt_;
  103. };
  104. // --------------------------------------------------------------------
  105. // Helper class to pretty-print info about a flag.
  106. class FlagHelpPrettyPrinter {
  107. public:
  108. // Pretty printer holds on to the std::ostream& reference to direct an output
  109. // to that stream.
  110. FlagHelpPrettyPrinter(size_t max_line_len, size_t min_line_len,
  111. size_t wrapped_line_indent, std::ostream& out)
  112. : out_(out),
  113. max_line_len_(max_line_len),
  114. min_line_len_(min_line_len),
  115. wrapped_line_indent_(wrapped_line_indent),
  116. line_len_(0),
  117. first_line_(true) {}
  118. void Write(y_absl::string_view str, bool wrap_line = false) {
  119. // Empty string - do nothing.
  120. if (str.empty()) return;
  121. std::vector<y_absl::string_view> tokens;
  122. if (wrap_line) {
  123. for (auto line : y_absl::StrSplit(str, y_absl::ByAnyChar("\n\r"))) {
  124. if (!tokens.empty()) {
  125. // Keep line separators in the input string.
  126. tokens.emplace_back("\n");
  127. }
  128. for (auto token :
  129. y_absl::StrSplit(line, y_absl::ByAnyChar(" \t"), y_absl::SkipEmpty())) {
  130. tokens.push_back(token);
  131. }
  132. }
  133. } else {
  134. tokens.push_back(str);
  135. }
  136. for (auto token : tokens) {
  137. bool new_line = (line_len_ == 0);
  138. // Respect line separators in the input string.
  139. if (token == "\n") {
  140. EndLine();
  141. continue;
  142. }
  143. // Write the token, ending the string first if necessary/possible.
  144. if (!new_line && (line_len_ + token.size() >= max_line_len_)) {
  145. EndLine();
  146. new_line = true;
  147. }
  148. if (new_line) {
  149. StartLine();
  150. } else {
  151. out_ << ' ';
  152. ++line_len_;
  153. }
  154. out_ << token;
  155. line_len_ += token.size();
  156. }
  157. }
  158. void StartLine() {
  159. if (first_line_) {
  160. line_len_ = min_line_len_;
  161. first_line_ = false;
  162. } else {
  163. line_len_ = min_line_len_ + wrapped_line_indent_;
  164. }
  165. out_ << TString(line_len_, ' ');
  166. }
  167. void EndLine() {
  168. out_ << '\n';
  169. line_len_ = 0;
  170. }
  171. private:
  172. std::ostream& out_;
  173. const size_t max_line_len_;
  174. const size_t min_line_len_;
  175. const size_t wrapped_line_indent_;
  176. size_t line_len_;
  177. bool first_line_;
  178. };
  179. void FlagHelpHumanReadable(const CommandLineFlag& flag, std::ostream& out) {
  180. FlagHelpPrettyPrinter printer(kHrfMaxLineLength, 4, 2, out);
  181. // Flag name.
  182. printer.Write(y_absl::StrCat("--", flag.Name()));
  183. // Flag help.
  184. printer.Write(y_absl::StrCat("(", flag.Help(), ");"), /*wrap_line=*/true);
  185. // The listed default value will be the actual default from the flag
  186. // definition in the originating source file, unless the value has
  187. // subsequently been modified using SetCommandLineOption() with mode
  188. // SET_FLAGS_DEFAULT.
  189. TString dflt_val = flag.DefaultValue();
  190. TString curr_val = flag.CurrentValue();
  191. bool is_modified = curr_val != dflt_val;
  192. if (flag.IsOfType<TString>()) {
  193. dflt_val = y_absl::StrCat("\"", dflt_val, "\"");
  194. }
  195. printer.Write(y_absl::StrCat("default: ", dflt_val, ";"));
  196. if (is_modified) {
  197. if (flag.IsOfType<TString>()) {
  198. curr_val = y_absl::StrCat("\"", curr_val, "\"");
  199. }
  200. printer.Write(y_absl::StrCat("currently: ", curr_val, ";"));
  201. }
  202. printer.EndLine();
  203. }
  204. // Shows help for every filename which matches any of the filters
  205. // If filters are empty, shows help for every file.
  206. // If a flag's help message has been stripped (e.g. by adding '#define
  207. // STRIP_FLAG_HELP 1' then this flag will not be displayed by '--help'
  208. // and its variants.
  209. void FlagsHelpImpl(std::ostream& out, PerFlagFilter filter_cb,
  210. HelpFormat format, y_absl::string_view program_usage_message) {
  211. if (format == HelpFormat::kHumanReadable) {
  212. out << flags_internal::ShortProgramInvocationName() << ": "
  213. << program_usage_message << "\n\n";
  214. } else {
  215. // XML schema is not a part of our public API for now.
  216. out << "<?xml version=\"1.0\"?>\n"
  217. << "<!-- This output should be used with care. We do not report type "
  218. "names for flags with user defined types -->\n"
  219. << "<!-- Prefer flag only_check_args for validating flag inputs -->\n"
  220. // The document.
  221. << "<AllFlags>\n"
  222. // The program name and usage.
  223. << XMLElement("program", flags_internal::ShortProgramInvocationName())
  224. << '\n'
  225. << XMLElement("usage", program_usage_message) << '\n';
  226. }
  227. // Ordered map of package name to
  228. // map of file name to
  229. // vector of flags in the file.
  230. // This map is used to output matching flags grouped by package and file
  231. // name.
  232. std::map<TString,
  233. std::map<TString, std::vector<const y_absl::CommandLineFlag*>>>
  234. matching_flags;
  235. flags_internal::ForEachFlag([&](y_absl::CommandLineFlag& flag) {
  236. // Ignore retired flags.
  237. if (flag.IsRetired()) return;
  238. // If the flag has been stripped, pretend that it doesn't exist.
  239. if (flag.Help() == flags_internal::kStrippedFlagHelp) return;
  240. // Make sure flag satisfies the filter
  241. if (!filter_cb(flag)) return;
  242. TString flag_filename = flag.Filename();
  243. matching_flags[TString(flags_internal::Package(flag_filename))]
  244. [flag_filename]
  245. .push_back(&flag);
  246. });
  247. y_absl::string_view package_separator; // controls blank lines between packages
  248. y_absl::string_view file_separator; // controls blank lines between files
  249. for (auto& package : matching_flags) {
  250. if (format == HelpFormat::kHumanReadable) {
  251. out << package_separator;
  252. package_separator = "\n\n";
  253. }
  254. file_separator = "";
  255. for (auto& flags_in_file : package.second) {
  256. if (format == HelpFormat::kHumanReadable) {
  257. out << file_separator << " Flags from " << flags_in_file.first
  258. << ":\n";
  259. file_separator = "\n";
  260. }
  261. std::sort(std::begin(flags_in_file.second),
  262. std::end(flags_in_file.second),
  263. [](const CommandLineFlag* lhs, const CommandLineFlag* rhs) {
  264. return lhs->Name() < rhs->Name();
  265. });
  266. for (const auto* flag : flags_in_file.second) {
  267. flags_internal::FlagHelp(out, *flag, format);
  268. }
  269. }
  270. }
  271. if (format == HelpFormat::kHumanReadable) {
  272. FlagHelpPrettyPrinter printer(kHrfMaxLineLength, 0, 0, out);
  273. if (filter_cb && matching_flags.empty()) {
  274. printer.Write("No flags matched.\n", true);
  275. }
  276. printer.EndLine();
  277. printer.Write(
  278. "Try --helpfull to get a list of all flags or --help=substring "
  279. "shows help for flags which include specified substring in either "
  280. "in the name, or description or path.\n",
  281. true);
  282. } else {
  283. // The end of the document.
  284. out << "</AllFlags>\n";
  285. }
  286. }
  287. void FlagsHelpImpl(std::ostream& out,
  288. flags_internal::FlagKindFilter filename_filter_cb,
  289. HelpFormat format, y_absl::string_view program_usage_message) {
  290. FlagsHelpImpl(
  291. out,
  292. [&](const y_absl::CommandLineFlag& flag) {
  293. return filename_filter_cb && filename_filter_cb(flag.Filename());
  294. },
  295. format, program_usage_message);
  296. }
  297. } // namespace
  298. // --------------------------------------------------------------------
  299. // Produces the help message describing specific flag.
  300. void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
  301. HelpFormat format) {
  302. if (format == HelpFormat::kHumanReadable)
  303. flags_internal::FlagHelpHumanReadable(flag, out);
  304. }
  305. // --------------------------------------------------------------------
  306. // Produces the help messages for all flags matching the filename filter.
  307. // If filter is empty produces help messages for all flags.
  308. void FlagsHelp(std::ostream& out, y_absl::string_view filter, HelpFormat format,
  309. y_absl::string_view program_usage_message) {
  310. flags_internal::FlagKindFilter filter_cb = [&](y_absl::string_view filename) {
  311. return filter.empty() || y_absl::StrContains(filename, filter);
  312. };
  313. flags_internal::FlagsHelpImpl(out, filter_cb, format, program_usage_message);
  314. }
  315. // --------------------------------------------------------------------
  316. // Checks all the 'usage' command line flags to see if any have been set.
  317. // If so, handles them appropriately.
  318. HelpMode HandleUsageFlags(std::ostream& out,
  319. y_absl::string_view program_usage_message) {
  320. switch (GetFlagsHelpMode()) {
  321. case HelpMode::kNone:
  322. break;
  323. case HelpMode::kImportant:
  324. flags_internal::FlagsHelpImpl(
  325. out, flags_internal::GetUsageConfig().contains_help_flags,
  326. GetFlagsHelpFormat(), program_usage_message);
  327. break;
  328. case HelpMode::kShort:
  329. flags_internal::FlagsHelpImpl(
  330. out, flags_internal::GetUsageConfig().contains_helpshort_flags,
  331. GetFlagsHelpFormat(), program_usage_message);
  332. break;
  333. case HelpMode::kFull:
  334. flags_internal::FlagsHelp(out, "", GetFlagsHelpFormat(),
  335. program_usage_message);
  336. break;
  337. case HelpMode::kPackage:
  338. flags_internal::FlagsHelpImpl(
  339. out, flags_internal::GetUsageConfig().contains_helppackage_flags,
  340. GetFlagsHelpFormat(), program_usage_message);
  341. break;
  342. case HelpMode::kMatch: {
  343. TString substr = GetFlagsHelpMatchSubstr();
  344. if (substr.empty()) {
  345. // show all options
  346. flags_internal::FlagsHelp(out, substr, GetFlagsHelpFormat(),
  347. program_usage_message);
  348. } else {
  349. auto filter_cb = [&substr](const y_absl::CommandLineFlag& flag) {
  350. if (y_absl::StrContains(flag.Name(), substr)) return true;
  351. if (y_absl::StrContains(flag.Filename(), substr)) return true;
  352. if (y_absl::StrContains(flag.Help(), substr)) return true;
  353. return false;
  354. };
  355. flags_internal::FlagsHelpImpl(
  356. out, filter_cb, HelpFormat::kHumanReadable, program_usage_message);
  357. }
  358. break;
  359. }
  360. case HelpMode::kVersion:
  361. if (flags_internal::GetUsageConfig().version_string)
  362. out << flags_internal::GetUsageConfig().version_string();
  363. // Unlike help, we may be asking for version in a script, so return 0
  364. break;
  365. case HelpMode::kOnlyCheckArgs:
  366. break;
  367. }
  368. return GetFlagsHelpMode();
  369. }
  370. // --------------------------------------------------------------------
  371. // Globals representing usage reporting flags
  372. namespace {
  373. Y_ABSL_CONST_INIT y_absl::Mutex help_attributes_guard(y_absl::kConstInit);
  374. Y_ABSL_CONST_INIT TString* match_substr
  375. Y_ABSL_GUARDED_BY(help_attributes_guard) = nullptr;
  376. Y_ABSL_CONST_INIT HelpMode help_mode Y_ABSL_GUARDED_BY(help_attributes_guard) =
  377. HelpMode::kNone;
  378. Y_ABSL_CONST_INIT HelpFormat help_format Y_ABSL_GUARDED_BY(help_attributes_guard) =
  379. HelpFormat::kHumanReadable;
  380. } // namespace
  381. TString GetFlagsHelpMatchSubstr() {
  382. y_absl::MutexLock l(&help_attributes_guard);
  383. if (match_substr == nullptr) return "";
  384. return *match_substr;
  385. }
  386. void SetFlagsHelpMatchSubstr(y_absl::string_view substr) {
  387. y_absl::MutexLock l(&help_attributes_guard);
  388. if (match_substr == nullptr) match_substr = new TString;
  389. match_substr->assign(substr.data(), substr.size());
  390. }
  391. HelpMode GetFlagsHelpMode() {
  392. y_absl::MutexLock l(&help_attributes_guard);
  393. return help_mode;
  394. }
  395. void SetFlagsHelpMode(HelpMode mode) {
  396. y_absl::MutexLock l(&help_attributes_guard);
  397. help_mode = mode;
  398. }
  399. HelpFormat GetFlagsHelpFormat() {
  400. y_absl::MutexLock l(&help_attributes_guard);
  401. return help_format;
  402. }
  403. void SetFlagsHelpFormat(HelpFormat format) {
  404. y_absl::MutexLock l(&help_attributes_guard);
  405. help_format = format;
  406. }
  407. // Deduces usage flags from the input argument in a form --name=value or
  408. // --name. argument is already split into name and value before we call this
  409. // function.
  410. bool DeduceUsageFlags(y_absl::string_view name, y_absl::string_view value) {
  411. if (y_absl::ConsumePrefix(&name, "help")) {
  412. if (name.empty()) {
  413. if (value.empty()) {
  414. SetFlagsHelpMode(HelpMode::kImportant);
  415. } else {
  416. SetFlagsHelpMode(HelpMode::kMatch);
  417. SetFlagsHelpMatchSubstr(value);
  418. }
  419. return true;
  420. }
  421. if (name == "match") {
  422. SetFlagsHelpMode(HelpMode::kMatch);
  423. SetFlagsHelpMatchSubstr(value);
  424. return true;
  425. }
  426. if (name == "on") {
  427. SetFlagsHelpMode(HelpMode::kMatch);
  428. SetFlagsHelpMatchSubstr(y_absl::StrCat("/", value, "."));
  429. return true;
  430. }
  431. if (name == "full") {
  432. SetFlagsHelpMode(HelpMode::kFull);
  433. return true;
  434. }
  435. if (name == "short") {
  436. SetFlagsHelpMode(HelpMode::kShort);
  437. return true;
  438. }
  439. if (name == "package") {
  440. SetFlagsHelpMode(HelpMode::kPackage);
  441. return true;
  442. }
  443. return false;
  444. }
  445. if (name == "version") {
  446. SetFlagsHelpMode(HelpMode::kVersion);
  447. return true;
  448. }
  449. if (name == "only_check_args") {
  450. SetFlagsHelpMode(HelpMode::kOnlyCheckArgs);
  451. return true;
  452. }
  453. return false;
  454. }
  455. // --------------------------------------------------------------------
  456. void MaybeExit(HelpMode mode) {
  457. switch (mode) {
  458. case flags_internal::HelpMode::kNone:
  459. return;
  460. case flags_internal::HelpMode::kOnlyCheckArgs:
  461. case flags_internal::HelpMode::kVersion:
  462. std::exit(0);
  463. default: // For all the other modes we exit with 1
  464. std::exit(1);
  465. }
  466. }
  467. // --------------------------------------------------------------------
  468. } // namespace flags_internal
  469. Y_ABSL_NAMESPACE_END
  470. } // namespace y_absl