usage.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <string>
  19. #include "absl/base/config.h"
  20. #include "absl/flags/commandlineflag.h"
  21. #include "absl/flags/declare.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. // Streams the help message describing `flag` to `out`.
  33. // The default value for `flag` is included in the output.
  34. void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
  35. HelpFormat format = HelpFormat::kHumanReadable);
  36. // Produces the help messages for all flags matching the filter. A flag matches
  37. // the filter if it is defined in a file with a filename which includes
  38. // filter string as a substring. You can use '/' and '.' to restrict the
  39. // matching to a specific file names. For example:
  40. // FlagsHelp(out, "/path/to/file.");
  41. // restricts help to only flags which resides in files named like:
  42. // .../path/to/file.<ext>
  43. // for any extension 'ext'. If the filter is empty this function produces help
  44. // messages for all flags.
  45. void FlagsHelp(std::ostream& out, absl::string_view filter,
  46. HelpFormat format, absl::string_view program_usage_message);
  47. // --------------------------------------------------------------------
  48. // If any of the 'usage' related command line flags (listed on the bottom of
  49. // this file) has been set this routine produces corresponding help message in
  50. // the specified output stream and returns:
  51. // 0 - if "version" or "only_check_flags" flags were set and handled.
  52. // 1 - if some other 'usage' related flag was set and handled.
  53. // -1 - if no usage flags were set on a commmand line.
  54. // Non negative return values are expected to be used as an exit code for a
  55. // binary.
  56. int HandleUsageFlags(std::ostream& out,
  57. absl::string_view program_usage_message);
  58. // --------------------------------------------------------------------
  59. // Globals representing usage reporting flags
  60. enum class HelpMode {
  61. kNone,
  62. kImportant,
  63. kShort,
  64. kFull,
  65. kPackage,
  66. kMatch,
  67. kVersion,
  68. kOnlyCheckArgs
  69. };
  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_