#includenamespace po = boost::program_options; int main(int argc, char** argv) { po::options_description desc("Options"); desc.add_options() ("help,h", "Print help message."); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return 0; } std::cout << "Number of arguments: " << vm.size() << std::endl; return 0; }
#includeIn this example, we create an options description object and add an option for help. We then create a variables map and parse the command line arguments using ArgList size's parse_command_line() function. We then check if the help option was passed and print the options description if it was. Finally, we iterate over each argument using ArgList size's iterator and print its key-value pair. To use ArgList size, you will need to add the Boost.Program_options library to your project.namespace po = boost::program_options; int main(int argc, char** argv) { po::options_description desc("Options"); desc.add_options() ("help,h", "Print help message."); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return 0; } for (auto it = vm.begin(); it != vm.end(); ++it) { std::cout << it->first << "=" << it->second << std::endl; } return 0; }