|
||||
File indexing completed on 2025-01-18 09:27:17
0001 // 0002 // Copyright 2019 The Abseil Authors. 0003 // 0004 // Licensed under the Apache License, Version 2.0 (the "License"); 0005 // you may not use this file except in compliance with the License. 0006 // You may obtain a copy of the License at 0007 // 0008 // https://www.apache.org/licenses/LICENSE-2.0 0009 // 0010 // Unless required by applicable law or agreed to in writing, software 0011 // distributed under the License is distributed on an "AS IS" BASIS, 0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0013 // See the License for the specific language governing permissions and 0014 // limitations under the License. 0015 // 0016 // ----------------------------------------------------------------------------- 0017 // File: parse.h 0018 // ----------------------------------------------------------------------------- 0019 // 0020 // This file defines the main parsing function for Abseil flags: 0021 // `absl::ParseCommandLine()`. 0022 0023 #ifndef ABSL_FLAGS_PARSE_H_ 0024 #define ABSL_FLAGS_PARSE_H_ 0025 0026 #include <string> 0027 #include <vector> 0028 0029 #include "absl/base/config.h" 0030 #include "absl/flags/internal/parse.h" 0031 0032 namespace absl { 0033 ABSL_NAMESPACE_BEGIN 0034 0035 // This type represent information about an unrecognized flag in the command 0036 // line. 0037 struct UnrecognizedFlag { 0038 enum Source { kFromArgv, kFromFlagfile }; 0039 0040 explicit UnrecognizedFlag(Source s, absl::string_view f) 0041 : source(s), flag_name(f) {} 0042 // This field indicates where we found this flag: on the original command line 0043 // or read in some flag file. 0044 Source source; 0045 // Name of the flag we did not recognize in --flag_name=value or --flag_name. 0046 std::string flag_name; 0047 }; 0048 0049 inline bool operator==(const UnrecognizedFlag& lhs, 0050 const UnrecognizedFlag& rhs) { 0051 return lhs.source == rhs.source && lhs.flag_name == rhs.flag_name; 0052 } 0053 0054 namespace flags_internal { 0055 0056 HelpMode ParseAbseilFlagsOnlyImpl( 0057 int argc, char* argv[], std::vector<char*>& positional_args, 0058 std::vector<UnrecognizedFlag>& unrecognized_flags, 0059 UsageFlagsAction usage_flag_action); 0060 0061 } // namespace flags_internal 0062 0063 // ParseAbseilFlagsOnly() 0064 // 0065 // Parses a list of command-line arguments, passed in the `argc` and `argv[]` 0066 // parameters, into a set of Abseil Flag values, returning any unparsed 0067 // arguments in `positional_args` and `unrecognized_flags` output parameters. 0068 // 0069 // This function classifies all the arguments (including content of the 0070 // flagfiles, if any) into one of the following groups: 0071 // 0072 // * arguments specified as "--flag=value" or "--flag value" that match 0073 // registered or built-in Abseil Flags. These are "Abseil Flag arguments." 0074 // * arguments specified as "--flag" that are unrecognized as Abseil Flags 0075 // * arguments that are not specified as "--flag" are positional arguments 0076 // * arguments that follow the flag-terminating delimiter (`--`) are also 0077 // treated as positional arguments regardless of their syntax. 0078 // 0079 // All of the deduced Abseil Flag arguments are then parsed into their 0080 // corresponding flag values. If any syntax errors are found in these arguments, 0081 // the binary exits with code 1. 0082 // 0083 // This function also handles Abseil Flags built-in usage flags (e.g. --help) 0084 // if any were present on the command line. 0085 // 0086 // All the remaining positional arguments including original program name 0087 // (argv[0]) are are returned in the `positional_args` output parameter. 0088 // 0089 // All unrecognized flags that are not otherwise ignored are returned in the 0090 // `unrecognized_flags` output parameter. Note that the special `undefok` 0091 // flag allows you to specify flags which can be safely ignored; `undefok` 0092 // specifies these flags as a comma-separated list. Any unrecognized flags 0093 // that appear within `undefok` will therefore be ignored and not included in 0094 // the `unrecognized_flag` output parameter. 0095 // 0096 void ParseAbseilFlagsOnly(int argc, char* argv[], 0097 std::vector<char*>& positional_args, 0098 std::vector<UnrecognizedFlag>& unrecognized_flags); 0099 0100 // ReportUnrecognizedFlags() 0101 // 0102 // Reports an error to `stderr` for all non-ignored unrecognized flags in 0103 // the provided `unrecognized_flags` list. 0104 void ReportUnrecognizedFlags( 0105 const std::vector<UnrecognizedFlag>& unrecognized_flags); 0106 0107 // ParseCommandLine() 0108 // 0109 // First parses Abseil Flags only from the command line according to the 0110 // description in `ParseAbseilFlagsOnly`. In addition this function handles 0111 // unrecognized and usage flags. 0112 // 0113 // If any unrecognized flags are located they are reported using 0114 // `ReportUnrecognizedFlags`. 0115 // 0116 // If any errors detected during command line parsing, this routine reports a 0117 // usage message and aborts the program. 0118 // 0119 // If any built-in usage flags were specified on the command line (e.g. 0120 // `--help`), this function reports help messages and then gracefully exits the 0121 // program. 0122 // 0123 // This function returns all the remaining positional arguments collected by 0124 // `ParseAbseilFlagsOnly`. 0125 std::vector<char*> ParseCommandLine(int argc, char* argv[]); 0126 0127 ABSL_NAMESPACE_END 0128 } // namespace absl 0129 0130 #endif // ABSL_FLAGS_PARSE_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |