A Simple Demo
//
// Created by Water on 2018/6/9.
//
#include #include #include namespace bpo = boost::program_options;
int main(int argc, char const *argv[])
{
bpo::options_description opts(“all options”);
bpo::variables_map vm;
opts.add\_options()
("filename", bpo::value(), "the file name which want to be found")
("help", "this is a program to find a specified file");
try{
bpo::store(bpo::parse\_command\_line(argc, argv, opts), vm);
}
catch(...){
std::cout << "undefined option!\\n";
return 0;
}
if(vm.count("help") ){
std::cout << opts << std::endl;
}
if(vm.count("filename") ){
std::cout << "find " << vm\["filename"\].as() << std::endl;
}
if(vm.empty() ){
std::cout << "no options found \\n";
}
return 0;
}
Test
How to Set Default Value
//
// Created by Water on 2018/6/9.
//
#include #include #include #include namespace bpo = boost::program_options;
int main(int argc, char const *argv[])
{
std::vector v_para;
std::string port;
int baud = 0;
int timeout = 0;
bpo::options_description opt(“all options”);
opt.add\_options()
("v\_para,v", bpo::value >()->multitoken(), "Serial v\_para")
("port,p", bpo::value(&port)->default\_value("COM1"), "Serial Port")
("baud,b", bpo::value(&baud)->default\_value(115200), "Serial Baud")
("timeout,t", bpo::value(&timeout)->default\_value(5000), "Timeout")
("help", "a demo ( default value )");
bpo::variables\_map vm;
try{
bpo::store(parse\_command\_line(argc, argv, opt), vm);
}
catch(...){
std::cout << "undefined option!\\n";
return 0;
}
bpo::notify(vm);
if(vm.count("help") ){
std::cout << opt << std::endl;
return 0;
}
if(vm.count("v\_para") ){
std::cout << "v\_para:";
for(auto& str : vm\["v\_para"\].as >() )
std::cout << str << " ";
std::cout << std::endl;
}
std::cout << "port:" << port << std::endl;
std::cout << "baud:" << baud << std::endl;
std::cout << "timeout:" << timeout << std::endl;
return 0;
}