boost 處理命令列選項引數

2021-08-04 03:53:41 字數 3407 閱讀 7016

// genmac.cpp : 定義控制台應用程式的入口點。

//#define _crtdbg_map_alloc

#include #include #include "stdafx.h"

#include #include #include #include #include #include #include using namespace std;

using namespace boost;

program_options::options_description boptions("options");

char* mprgpath=null;

void showhelpmessage(const tchar* pvmsg=null)

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)

fstream _file;

if (pvsoutput.size()!=0 )

_file.close();

trycatch (std::exception & e)

} 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 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)

else

} _file.close();

cout << "total " << lvcc << " records created." << endl;

return true;

}int main(int argc,char** argv)

// process other option

if (!mvmap.count("from"))

if (!mvmap.count("to"))

if (macgen(lvsmacfrom, lvsmacto, lvsoutfile))

else

}

上面演示了如何接受乙個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.

Boost 解析命令列引數

boost中有乙個program options庫,專門用來解析程式命令列引數的。allow long 接受長名稱 allow short 接受短名稱 allow dash for short 允許短選項使用 allow slash for short 允許短選項使用 long allow adja...

main 處理命令列選項

main 處理命令列選項 int main int argc,char argv 是c 的裡面的 main 函式的一種擴充套件原型 其中int argc 表示的是執行程式時命令列裡所帶引數的個數 1 因為包括了該程式名 如果不帶引數的話那就預設是 1 char argv 引數是字元陣列的指標,也可以...

main處理命令列選項

main處理命令列選項 雨竹清風 main 函式的引數可以是不為空,main 函式中的引數有兩個,乙個是argc表示陣列中元素的個數,乙個是char argv,表示的是指向乙個字串陣列的指標,所以也可以寫成char argv。有參的main 函式可寫為main int argc,char argv ...