|
| 1 | +/* |
| 2 | +http://www.apache.org/licenses/LICENSE-2.0.txt |
| 3 | +Copyright 2017 Intel Corporation |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | +#pragma once |
| 15 | + |
| 16 | +#include <boost/program_options.hpp> |
| 17 | +#include <fstream> |
| 18 | +#include <iostream> |
| 19 | +#include <iterator> |
| 20 | +#include <map> |
| 21 | +#include <spdlog/spdlog.h> |
| 22 | +#include <snap/rpc/plugin.pb.h> |
| 23 | + |
| 24 | + |
| 25 | +#define COMMAND_DESC "COMMANDS" |
| 26 | +#define GLOBAL_DESC "GLOBAL OPTIONS" |
| 27 | +#define ADDITIONAL_DESC "" |
| 28 | + |
| 29 | +namespace po = boost::program_options; |
| 30 | +namespace spd = spdlog; |
| 31 | + |
| 32 | +using namespace std; |
| 33 | + |
| 34 | + |
| 35 | +namespace Plugin { |
| 36 | + //helper function to simplify main part |
| 37 | + template<class T> |
| 38 | + ostream& operator<<(ostream& os, const vector<T>& v) { |
| 39 | + copy(v.begin(), v.end(), ostream_iterator<T>(os, " ")); |
| 40 | + return os; |
| 41 | + }; |
| 42 | + |
| 43 | + class Flags { |
| 44 | + private: |
| 45 | + std::shared_ptr<spd::logger> _logger; |
| 46 | + |
| 47 | + po::variables_map _flags; |
| 48 | + |
| 49 | + // Initial option descriptions |
| 50 | + po::options_description _command{COMMAND_DESC}, _global{GLOBAL_DESC}; |
| 51 | + po::options_description _hidden, _additional{ADDITIONAL_DESC}; |
| 52 | + |
| 53 | + // Combined option descriptions |
| 54 | + po::options_description _visible, _command_line, _config_file; |
| 55 | + |
| 56 | + // Default variables |
| 57 | + int _log_level, _stand_alone_port, _max_collect_duration; |
| 58 | + std::string _options_file, _listen_port, _listen_addr, _cert_path, _key_path, _root_cert_paths; |
| 59 | + int64_t _max_metrics_buffer; |
| 60 | + |
| 61 | + bool _framework_json_parsed; |
| 62 | + |
| 63 | + public: |
| 64 | + enum FlagType { |
| 65 | + Bool, |
| 66 | + Int, |
| 67 | + String |
| 68 | + }; |
| 69 | + |
| 70 | + enum FlagLevel { |
| 71 | + Command, |
| 72 | + Global, |
| 73 | + Hidden, |
| 74 | + Custom |
| 75 | + }; |
| 76 | + |
| 77 | + Flags() { |
| 78 | + _logger = spdlog::stderr_logger_mt("flags"); |
| 79 | + } |
| 80 | + |
| 81 | + Flags(const int &argc, char **argv) : |
| 82 | + _framework_json_parsed(false) { |
| 83 | + std::string logger_name = argc > 0 ? "flags_" + std::string(argv[0]) : "flags"; |
| 84 | + _logger = spdlog::stderr_logger_mt(logger_name); |
| 85 | + this->SetFlags(); |
| 86 | + this->ParseFlags(argc, argv); |
| 87 | + } |
| 88 | + |
| 89 | + int addDefaultCommandFlags(); |
| 90 | + int addDefaultGlobalFlags(); |
| 91 | + int addDefaultHiddenFlags(); |
| 92 | + int SetDefaultFlags(); |
| 93 | + |
| 94 | + int addBoolFlag(const char *optionName, const char *description, |
| 95 | + FlagLevel flagLevel); |
| 96 | + int addIntFlag(const char *optionName, const char *description, |
| 97 | + FlagLevel flagLevel); |
| 98 | + int addStringFlag(const char *optionName, const char *description, |
| 99 | + FlagLevel flagLevel); |
| 100 | + int AddFlag(const char *optionName, const char *description, |
| 101 | + FlagType flagType, FlagLevel flagLevel); |
| 102 | + |
| 103 | + int setVisibleFlags(); |
| 104 | + int setCommandLineFlags(); |
| 105 | + int setConfigFileFlags(); |
| 106 | + int SetCombinedFlags(); |
| 107 | + |
| 108 | + int SetFlags() { |
| 109 | + if (SetDefaultFlags() != 0) return 1; |
| 110 | + if (SetCombinedFlags() != 0) return 1; |
| 111 | + return 0; |
| 112 | + } |
| 113 | + |
| 114 | + int parseCommandLineFlags(const int &argc, char **argv); |
| 115 | + int parsejsonFlags(const int &argc, char **argv); |
| 116 | + int parseConfigFileFlags(std::string filePathAndName = ""); |
| 117 | + int ParseFlags(const int &argc, char **argv, std::string filePathAndName = ""); |
| 118 | + |
| 119 | + |
| 120 | + void ShowVariablesMap(); |
| 121 | + bool IsParsedFlag(const char *flagKey) { return _flags.count(flagKey); } |
| 122 | + po::variables_map GetFlagsVM() { return _flags; } |
| 123 | + const bool IsConfigFromFramework() const {return _framework_json_parsed; } |
| 124 | + |
| 125 | + bool GetFlagBoolValue(const char *flagKey); |
| 126 | + int GetFlagIntValue(const char *flagKey); |
| 127 | + int64_t GetFlagInt64Value(const char *flagKey); |
| 128 | + std::string GetFlagStrValue(const char *flagKey); |
| 129 | + |
| 130 | + void SetFlagsLogLevel(const int &logLevel = 2); |
| 131 | + |
| 132 | + int helpFlagCalled() { |
| 133 | + std::cout << _visible << std::endl; |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + po::options_description GetCommandOptions() { return _command; } |
| 138 | + void PrintCommandOptions() { std::cout << _command << std::endl; } |
| 139 | + |
| 140 | + po::options_description GetGlobalOptions() { return _global; } |
| 141 | + void PrintGlobalOptions() { std::cout << _global << std::endl; } |
| 142 | + |
| 143 | + po::options_description GetHiddenOptions() { return _hidden; } |
| 144 | + void PrintHiddenOptions() { std::cout << _hidden << std::endl; } |
| 145 | + |
| 146 | + po::options_description GetAdditionalOptions() { return _additional; } |
| 147 | + void PrintAdditionalOptions() { std::cout << _additional << std::endl; } |
| 148 | + |
| 149 | + po::options_description GetVisibleOptions() { return _visible; } |
| 150 | + void PrintVisibleOptions() { std::cout << _visible << std::endl; } |
| 151 | + |
| 152 | + po::options_description GetCommandLineOptions() { return _command_line; } |
| 153 | + void PrintCommandLineOptions() { std::cout << _command_line << std::endl; } |
| 154 | + |
| 155 | + po::options_description GetConfigFileOptions() { return _config_file; } |
| 156 | + void PrintConfigFileOptions() { std::cout << _config_file << std::endl; } |
| 157 | + |
| 158 | + const rpc::ConfigMap GenerateConfigMapFromCommandJson(); |
| 159 | + }; |
| 160 | +} // namespace Plugin |
0 commit comments