Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/option-parser.h
1 /* 2 * Copyright 2016 WebAssembly Community Group participants 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 * http://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 #ifndef WABT_OPTION_PARSER_H_ 18 #define WABT_OPTION_PARSER_H_ 19 20 #include <functional> 21 #include <string> 22 #include <vector> 23 24 #include "wabt/common.h" 25 26 namespace wabt { 27 28 class OptionParser { 29 public: 30 enum class HasArgument { No, Yes }; 31 enum class ArgumentCount { One, OneOrMore, ZeroOrMore }; 32 33 struct Option; 34 using Callback = std::function<void(const char*)>; 35 using NullCallback = std::function<void()>; 36 37 struct Option { 38 Option(char short_name, 39 const std::string& long_name, 40 const std::string& metavar, 41 HasArgument has_argument, 42 const std::string& help, 43 const Callback&); 44 45 char short_name; 46 std::string long_name; 47 std::string metavar; 48 bool has_argument; 49 std::string help; 50 Callback callback; 51 }; 52 53 struct Argument { 54 Argument(const std::string& name, ArgumentCount, const Callback&); 55 56 std::string name; 57 ArgumentCount count; 58 Callback callback; 59 int handled_count = 0; 60 }; 61 62 explicit OptionParser(const char* program_name, const char* description); 63 64 void AddOption(const Option&); 65 void AddArgument(const std::string& name, ArgumentCount, const Callback&); 66 void SetErrorCallback(const Callback&); 67 void Parse(int argc, char* argv[]); 68 void PrintHelp(); 69 70 // Helper functions. 71 void AddOption(char short_name, 72 const char* long_name, 73 const char* help, 74 const NullCallback&); 75 void AddOption(const char* long_name, const char* help, const NullCallback&); 76 void AddOption(char short_name, 77 const char* long_name, 78 const char* metavar, 79 const char* help, 80 const Callback&); 81 82 private: 83 static int Match(const char* s, const std::string& full, bool has_argument); 84 void WABT_PRINTF_FORMAT(2, 3) Errorf(const char* format, ...); 85 void HandleArgument(size_t* arg_index, const char* arg_value); 86 87 // Print the error and exit(1). 88 void DefaultError(const std::string&); 89 90 std::string program_name_; 91 std::string description_; 92 std::vector<Option> options_; 93 std::vector<Argument> arguments_; 94 Callback on_error_; 95 }; 96 97 } // namespace wabt 98 99 #endif /* WABT_OPTION_PARSER_H_ */