You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

usage_config.h 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. //
  16. // -----------------------------------------------------------------------------
  17. // File: usage_config.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This file defines the main usage reporting configuration interfaces and
  21. // documents Abseil's supported built-in usage flags. If these flags are found
  22. // when parsing a command-line, Abseil will exit the program and display
  23. // appropriate help messages.
  24. #ifndef ABSL_FLAGS_USAGE_CONFIG_H_
  25. #define ABSL_FLAGS_USAGE_CONFIG_H_
  26. #include <functional>
  27. #include <string>
  28. #include "absl/base/config.h"
  29. #include "absl/strings/string_view.h"
  30. // -----------------------------------------------------------------------------
  31. // Built-in Usage Flags
  32. // -----------------------------------------------------------------------------
  33. //
  34. // Abseil supports the following built-in usage flags. When passed, these flags
  35. // exit the program and :
  36. //
  37. // * --help
  38. // Shows help on important flags for this binary
  39. // * --helpfull
  40. // Shows help on all flags
  41. // * --helpshort
  42. // Shows help on only the main module for this program
  43. // * --helppackage
  44. // Shows help on all modules in the main package
  45. // * --version
  46. // Shows the version and build info for this binary and exits
  47. // * --only_check_args
  48. // Exits after checking all flags
  49. // * --helpon
  50. // Shows help on the modules named by this flag value
  51. // * --helpmatch
  52. // Shows help on modules whose name contains the specified substring
  53. namespace absl
  54. {
  55. ABSL_NAMESPACE_BEGIN
  56. namespace flags_internal
  57. {
  58. using FlagKindFilter = std::function<bool(absl::string_view)>;
  59. } // namespace flags_internal
  60. // FlagsUsageConfig
  61. //
  62. // This structure contains the collection of callbacks for changing the behavior
  63. // of the usage reporting routines in Abseil Flags.
  64. struct FlagsUsageConfig
  65. {
  66. // Returns true if flags defined in the given source code file should be
  67. // reported with --helpshort flag. For example, if the file
  68. // "path/to/my/code.cc" defines the flag "--my_flag", and
  69. // contains_helpshort_flags("path/to/my/code.cc") returns true, invoking the
  70. // program with --helpshort will include information about --my_flag in the
  71. // program output.
  72. flags_internal::FlagKindFilter contains_helpshort_flags;
  73. // Returns true if flags defined in the filename should be reported with
  74. // --help flag. For example, if the file
  75. // "path/to/my/code.cc" defines the flag "--my_flag", and
  76. // contains_help_flags("path/to/my/code.cc") returns true, invoking the
  77. // program with --help will include information about --my_flag in the
  78. // program output.
  79. flags_internal::FlagKindFilter contains_help_flags;
  80. // Returns true if flags defined in the filename should be reported with
  81. // --helppackage flag. For example, if the file
  82. // "path/to/my/code.cc" defines the flag "--my_flag", and
  83. // contains_helppackage_flags("path/to/my/code.cc") returns true, invoking the
  84. // program with --helppackage will include information about --my_flag in the
  85. // program output.
  86. flags_internal::FlagKindFilter contains_helppackage_flags;
  87. // Generates string containing program version. This is the string reported
  88. // when user specifies --version in a command line.
  89. std::function<std::string()> version_string;
  90. // Normalizes the filename specific to the build system/filesystem used. This
  91. // routine is used when we report the information about the flag definition
  92. // location. For instance, if your build resides at some location you do not
  93. // want to expose in the usage output, you can trim it to show only relevant
  94. // part.
  95. // For example:
  96. // normalize_filename("/my_company/some_long_path/src/project/file.cc")
  97. // might produce
  98. // "project/file.cc".
  99. std::function<std::string(absl::string_view)> normalize_filename;
  100. };
  101. // SetFlagsUsageConfig()
  102. //
  103. // Sets the usage reporting configuration callbacks. If any of the callbacks are
  104. // not set in usage_config instance, then the default value of the callback is
  105. // used.
  106. void SetFlagsUsageConfig(FlagsUsageConfig usage_config);
  107. namespace flags_internal
  108. {
  109. FlagsUsageConfig GetUsageConfig();
  110. void ReportUsageError(absl::string_view msg, bool is_fatal);
  111. } // namespace flags_internal
  112. ABSL_NAMESPACE_END
  113. } // namespace absl
  114. extern "C"
  115. {
  116. // Additional report of fatal usage error message before we std::exit. Error is
  117. // fatal if is_fatal argument to ReportUsageError is true.
  118. void ABSL_INTERNAL_C_SYMBOL(AbslInternalReportFatalUsageError)(
  119. absl::string_view
  120. );
  121. } // extern "C"
  122. #endif // ABSL_FLAGS_USAGE_CONFIG_H_