usage.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef ABSL_FLAGS_INTERNAL_USAGE_H_
  16. #define ABSL_FLAGS_INTERNAL_USAGE_H_
  17. #include <iosfwd>
  18. #include <ostream>
  19. #include <string>
  20. #include "absl/base/config.h"
  21. #include "absl/flags/commandlineflag.h"
  22. #include "absl/strings/string_view.h"
  23. // --------------------------------------------------------------------
  24. // Usage reporting interfaces
  25. namespace absl {
  26. ABSL_NAMESPACE_BEGIN
  27. namespace flags_internal {
  28. // The format to report the help messages in.
  29. enum class HelpFormat {
  30. kHumanReadable,
  31. };
  32. // The kind of usage help requested.
  33. enum class HelpMode {
  34. kNone,
  35. kImportant,
  36. kShort,
  37. kFull,
  38. kPackage,
  39. kMatch,
  40. kVersion,
  41. kOnlyCheckArgs
  42. };
  43. // Streams the help message describing `flag` to `out`.
  44. // The default value for `flag` is included in the output.
  45. void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
  46. HelpFormat format = HelpFormat::kHumanReadable);
  47. // Produces the help messages for all flags matching the filter. A flag matches
  48. // the filter if it is defined in a file with a filename which includes
  49. // filter string as a substring. You can use '/' and '.' to restrict the
  50. // matching to a specific file names. For example:
  51. // FlagsHelp(out, "/path/to/file.");
  52. // restricts help to only flags which resides in files named like:
  53. // .../path/to/file.<ext>
  54. // for any extension 'ext'. If the filter is empty this function produces help
  55. // messages for all flags.
  56. void FlagsHelp(std::ostream& out, absl::string_view filter,
  57. HelpFormat format, absl::string_view program_usage_message);
  58. // --------------------------------------------------------------------
  59. // If any of the 'usage' related command line flags (listed on the bottom of
  60. // this file) has been set this routine produces corresponding help message in
  61. // the specified output stream and returns HelpMode that was handled. Otherwise
  62. // it returns HelpMode::kNone.
  63. HelpMode HandleUsageFlags(std::ostream& out,
  64. absl::string_view program_usage_message);
  65. // --------------------------------------------------------------------
  66. // Encapsulates the logic of exiting the binary depending on handled help mode.
  67. void MaybeExit(HelpMode mode);
  68. // --------------------------------------------------------------------
  69. // Globals representing usage reporting flags
  70. // Returns substring to filter help output (--help=substr argument)
  71. std::string GetFlagsHelpMatchSubstr();
  72. // Returns the requested help mode.
  73. HelpMode GetFlagsHelpMode();
  74. // Returns the requested help format.
  75. HelpFormat GetFlagsHelpFormat();
  76. // These are corresponding setters to the attributes above.
  77. void SetFlagsHelpMatchSubstr(absl::string_view);
  78. void SetFlagsHelpMode(HelpMode);
  79. void SetFlagsHelpFormat(HelpFormat);
  80. // Deduces usage flags from the input argument in a form --name=value or
  81. // --name. argument is already split into name and value before we call this
  82. // function.
  83. bool DeduceUsageFlags(absl::string_view name, absl::string_view value);
  84. } // namespace flags_internal
  85. ABSL_NAMESPACE_END
  86. } // namespace absl
  87. #endif // ABSL_FLAGS_INTERNAL_USAGE_H_