- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/UIElements/CommandLineUI/CommandLineParser.cpp ¶
r3a21a7 re4afb4 21 21 22 22 #include <boost/filesystem.hpp> 23 #include <boost/lexical_cast.hpp> 23 24 #include <boost/program_options.hpp> 24 25 #include <fstream> 25 26 #include <iostream> 26 27 #include <map> 28 #include <vector> 27 29 28 30 #include "Actions/Action.hpp" … … 32 34 #include "Actions/OptionTrait.hpp" 33 35 #include "Actions/Values.hpp" 36 #include "Helpers/Assert.hpp" 34 37 #include "Helpers/Log.hpp" 35 38 #include "Helpers/Verbose.hpp" 36 39 #include "CommandLineParser.hpp" 37 #include "CommandLineParser_validate.hpp"38 40 39 41 #include "Patterns/Singleton_impl.hpp" 40 42 41 43 class element; 44 45 using namespace std; 46 47 /** boost::program_options validator specialization for VectorValue. 48 * \param &v reference for return value 49 * \param &values string vector of scanned options 50 * \param * 51 * \param 52 * 53 */ 54 void validate(boost::any& v, const std::vector<std::string>& values, VectorValue *, int) 55 { 56 VectorValue VV; 57 std::vector<std::string> components; 58 59 // split comma-separated values 60 if (values.size() != 1) { 61 cerr << "Not one vector but " << values.size() << " given " << endl; 62 throw boost::program_options::validation_error("Unequal to one vector given"); 63 } 64 std::string argument(values.at(0)); 65 std::string::iterator Aiter = argument.begin(); 66 std::string::iterator Biter = argument.begin(); 67 for (; Aiter != argument.end(); ++Aiter) { 68 if (*Aiter == ',') { 69 components.push_back(string(Biter,Aiter)); 70 do { 71 Aiter++; 72 } while (*Aiter == ' ' || *Aiter == '\t'); 73 Biter = Aiter; 74 } 75 } 76 components.push_back(string(Biter,argument.end())); 77 78 if (components.size() != 3) { 79 cerr << "Specified vector does not have three components but " << components.size() << endl; 80 throw boost::program_options::validation_error("Specified vector does not have three components"); 81 } 82 VV.x = boost::lexical_cast<double>(components.at(0)); 83 VV.y = boost::lexical_cast<double>(components.at(1)); 84 VV.z = boost::lexical_cast<double>(components.at(2)); 85 v = boost::any(VectorValue(VV)); 86 } 87 88 void validate(boost::any& v, const std::vector<std::string>& values, BoxValue *, int) 89 { 90 BoxValue BV; 91 std::vector<std::string> components; 92 93 // split comma-separated values 94 if (values.size() != 1) { 95 cerr << "Not one vector but " << values.size() << " given " << endl; 96 throw boost::program_options::validation_error("Unequal to one vector given"); 97 } 98 std::string argument(values.at(0)); 99 std::string::iterator Aiter = argument.begin(); 100 std::string::iterator Biter = argument.begin(); 101 for (; Aiter != argument.end(); ++Aiter) { 102 if (*Aiter == ',') { 103 components.push_back(string(Biter,Aiter)); 104 do { 105 Aiter++; 106 } while (*Aiter == ' ' || *Aiter == '\t'); 107 Biter = Aiter; 108 } 109 } 110 components.push_back(string(Biter,argument.end())); 111 112 if (components.size() != 6) { 113 cerr << "Specified vector does not have three components but " << components.size() << endl; 114 throw boost::program_options::validation_error("Specified symmetric box matrix does not have six components"); 115 } 116 BV.xx = boost::lexical_cast<double>(components.at(0)); 117 BV.yx = boost::lexical_cast<double>(components.at(1)); 118 BV.yy = boost::lexical_cast<double>(components.at(2)); 119 BV.zx = boost::lexical_cast<double>(components.at(3)); 120 BV.zy = boost::lexical_cast<double>(components.at(4)); 121 BV.zz = boost::lexical_cast<double>(components.at(5)); 122 v = boost::any(BoxValue(BV)); 123 } 124 125 /** boost::program_options validator specialization for boost::filesystem::path. 126 * \param &v reference for return value 127 * \param &values string vector of scanned options 128 * \param * 129 * \param 130 * 131 */ 132 void validate(boost::any& v, const std::vector<std::string>& values, boost::filesystem::path *, int) 133 { 134 boost::filesystem::path filename; 135 136 std::cout << "boost::filesystem::path validator used." << std::endl; 137 138 // split comma-separated values 139 if (values.size() != 1) { 140 cerr << "Not one file but " << values.size() << " given " << endl; 141 throw boost::program_options::validation_error("Unequal to one file given"); 142 } 143 filename = values.at(0); 144 v = boost::any(boost::filesystem::path(filename)); 145 } 146 42 147 43 148 /** Constructor of class CommandLineParser. … … 356 461 { 357 462 po::store(po::command_line_parser(argc,argv).options(cmdline_options).run(), vm); 358 std::ifstream input;463 ifstream input; 359 464 input.open("example.cfg"); 360 465 if (!input.fail()) … … 368 473 void CommandLineParser::scanforSequenceOfArguments() 369 474 { 370 std::map <std::string, std::string> ShortFormToActionMap = getShortFormToActionMap();475 map <std::string, std::string> ShortFormToActionMap = getShortFormToActionMap(); 371 476 // go through all arguments 372 477 for (int i=1;i<argc;i++) { 373 ( std::cout << Verbose(1) << "Checking on " << argv[i] << std::endl);478 (cout << Verbose(1) << "Checking on " << argv[i] << endl); 374 479 // check whether they 375 480 if (argv[i][0] == '-') { // .. begin with - … … 380 485 // .. and check that next letter is not numeric, if so insert 381 486 } else if (((argv[i][1] < '0') || (argv[i][1] > '9')) && ((argv[i][1] != '.'))) { 382 std::map <std::string, std::string>::iterator iter = ShortFormToActionMap.find(&(argv[i][1]));487 map <std::string, std::string>::iterator iter = ShortFormToActionMap.find(&(argv[i][1])); 383 488 if (iter != ShortFormToActionMap.end()) { 384 489 (cout << Verbose(1) << "Putting " << iter->second << " for " << iter->first << " into the sequence." << endl); … … 406 511 std::map <std::string, std::string> CommandLineParser::getShortFormToActionMap() 407 512 { 408 std::map <std::string, std::string> result;513 map <std::string, std::string> result; 409 514 410 515 ActionRegistry &AR = ActionRegistry::getInstance();
Note:
See TracChangeset
for help on using the changeset viewer.