Boost 解析命令列引數

2021-07-25 04:47:18 字數 1554 閱讀 2586

boost中有乙個program_options庫,專門用來解析程式命令列引數的。

allow_long:接受長名稱

allow_short:接受短名稱

allow_dash_for_short:允許短選項使用"-"

allow_slash_for_short:允許短選項使用"/"

long_allow_adjacent:允許長選項中的引數在同乙個token。如"--foo=5"

long_allow_next:允許長選項中的引數在下乙個token。如"--foo 5"

short_allow_adjacent:允許短選項中的引數在同乙個token。如"-f5"

short_allow_next:允許短選項中的引數在下乙個token。如"-f 5"

allow_sticky:允許合併多個短選項,如將"-s -k"寫為"-sk"。除最後乙個選項外,其它所有選項不可接受引數。例如,如果"-s"要接受乙個引數,則"k"將被視為引數而不是另乙個選項。dos風格的短選項不可以合併。

allow_guessing:允許對長選項使用簡寫,如果簡寫可以明確標識乙個長選項。如果使用猜測功能,則不可以有某個長選項名是另乙個長選項名的字首。

long_case_insensitive:忽略長選項的大小寫

short_case_insensitive:忽略短選項的大小寫

case_insensitive:忽略選項的大小寫(long_case_insensitive | short_case_insensitive)

allow_long_disguise:允許長選項帶有單個選項開始字元,如"-foo=10"

unix_style:傳統unix風格(allow_short | short_allow_adjacent | short_allow_next | allow_long | long_allow_adjacent | long_allow_next | allow_sticky | allow_guessing | allow_dash_for_short)

default_style:預設風格(unix_style)

一般來說,allow_guessing和allow_long_disguise容易造成混亂,不建議使用。

boost命令行庫使用的預設風格是unix風格,以下風格都是合法的。

-x-a=5

-a 5

--xx

--yy=5

--yy 5

-bat

根據上面的選項風格可以看到,帶值的寫法就是要麼用"=",要麼用空格。如"--name=jack"、"/n pony"

#include #include #include #include namespace po = boost::program_options;

int main()

std::cout << map["age"].as() << std::endl;

system("pause");

return 0;

}

使用命令列"-n pony",輸出:

pony

18

解析命令列引數

include include include include int make argv const char astr,const char delimiters,char argvp void free argv char argvp int main int argc,char argv i...

boost 處理命令列選項引數

genmac.cpp 定義控制台應用程式的入口點。define crtdbg map alloc include include include stdafx.h include include include include include include include using namesp...

python解析命令列引數

使用乙個先進的模組名為argparse,跟unix程式的命令列引數很像。直接對code做個筆記 import sys import argparse def main args print first name directory s args.first name print first para...