C++怎么用Boost庫(kù)實(shí)現(xiàn)命令行-創(chuàng)新互聯(lián)

這篇文章主要介紹“C++怎么用Boost庫(kù)實(shí)現(xiàn)命令行”,在日常操作中,相信很多人在C++怎么用Boost庫(kù)實(shí)現(xiàn)命令行問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C++怎么用Boost庫(kù)實(shí)現(xiàn)命令行”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

為企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站優(yōu)化、成都全網(wǎng)營(yíng)銷推廣、競(jìng)價(jià)托管、品牌運(yùn)營(yíng)等營(yíng)銷獲客服務(wù)。創(chuàng)新互聯(lián)擁有網(wǎng)絡(luò)營(yíng)銷運(yùn)營(yíng)團(tuán)隊(duì),以豐富的互聯(lián)網(wǎng)營(yíng)銷經(jīng)驗(yàn)助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營(yíng)銷獲客難題,做到“讓獲客更簡(jiǎn)單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營(yíng)銷”三大難題,同時(shí)降低了營(yíng)銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!

第一次嘗試

#include <iostream>
#include <boost/program_options.hpp>

// 定義命名空間
namespace opt = boost::program_options;

int main(int argc, char const *argv[])
{
	opt::options_description desc("Usage: 32位端口快速掃描器\n\n options: \n");

	desc.add_options()
		("Address,a", opt::value<std::string>()->default_value("127.0.0.1"), "輸入一個(gè)IP地址"),
		("StartPort,s", opt::value<int>()->default_value(1024), "傳入掃描起始端口"),
		("EndPort,e", opt::value<int>()->default_value(65535), "傳入掃描結(jié)束端口"),
		("Help,h", "彈出幫助菜單");

	// 解析參數(shù)將值傳遞給virtual_map
	opt::variables_map virtual_map;

	try
	{
		opt::store(opt::parse_command_line(argc, argv, desc), virtual_map);
	}
	catch (...)
	{
		std::cout << "error \n";
		return 0;
	}

	// 參數(shù)解析完畢,處理
	if (virtual_map.count("Help"))
	{
		printf("幫助");
	}
	if (virtual_map.count("Address"))
	{
		std::cout << "找到" << virtual_map["Address"].as<std::string>() << std::endl;
	}
	if (virtual_map.empty())
	{
		std::cout << "no options\n";
	}
	return 0;
}

第二次嘗試

#include <iostream>  
#include <vector>  
#include <string>
#include <boost/program_options.hpp>  

namespace opt = boost::program_options;

int main(int argc, char const *argv[])
{
	int start_port = 1024, end_port = 65535;
	std::vector<std::string> address;

	opt::options_description opt("\nUsage: 32位端口快速掃描器 Ver:1.0 \n\n options: \n");
	opt.add_options()
		("address,a", opt::value<std::vector<std::string> >()->multitoken(), "指定地址")
		("start_port,s", opt::value<int>(&start_port)->default_value(1024), "開(kāi)始端口")
		("end_port,e", opt::value<int>(&end_port)->default_value(65535), "結(jié)束端口")
		("help", "幫助菜單");

	opt::variables_map vm;
	try
	{
		opt::store(parse_command_line(argc, argv, opt), vm);
	}
	catch (...){
		std::cout << "command error!\n";
		return 0;
	}

	opt::notify(vm);
	if (vm.count("help"))
	{
		std::cout << opt << std::endl;
		return 0;
	}
	if (vm.count("address") && vm.count("start_port") && vm.count("end_port"))
	{
		//遍歷選項(xiàng)值  
		for (auto& str : vm["address"].as<std::vector<std::string> >())
			std::cout << str << " ";
		int x = vm["start_port"].as<int>();
		std::cout << x << std::endl;
	}
	return 0;
}

最終版

#include <iostream>
#include <boost/program_options.hpp>

namespace opt = boost::program_options;

int main(int argc, char const *argv[])
{
	opt::options_description des_cmd("\n Usage: 32位端口快速掃描器 Ver:1.0 \n\n Options: \n");
	des_cmd.add_options()
		("address,a", opt::value<std::string>()->default_value("127.0.0.1"), "指定地址")
		("start_port,s", opt::value<int>()->default_value(1024), "開(kāi)始端口")
		("end_port,e", opt::value<int>()->default_value(65535), "結(jié)束端口")
		("help,h", "幫助菜單");

	opt::variables_map virtual_map;
	try
	{
		opt::store(opt::parse_command_line(argc, argv, des_cmd), virtual_map);
	}
	catch (...){ return 0; }
	
	// 定義消息
	opt::notify(virtual_map);
	
	// 無(wú)參數(shù)直接返回
	if (virtual_map.empty())
	{
		return 0;
	}
	else if (virtual_map.count("help") || virtual_map.count("h"))
	{
		std::cout << des_cmd << std::endl;
		return 0;
	}
	else if (virtual_map.count("address") && virtual_map.count("start_port") && virtual_map.count("end_port"))
	{
		std::cout << "Addr = " << virtual_map["address"].as<std::string>() << std::endl;
		std::cout << "StartPort = " << virtual_map["start_port"].as<int>() << std::endl;
		std::cout << "EndPort = " << virtual_map["end_port"].as<int>() << std::endl;
	}
	else
	{
		std::cout << "option error" << std::endl;
	}
	return 0;
}

命令行下使用help輸出幫助菜單,當(dāng)傳入三個(gè)參數(shù)時(shí),即可解析到第二個(gè)判斷上,執(zhí)行相應(yīng)的函數(shù)即可。

C++怎么用Boost庫(kù)實(shí)現(xiàn)命令行

到此,關(guān)于“C++怎么用Boost庫(kù)實(shí)現(xiàn)命令行”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

網(wǎng)頁(yè)名稱:C++怎么用Boost庫(kù)實(shí)現(xiàn)命令行-創(chuàng)新互聯(lián)
文章地址:http://muchs.cn/article0/djijio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站制作全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站制作定制網(wǎng)站、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化