xref: /aosp_15_r20/external/abseil-cpp/absl/flags/internal/usage.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker //
2*9356374aSAndroid Build Coastguard Worker //  Copyright 2019 The Abseil Authors.
3*9356374aSAndroid Build Coastguard Worker //
4*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*9356374aSAndroid Build Coastguard Worker //
8*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
9*9356374aSAndroid Build Coastguard Worker //
10*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*9356374aSAndroid Build Coastguard Worker // limitations under the License.
15*9356374aSAndroid Build Coastguard Worker 
16*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_FLAGS_INTERNAL_USAGE_H_
17*9356374aSAndroid Build Coastguard Worker #define ABSL_FLAGS_INTERNAL_USAGE_H_
18*9356374aSAndroid Build Coastguard Worker 
19*9356374aSAndroid Build Coastguard Worker #include <iosfwd>
20*9356374aSAndroid Build Coastguard Worker #include <ostream>
21*9356374aSAndroid Build Coastguard Worker #include <string>
22*9356374aSAndroid Build Coastguard Worker 
23*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
24*9356374aSAndroid Build Coastguard Worker #include "absl/flags/commandlineflag.h"
25*9356374aSAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker // --------------------------------------------------------------------
28*9356374aSAndroid Build Coastguard Worker // Usage reporting interfaces
29*9356374aSAndroid Build Coastguard Worker 
30*9356374aSAndroid Build Coastguard Worker namespace absl {
31*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
32*9356374aSAndroid Build Coastguard Worker namespace flags_internal {
33*9356374aSAndroid Build Coastguard Worker 
34*9356374aSAndroid Build Coastguard Worker // The format to report the help messages in.
35*9356374aSAndroid Build Coastguard Worker enum class HelpFormat {
36*9356374aSAndroid Build Coastguard Worker   kHumanReadable,
37*9356374aSAndroid Build Coastguard Worker };
38*9356374aSAndroid Build Coastguard Worker 
39*9356374aSAndroid Build Coastguard Worker // The kind of usage help requested.
40*9356374aSAndroid Build Coastguard Worker enum class HelpMode {
41*9356374aSAndroid Build Coastguard Worker   kNone,
42*9356374aSAndroid Build Coastguard Worker   kImportant,
43*9356374aSAndroid Build Coastguard Worker   kShort,
44*9356374aSAndroid Build Coastguard Worker   kFull,
45*9356374aSAndroid Build Coastguard Worker   kPackage,
46*9356374aSAndroid Build Coastguard Worker   kMatch,
47*9356374aSAndroid Build Coastguard Worker   kVersion,
48*9356374aSAndroid Build Coastguard Worker   kOnlyCheckArgs
49*9356374aSAndroid Build Coastguard Worker };
50*9356374aSAndroid Build Coastguard Worker 
51*9356374aSAndroid Build Coastguard Worker // Streams the help message describing `flag` to `out`.
52*9356374aSAndroid Build Coastguard Worker // The default value for `flag` is included in the output.
53*9356374aSAndroid Build Coastguard Worker void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
54*9356374aSAndroid Build Coastguard Worker               HelpFormat format = HelpFormat::kHumanReadable);
55*9356374aSAndroid Build Coastguard Worker 
56*9356374aSAndroid Build Coastguard Worker // Produces the help messages for all flags matching the filter. A flag matches
57*9356374aSAndroid Build Coastguard Worker // the filter if it is defined in a file with a filename which includes
58*9356374aSAndroid Build Coastguard Worker // filter string as a substring. You can use '/' and '.' to restrict the
59*9356374aSAndroid Build Coastguard Worker // matching to a specific file names. For example:
60*9356374aSAndroid Build Coastguard Worker //   FlagsHelp(out, "/path/to/file.");
61*9356374aSAndroid Build Coastguard Worker // restricts help to only flags which resides in files named like:
62*9356374aSAndroid Build Coastguard Worker //  .../path/to/file.<ext>
63*9356374aSAndroid Build Coastguard Worker // for any extension 'ext'. If the filter is empty this function produces help
64*9356374aSAndroid Build Coastguard Worker // messages for all flags.
65*9356374aSAndroid Build Coastguard Worker void FlagsHelp(std::ostream& out, absl::string_view filter,
66*9356374aSAndroid Build Coastguard Worker                HelpFormat format, absl::string_view program_usage_message);
67*9356374aSAndroid Build Coastguard Worker 
68*9356374aSAndroid Build Coastguard Worker // --------------------------------------------------------------------
69*9356374aSAndroid Build Coastguard Worker 
70*9356374aSAndroid Build Coastguard Worker // If any of the 'usage' related command line flags (listed on the bottom of
71*9356374aSAndroid Build Coastguard Worker // this file) has been set this routine produces corresponding help message in
72*9356374aSAndroid Build Coastguard Worker // the specified output stream and returns HelpMode that was handled. Otherwise
73*9356374aSAndroid Build Coastguard Worker // it returns HelpMode::kNone.
74*9356374aSAndroid Build Coastguard Worker HelpMode HandleUsageFlags(std::ostream& out,
75*9356374aSAndroid Build Coastguard Worker                           absl::string_view program_usage_message);
76*9356374aSAndroid Build Coastguard Worker 
77*9356374aSAndroid Build Coastguard Worker // --------------------------------------------------------------------
78*9356374aSAndroid Build Coastguard Worker // Encapsulates the logic of exiting the binary depending on handled help mode.
79*9356374aSAndroid Build Coastguard Worker 
80*9356374aSAndroid Build Coastguard Worker void MaybeExit(HelpMode mode);
81*9356374aSAndroid Build Coastguard Worker 
82*9356374aSAndroid Build Coastguard Worker // --------------------------------------------------------------------
83*9356374aSAndroid Build Coastguard Worker // Globals representing usage reporting flags
84*9356374aSAndroid Build Coastguard Worker 
85*9356374aSAndroid Build Coastguard Worker // Returns substring to filter help output (--help=substr argument)
86*9356374aSAndroid Build Coastguard Worker std::string GetFlagsHelpMatchSubstr();
87*9356374aSAndroid Build Coastguard Worker // Returns the requested help mode.
88*9356374aSAndroid Build Coastguard Worker HelpMode GetFlagsHelpMode();
89*9356374aSAndroid Build Coastguard Worker // Returns the requested help format.
90*9356374aSAndroid Build Coastguard Worker HelpFormat GetFlagsHelpFormat();
91*9356374aSAndroid Build Coastguard Worker 
92*9356374aSAndroid Build Coastguard Worker // These are corresponding setters to the attributes above.
93*9356374aSAndroid Build Coastguard Worker void SetFlagsHelpMatchSubstr(absl::string_view);
94*9356374aSAndroid Build Coastguard Worker void SetFlagsHelpMode(HelpMode);
95*9356374aSAndroid Build Coastguard Worker void SetFlagsHelpFormat(HelpFormat);
96*9356374aSAndroid Build Coastguard Worker 
97*9356374aSAndroid Build Coastguard Worker // Deduces usage flags from the input argument in a form --name=value or
98*9356374aSAndroid Build Coastguard Worker // --name. argument is already split into name and value before we call this
99*9356374aSAndroid Build Coastguard Worker // function.
100*9356374aSAndroid Build Coastguard Worker bool DeduceUsageFlags(absl::string_view name, absl::string_view value);
101*9356374aSAndroid Build Coastguard Worker 
102*9356374aSAndroid Build Coastguard Worker }  // namespace flags_internal
103*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
104*9356374aSAndroid Build Coastguard Worker }  // namespace absl
105*9356374aSAndroid Build Coastguard Worker 
106*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_FLAGS_INTERNAL_USAGE_H_
107