// ...#include<boost/program_options.hpp>namespacepo=boost::program_options;intmain(intargc,char*argv[]){intopt;// Declare the supported options.po::options_descriptiondesc("Allowed options");desc.add_options()("help,h","produce help message")("longoption","This is a long option: --longoption.")("option2,o","This is a shorthand option with long option, use --option2 or -o")(",O","This is a shorthand only option: use -O only")// see below about how to access("required,r",po::value<std::strubg>()->required(),"a mandatory option with string argument: -r string")// NOTE: required arguments will prevent --help from work. You can use count("required") == 0 instead...("list",po::value<std::vector<int>>(),"List of values: --list 1 --list 2 --list 3")("opt",po::value(&opt),"Another option with argument, directly into the variable")("default",po::value<std::string>()->default_value("the-fault"),"optional argument with default value");po::variables_mapvm;po::store(po::parse_command_line(argc,argv,desc),vm);po::notify(vm);if(vm.count("help")){std::cout<<desc<<"\n";return1;}std::stringrequired_string=vm["required"].as<std::string>();autolist=vm["list"].as<std::vector<int>>();if(vm.count("-O")){// do something with shorthand option}// ...}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.