{"id":221672,"date":"2021-07-14T10:01:08","date_gmt":"2021-07-14T02:01:08","guid":{"rendered":"https:\/\/gulass.cn\/?p=221672"},"modified":"2021-06-28T11:02:18","modified_gmt":"2021-06-28T03:02:18","slug":"linux-boost-two","status":"publish","type":"post","link":"https:\/\/gulass.cn\/linux-boost-two.html","title":{"rendered":"\u7b80\u5355\u89e3\u6790C++\u57fa\u4e8eBoost\u5e93\u5b9e\u73b0\u547d\u4ee4\u884c"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nBoost\u5e93\u4e2d\u9ed8\u8ba4\u81ea\u5e26\u4e86\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u5668\uff0c\u4ee5\u5f80\u6211\u90fd\u662f\u81ea\u5df1\u5b9e\u73b0\u53c2\u6570\u89e3\u6790\u7684\uff0c\u4eca\u5929\u5076\u5c14\u53d1\u73b0\u8fd9\u4e2a\u597d\u4e1c\u897f\uff0c\u5c31\u6765\u603b\u7ed3\u4e00\u4e0b\u53c2\u6570\u89e3\u6790\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u8be5\u5e93\u9700\u8981\u5f15\u5165program_options.hpp\u5934\u6587\u4ef6\uff0c\u5373\u53ef\u4f7f\u7528\u4e86<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u7b2c\u4e00\u6b21\u5c1d\u8bd5<\/strong><\/div>\n
#include \r\n#include \r\n\r\n\/\/ \u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\r\nnamespace opt = boost::program_options;\r\n\r\nint main(int argc, char const *argv[])\r\n{\r\n\topt::options_description desc(\"Usage: 32\u4f4d\u7aef\u53e3\u5feb\u901f\u626b\u63cf\u5668\\n\\n options: \\n\");\r\n\r\n\tdesc.add_options()\r\n\t\t(\"Address,a\", opt::value()->default_value(\"127.0.0.1\"), \"\u8f93\u5165\u4e00\u4e2aIP\u5730\u5740\"),\r\n\t\t(\"StartPort,s\", opt::value()->default_value(1024), \"\u4f20\u5165\u626b\u63cf\u8d77\u59cb\u7aef\u53e3\"),\r\n\t\t(\"EndPort,e\", opt::value<\/int>()->default_value(65535), \"\u4f20\u5165\u626b\u63cf\u7ed3\u675f\u7aef\u53e3\"),\r\n\t\t(\"Help,h\", \"\u5f39\u51fa\u5e2e\u52a9\u83dc\u5355\");\r\n\r\n\t\/\/ \u89e3\u6790\u53c2\u6570\u5c06\u503c\u4f20\u9012\u7ed9virtual_map\r\n\topt::variables_map virtual_map;\r\n\r\n\ttry\r\n\t{\r\n\t\topt::store(opt::parse_command_line(argc, argv, desc), virtual_map);\r\n\t}\r\n\tcatch (...)\r\n\t{\r\n\t\tstd::cout << \"error \\n\";\r\n\t\treturn 0;\r\n\t}\r\n\r\n\t\/\/ \u53c2\u6570\u89e3\u6790\u5b8c\u6bd5\uff0c\u5904\u7406\r\n\tif (virtual_map.count(\"Help\"))\r\n\t{\r\n\t\tprintf(\"\u5e2e\u52a9\");\r\n\t}\r\n\tif (virtual_map.count(\"Address\"))\r\n\t{\r\n\t\tstd::cout << \"\u627e\u5230\" << virtual_map[\"Address\"].as() << std::endl;\r\n\t}\r\n\tif (virtual_map.empty())\r\n\t{\r\n\t\tstd::cout << \"no options\\n\";\r\n\t}\r\n\treturn 0;\r\n}<\/std::string><\/int><\/std::string><\/iostream><\/pre>\n
\u7b2c\u4e8c\u6b21\u5c1d\u8bd5<\/strong><\/div>\n
#include   \r\n#include   \r\n#include \r\n#include   \r\n\r\nnamespace opt = boost::program_options;\r\n\r\nint main(int argc, char const *argv[])\r\n{\r\n\tint start_port = 1024, end_port = 65535;\r\n\tstd::vector address;\r\n\r\n\topt::options_description opt(\"\\nUsage: 32\u4f4d\u7aef\u53e3\u5feb\u901f\u626b\u63cf\u5668 Ver:1.0 \\n\\n options: \\n\");\r\n\topt.add_options()\r\n\t\t(\"address,a\", opt::value >()->multitoken(), \"\u6307\u5b9a\u5730\u5740\")\r\n\t\t(\"start_port,s\", opt::value(&start_port)->default_value(1024), \"\u5f00\u59cb\u7aef\u53e3\")\r\n\t\t(\"end_port,e\", opt::value<\/int>(&end_port)->default_value(65535), \"\u7ed3\u675f\u7aef\u53e3\")\r\n\t\t(\"help\", \"\u5e2e\u52a9\u83dc\u5355\");\r\n\r\n\topt::variables_map vm;\r\n\ttry\r\n\t{\r\n\t\topt::store(parse_command_line(argc, argv, opt), vm);\r\n\t}\r\n\tcatch (...){\r\n\t\tstd::cout << \"command error\uff01\\n\";\r\n\t\treturn 0;\r\n\t}\r\n\r\n\topt::notify(vm);\r\n\tif (vm.count(\"help\"))\r\n\t{\r\n\t\tstd::cout << opt << std::endl;\r\n\t\treturn 0;\r\n\t}\r\n\tif (vm.count(\"address\") && vm.count(\"start_port\") && vm.count(\"end_port\"))\r\n\t{\r\n\t\t\/\/\u904d\u5386\u9009\u9879\u503c  \r\n\t\tfor (auto& str : vm[\"address\"].as >())\r\n\t\t\tstd::cout << str << \" \";\r\n\t\tint x = vm[\"start_port\"].as();\r\n\t\tstd::cout << x << std::endl;\r\n\t}\r\n\treturn 0;\r\n}<\/int><\/std::string><\/int><\/std::string><\/string><\/vector><\/iostream><\/pre>\n
\u6700\u7ec8\u7248<\/strong><\/div>\n
#include \r\n#include \r\n\r\nnamespace opt = boost::program_options;\r\n\r\nint main(int argc, char const *argv[])\r\n{\r\n\topt::options_description des_cmd(\"\\n Usage: 32\u4f4d\u7aef\u53e3\u5feb\u901f\u626b\u63cf\u5668 Ver:1.0 \\n\\n Options: \\n\");\r\n\tdes_cmd.add_options()\r\n\t\t(\"address,a\", opt::value()->default_value(\"127.0.0.1\"), \"\u6307\u5b9a\u5730\u5740\")\r\n\t\t(\"start_port,s\", opt::value()->default_value(1024), \"\u5f00\u59cb\u7aef\u53e3\")\r\n\t\t(\"end_port,e\", opt::value<\/int>()->default_value(65535), \"\u7ed3\u675f\u7aef\u53e3\")\r\n\t\t(\"help,h\", \"\u5e2e\u52a9\u83dc\u5355\");\r\n\r\n\topt::variables_map virtual_map;\r\n\ttry\r\n\t{\r\n\t\topt::store(opt::parse_command_line(argc, argv, des_cmd), virtual_map);\r\n\t}\r\n\tcatch (...){ return 0; }\r\n\t\r\n\t\/\/ \u5b9a\u4e49\u6d88\u606f\r\n\topt::notify(virtual_map);\r\n\t\r\n\t\/\/ \u65e0\u53c2\u6570\u76f4\u63a5\u8fd4\u56de\r\n\tif (virtual_map.empty())\r\n\t{\r\n\t\treturn 0;\r\n\t}\r\n\telse if (virtual_map.count(\"help\") || virtual_map.count(\"h\"))\r\n\t{\r\n\t\tstd::cout << des_cmd << std::endl;\r\n\t\treturn 0;\r\n\t}\r\n\telse if (virtual_map.count(\"address\") && virtual_map.count(\"start_port\") && virtual_map.count(\"end_port\"))\r\n\t{\r\n\t\tstd::cout << \"Addr = \" << virtual_map[\"address\"].as() << std::endl;\r\n\t\tstd::cout << \"StartPort = \" << virtual_map[\"start_port\"].as() << std::endl;\r\n\t\tstd::cout << \"EndPort = \" << virtual_map[\"end_port\"].as<\/int>() << std::endl;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tstd::cout << \"option error\" << std::endl;\r\n\t}\r\n\treturn 0;\r\n}<\/int><\/std::string><\/int><\/std::string><\/iostream><\/pre>\n

\u547d\u4ee4\u884c\u4e0b\u4f7f\u7528help\u8f93\u51fa\u5e2e\u52a9\u83dc\u5355\uff0c\u5f53\u4f20\u5165\u4e09\u4e2a\u53c2\u6570\u65f6\uff0c\u5373\u53ef\u89e3\u6790\u5230\u7b2c\u4e8c\u4e2a\u5224\u65ad\u4e0a\uff0c\u6267\u884c\u76f8\u5e94\u7684\u51fd\u6570\u5373\u53ef\u3002<\/p>\n

\"\"<\/p>\n

\u4ee5\u4e0a\u5c31\u662fC++\u57fa\u4e8eBoost\u5e93\u5b9e\u73b0\u547d\u4ee4\u884c\u89e3\u6790\u7684\u8be6\u7ec6\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

#include #include \/\/ \u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 namespace opt = boost::prog […]<\/p>\n","protected":false},"author":1482,"featured_media":4213,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/221672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=221672"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/221672\/revisions"}],"predecessor-version":[{"id":221678,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/221672\/revisions\/221678"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/4213"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=221672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=221672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=221672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}