boost::program_options
boost::program_optionsåå空éã«ããã¯ã©ã¹ãç¨ããã¨ãã³ãã³ãã©ã¤ã³å¼æ°ï¼ãªãã·ã§ã³ï¼ãæè»ã«ç°¡åã«è§£æã§ãã¾ããèªä½ã¢ããªã±ã¼ã·ã§ã³ã§ãªãã·ã§ã³ãã¤ããã®ãªãã°ä½¿ããªãæã¯ãªãã§ãã
å
·ä½çã«ã¯ã渡ãããã³ãã³ãã©ã¤ã³å¼æ°ãã³ãã³ãæ¯ã«ï¼ããã°ï¼ããããã®å¼æ°ããã¢ã«ãã¦åãããªãã·ã§ã³ã®é åºã«ä¾åãããçç¥å¯è½ãªãã·ã§ã³ãå¼æ°ãè¨å®ãããã¨ãã§ãã¾ãã
let's boostããã®ãµã³ãã«ã¨ã»ã¨ãã©ä¸ç·ã§ãããµã³ãã«ã³ã¼ããè¼ãã¾ãã
#include <iostream> #include <string> #include <boost/program_options.hpp> int main(int argc, char** argv) { namespace po = boost::program_options; po::options_description opt("ãªãã·ã§ã³"); opt.add_options() ("help,h", "ãã«ãã表示") ("op", po::value<std::string>(), "æ¼ç®åã®ç¨®é¡(add, sub)") ("lhs,l", po::value<int>(), "左辺å¤") ("rhs,r", po::value<int>(), "å³è¾ºå¤");
options_descriptionã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã«ããªãã·ã§ã³ç¾¤ã®å称ãè¨å®ãã¾ãã
次ã«ã¡ã³ãé¢æ°ã®add_optionã使ã£ã¦ãªãã·ã§ã³ãè¨å®ãã¦ããã¾ããadd_optionã®ç¬¬1å¼æ°ã«ãªãã·ã§ã³ã®"--xxx"ã®xxxã«å½ããæååãè¨å®ãã¾ããæ´ã«ã«ã³ãåºåãã§ãã®ãªãã·ã§ã³ã®çç¥è¡¨ç¾("-x"ã®x)ãè¨å®åºæ¥ã¾ããç¡ãã¦ã大ä¸å¤«ã§ãã第2å¼æ°ã«ããã®ãªãã·ã§ã³ã«å¼æ°ããªããã°ãã®ãªãã·ã§ã³ã®èª¬æããå¼æ°ãããã°value
program_optionsã¯ã©ã¹ã¯operator<<(std::ostream&, const program_options)ããªã¼ãã¼ãã¼ãããã¦ããã®ã§ãã¹ããªã¼ã ã«æµããã¨ãã§ãã¾ããã³ãã³ãä¸è¦§ã®æååï¼ãã«ãï¼ã渡ããã¾ãã
po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, opt), vm); } catch(const po::error_with_option_name& e) { std::cout << e.what() << std::endl; } po::notify(vm);
variables_mapã¯ã©ã¹ã¯ã³ãã³ãæååããã¼ã¨ãããã®ãªãã·ã§ã³ã®å¼æ°ãå¤ã¨ããmapã§ããå¤ã¯ã©ããªåã§ã大ä¸å¤«ãªããã«boost::anyåã«ãªã£ã¦ãã¾ãã
parse_commané¢æ°ã§ã³ãã³ãã©ã¤ã³å¼æ°ãoptã®ãªãã·ã§ã³ç¾¤ã«å解ããstoreé¢æ°ã§variables_mapã«ä»£å
¥ãã¦ããã¾ãã
ãã®éã«ãç»é²ããã¦ããªããªãã·ã§ã³ãã³ãã³ãã©ã¤ã³å¼æ°ã«æ¸¡ãããããå¼æ°ãå¿
è¦ãªã³ãã³ãã§å¼æ°ããªãã£ãããå¿
é ã®ãªãã·ã§ã³ããªãå ´åãerror_with_option_nameä¾å¤ãæãããã¾ãã
notifyé¢æ°ã¯variables_mapã®ã¡ã³ãé¢æ°notifyãå¼ã³ã¾ãã
if (vm.count("help") || !vm.count("op")) { std::cout << opt << std::endl; } else { try { const std::string op = vm["op"].as<std::string>(); const int lhs = vm["lhs"].as<int>(); const int rhs = vm["rhs"].as<int>(); if (op == "add") { std::cout << lhs + rhs << std::endl; } if (op == "sub") { std::cout << lhs - rhs << std::endl; } } catch(const boost::bad_any_cast& e) { std::cout << e.what() << std::endl; } }
variables_mapã®ã¡ã³ãé¢æ°countã¯ãã®ãªãã·ã§ã³ãå¹¾ã¤æ¸¡ããããè¿ãã¾ãããã®ã³ã¼ãã®å ´åã¯ãhelpãªãã·ã§ã³ãæãããopãªãã·ã§ã³ãç¡ãå ´åã¯ãã«ãã表示ããã¨ããå¦çã«ãªã£ã¦ãã¾ãããªãã·ã§ã³ã®å¼æ°ã¯asé¢æ°ã§é©åãªåãæå®ãã¦å¾ããã¾ãããã®ãªãã·ã§ã³ã«å¼æ°ããªãã®ã«å¾ããã¨ãããããã£ã¹ããåºæ¥ãªãå ´åã¯bad_any_castä¾å¤ãæãããã¾ãã
ãã¡ã¤ã«ãããªãã·ã§ã³ãèªã¿åãæ©è½ããåããªãã·ã§ã³ã«è¤æ°ã®å¤ãæå®ãããã¨ãã§ãã¾ãããä»åã¯ãããã®æ¹æ³ã¯çç¥ãã¾ãã
æå¾ã«ãµã³ãã«ã³ã¼ãå ¨ä½è¼ãã¾ãã
#include <iostream> #include <string> #include <boost/program_options.hpp> int main(int argc, char** argv) { namespace po = boost::program_options; po::options_description opt("ãªãã·ã§ã³"); opt.add_options() ("help,h", "ãã«ãã表示") ("op", po::value<std::string>(), "æ¼ç®åã®ç¨®é¡(add, sub)") ("lhs,l", po::value<int>(), "左辺å¤") ("rhs,r", po::value<int>(), "å³è¾ºå¤"); po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, opt), vm); } catch(const boost::program_options::error_with_option_name& e) { std::cout << e.what() << std::endl; } po::notify(vm); if (vm.count("help") || !vm.count("op")) { std::cout << opt << std::endl; } else { try { const std::string op = vm["op"].as<std::string>(); const int lhs = vm["lhs"].as<int>(); const int rhs = vm["rhs"].as<int>(); if (op == "add") { std::cout << lhs + rhs << std::endl; } if (op == "sub") { std::cout << lhs - rhs << std::endl; } } catch(const boost::bad_any_cast& e) { std::cout << e.what() << std::endl; } } }