boost 处理命令行选项参数

时间:2021-04-17 14:28:25
// genmac.cpp : 定义控制台应用程序的入口点。
//

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include "stdafx.h"
#include  <stdio.h>
#include <StrUtils.h>
#include <Utils.h>
#include <iostream>
#include <fstream>
#include <exception>
#include <boost/program_options.hpp>

using namespace std;
using namespace boost;

program_options::options_description bOptions("Options");
char* mPrgPath=NULL;
void showhelpMessage(const TCHAR* pvMsg=NULL) {
	if (pvMsg) {
		wcout << pvMsg << endl << endl;
	}
	cout << "This is genmac 1.0.7 that built by rocklee,ALCO MIS." << endl;
	cout << bOptions << endl;
	cout << "example: " << mPrgPath << " --from=\"01-02-03-04-05-06\" --to=\"01-02-03-04-05-ff\" --output=\"c:\\temp\\genmac.txt\"" << endl;
}
bool macgen(string pvsMacFrom , string pvsMacTo,string pvsOutput) {
	//char* lvlpcMacFrom = "20-D1-60-DB-4C-3B";
	//char* lvlpcMacTo = "20-D1-60-DB-4D-3C";
	if (pvsMacFrom.size() != 17 || pvsMacTo.size() != 17 ||
		!(pvsMacFrom.substr(2, 1) == "-" &&pvsMacFrom.substr(5, 1) == "-" && pvsMacFrom.substr(8, 1) == "-" && pvsMacFrom.substr(11, 1) == "-" && pvsMacFrom.substr(14, 1) == "-")||
		!(pvsMacTo.substr(2, 1) == "-" &&pvsMacTo.substr(5, 1) == "-" && pvsMacTo.substr(8, 1) == "-" && pvsMacTo.substr(11, 1) == "-" && pvsMacTo.substr(14, 1) == "-")
		) {
		showhelpMessage(_TEXT("Invalid Mac format ."));
		return false;
	}
	fstream _file;
	if (pvsOutput.size()!=0 )
	{
		_file.open(pvsOutput.data(), ios::in);
		if (_file){
			showhelpMessage(_TEXT("target file is exists."));
			return false;
		}
		_file.close();
		try{			
			_file.open(pvsOutput.data(), ios::out);			
		}
		catch (std::exception & e) {
			wstring lvwsTmp = freestyle::StrUtils::MBytesToWString(e.what());
			showhelpMessage(lvwsTmp.data());
			_file.close();
			return false;
		}
	}
	bool lvbOutputToConsole = pvsOutput.size() == 0;
	long long lvulMac1 = freestyle::hex_to_decimal(freestyle::StrUtils::replace_all_distinct(pvsMacFrom, "-", "").data());// atoll(freestyle::StrUtils::replace_all_distinct(string(lvlpcMac1), "-", "").data());
	long long lvulMac2 = freestyle::hex_to_decimal(freestyle::StrUtils::replace_all_distinct(pvsMacTo, "-", "").data());//atoll(freestyle::StrUtils::replace_all_distinct(string(lvlpcMac2), "-", "").data());
	int lvCC = 0;
	for (long long lvulItem = lvulMac1; lvulItem <= lvulMac2; ++lvulItem) {
		char lvlpcCC[10];
		sprintf_s(lvlpcCC, "%9d", ++lvCC);
		/*if (lvbOutputToConsole){
			cout << lvlpcCC << "  " << lvulItem << " ";
		}*/
		char lvlpcTmp[13];
		sprintf_s(lvlpcTmp, "%12llX", lvulItem);
		string lvsTmp(lvlpcTmp);
		lvsTmp.insert(2, "-");
		lvsTmp.insert(5, "-");
		lvsTmp.insert(8, "-");
		lvsTmp.insert(11, "-");
		lvsTmp.insert(14, "-");
		if (lvbOutputToConsole){
			cout << lvlpcCC << " ";
			cout << lvsTmp.data() << endl;
		}
		else {
			_file << lvsTmp.data() << endl;
		}
	}	
	_file.close();
	cout << "Total " << lvCC << " records created." << endl;
	return true;
}

int main(int argc,char** argv)
{	
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	mPrgPath = argv[0];
	string lvsMacFrom, lvsMacTo,lvsOutFile;
	bOptions.add_options()
		("help", "Show help message")
		("from", program_options::value<string>(&lvsMacFrom), "MAC from")
		("to", program_options::value<string>(&lvsMacTo), "MAC to")
		("output", program_options::value<string>(&lvsOutFile), "output file name path");
	// parse program options
	program_options::variables_map mVMap;
	program_options::store(parse_command_line(argc, argv, bOptions), mVMap);
	program_options::notify(mVMap);

	// output help message if required
	if (mVMap.count("help")||mVMap.size()==0)
	{
		showhelpMessage();
		return 1;
	}

	// process other option
	if (!mVMap.count("from"))
	{
		showhelpMessage(_TEXT("Missing from option."));
		return 1;
	}
	if (!mVMap.count("to"))
	{
		showhelpMessage(_TEXT("Missing to option."));
		return 1;
	}
	if (macgen(lvsMacFrom, lvsMacTo, lvsOutFile)) {
		return 0;
	}
	else {
		return 1;
	}
}


上面演示了如何接受一个mac范围, 生成mac列表然后根据参数要求输出到指定文件或控制台. 主要留意program_options的add_options方法, 还有program_options::variables_map的count.


运行之:

C:\temp>genmac.exe
This is genmac 1.0.7 that built by rocklee,ALCO MIS.
Command Options:
  --help                Show help message
  --from arg            MAC from
  --to arg              MAC to
  --output arg          output file name path
C:\temp>genmac.exe --from="01-02-03-04-05-06"
Missing to option.

This is genmac 1.0.7 that built by rocklee,ALCO MIS.
Command Options:
  --help                Show help message
  --from arg            MAC from
  --to arg              MAC to
  --output arg          output file name path

example: genmac.exe --from="01-02-03-04-05-06" --to="01-02-03-04-05-ff" --output
="c:\temp\genmac.txt"

C:\temp>genmac.exe --from="01-02-03-04-05-06" --to="01-02-03-04-05-10"
        1  1-02-03-04-05-06
        2  1-02-03-04-05-07
        3  1-02-03-04-05-08
        4  1-02-03-04-05-09
        5  1-02-03-04-05-0A
        6  1-02-03-04-05-0B
        7  1-02-03-04-05-0C
        8  1-02-03-04-05-0D
        9  1-02-03-04-05-0E
       10  1-02-03-04-05-0F
       11  1-02-03-04-05-10
Total 11 records created.