|
| 1 | + |
| 2 | +#include "program_options.hpp" |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | +#include <iostream> |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +//constructor |
| 9 | +Program_Options::Program_Options(int const& argc, char** const& argv){ |
| 10 | + using namespace boost::program_options; |
| 11 | + |
| 12 | + //build all the possible flags and add description. |
| 13 | + options_description desc (Get_Options_Description()); |
| 14 | + |
| 15 | + //set positional arguments |
| 16 | + positional_options_description pod; |
| 17 | + pod.add("query", -1); |
| 18 | + |
| 19 | + //build variable map |
| 20 | + Build_Variable_Map(argc,argv,desc,pod); |
| 21 | + |
| 22 | + //process immediate options |
| 23 | + Process_Immediate_Options(desc); |
| 24 | + |
| 25 | + //validate the mandatory flags |
| 26 | + Check_For_Mandatory_Flags_Not_Passed(); |
| 27 | +} |
| 28 | +boost::program_options::options_description Program_Options::Get_Options_Description(void){ |
| 29 | + using namespace boost::program_options; |
| 30 | + |
| 31 | + //Program Description |
| 32 | + options_description desc("adjust bazel visibility across a codebase."); |
| 33 | + |
| 34 | + //Program Flags |
| 35 | + desc.add_options() |
| 36 | + |
| 37 | + //these are flag descriptions of that can be passed into the class. |
| 38 | + //the code inserted, are the flags added by the user through the |
| 39 | + //program_options_maker flag interface |
| 40 | + ("run_dir",value<std::string>(),"where to run the analysis") |
| 41 | + ("file",value<std::string>(),"the file to remove headers from") |
| 42 | + ("dir",value<std::string>(),"the dir to remove headers from") |
| 43 | + ("target",value<std::string>(),"target to check with") |
| 44 | + ("commands",value<std::vector<std::string>>(),"commands to check with") |
| 45 | + ("start-at",value<int>(),"where to start in the file iteration") |
| 46 | + ("find",value<std::string>(),"regex to match on") |
| 47 | + ("replace",value<std::string>(),"what to replace the regex match with") |
| 48 | + |
| 49 | + //+----------------------------------------------------------+ |
| 50 | + //| Obligatory | |
| 51 | + //+----------------------------------------------------------+ |
| 52 | + ("help,h","produce this help message") |
| 53 | + ("version,v","display version") |
| 54 | + ; |
| 55 | + |
| 56 | + return desc; |
| 57 | +} |
| 58 | +std::string Program_Options::Get_Help_Message(){ |
| 59 | + std::stringstream ss; |
| 60 | + ss << Get_Options_Description(); |
| 61 | + return ss.str(); |
| 62 | +} |
| 63 | +void Program_Options::Build_Variable_Map(int const& argc, char** const& argv, boost::program_options::options_description const& desc, boost::program_options::positional_options_description const& pod){ |
| 64 | + using namespace boost::program_options; |
| 65 | + |
| 66 | + //store user flag data. crash elegantly if they pass incorrect flags. |
| 67 | + try{ |
| 68 | + store(command_line_parser(argc, argv).options(desc).positional(pod).run(), vm); |
| 69 | + notify(vm); |
| 70 | + } |
| 71 | + catch(error& e){ |
| 72 | + std::cerr << "ERROR: " << e.what() << std::endl; |
| 73 | + std::cerr << desc << std::endl; |
| 74 | + exit(EXIT_FAILURE); |
| 75 | + } |
| 76 | + |
| 77 | + return; |
| 78 | +} |
| 79 | +void Program_Options::Process_Immediate_Options( boost::program_options::options_description const& desc){ |
| 80 | + |
| 81 | + //do not continue the program if the user wanted to see the version or help data |
| 82 | + if (vm.count("version")){ |
| 83 | + std::cout << "\nThis is version " << "1" << " of noogle.\n\n"; |
| 84 | + exit(EXIT_SUCCESS); |
| 85 | + } |
| 86 | + else if (vm.count("help")){ |
| 87 | + std::cout << '\n' << desc << '\n'; |
| 88 | + exit(EXIT_SUCCESS); |
| 89 | + } |
| 90 | + |
| 91 | + return; |
| 92 | +} |
| 93 | + |
| 94 | +void Program_Options::Check_For_Mandatory_Flags_Not_Passed(){ |
| 95 | + std::vector<std::string> flags_not_passed; |
| 96 | + //if(!vm.count("input_files")){flags_not_passed.push_back("input_files");} |
| 97 | + //if(!vm.count("exporter")){flags_not_passed.push_back("exporter");} |
| 98 | + //if(!vm.count("language")){flags_not_passed.push_back("language");} |
| 99 | + |
| 100 | + if (!flags_not_passed.empty()){ |
| 101 | + std::cerr << "you need to pass the following flags still:\n"; |
| 102 | + for (auto it: flags_not_passed){ |
| 103 | + std::cerr << '\t' << it << '\n'; |
| 104 | + } |
| 105 | + exit(EXIT_FAILURE); |
| 106 | + } |
| 107 | + return; |
| 108 | +} |
| 109 | +std::string Program_Options::Run_Dir() const{ |
| 110 | + std::string data; |
| 111 | + if (vm.count("run_dir")){ |
| 112 | + data = vm["run_dir"].as<std::string>(); |
| 113 | + } |
| 114 | + |
| 115 | + return data; |
| 116 | +} |
| 117 | +std::string Program_Options::File() const{ |
| 118 | + std::string data; |
| 119 | + if (vm.count("file")){ |
| 120 | + data = vm["file"].as<std::string>(); |
| 121 | + } |
| 122 | + |
| 123 | + return data; |
| 124 | +} |
| 125 | +std::string Program_Options::Target() const{ |
| 126 | + std::string data; |
| 127 | + if (vm.count("target")){ |
| 128 | + data = vm["target"].as<std::string>(); |
| 129 | + } |
| 130 | + |
| 131 | + return data; |
| 132 | +} |
| 133 | +std::string Program_Options::Dir() const{ |
| 134 | + std::string data; |
| 135 | + if (vm.count("dir")){ |
| 136 | + data = vm["dir"].as<std::string>(); |
| 137 | + } |
| 138 | + |
| 139 | + return data; |
| 140 | +} |
| 141 | + |
| 142 | +int Program_Options::Start_At() const{ |
| 143 | + int data = 0; |
| 144 | + if (vm.count("start-at")){ |
| 145 | + data = vm["start-at"].as<int>(); |
| 146 | + } |
| 147 | + |
| 148 | + return data; |
| 149 | +} |
| 150 | + |
| 151 | +std::string Program_Options::Find() const{ |
| 152 | + std::string data; |
| 153 | + if (vm.count("find")){ |
| 154 | + data = vm["find"].as<std::string>(); |
| 155 | + } |
| 156 | + return data; |
| 157 | +} |
| 158 | +std::string Program_Options::Replace() const{ |
| 159 | + std::string data; |
| 160 | + if (vm.count("replace")){ |
| 161 | + data = vm["replace"].as<std::string>(); |
| 162 | + } |
| 163 | + return data; |
| 164 | +} |
| 165 | + |
| 166 | +std::vector<std::string> Program_Options::Commands() const{ |
| 167 | + std::vector<std::string> data; |
| 168 | + if (vm.count("commands")){ |
| 169 | + data = vm["commands"].as<std::vector<std::string>>(); |
| 170 | + } |
| 171 | + |
| 172 | + return data; |
| 173 | +} |
0 commit comments