Here is a simple program using boost to parse options:
#include <iostream> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc, char **argv) { try { size_t param = std::numeric_limits<size_t>::max(); po::options_description desc("Syntax: [options] \"input binary file\".\nAllowed options:"); desc.add_options() ("help,h", "produce help message") ("param,p", po::value<size_t>(¶m), "param"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::store(po::command_line_parser(argc, argv).options(desc).run(), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << "\n"; } else { std::cout << "Running with param " << param << std::endl; return 0; } } catch(std::exception& e) { std::cerr << "error: " << e.what() << "\n"; } catch(...) { std::cerr << "Exception of unknown type!\n"; } return 1; } Here are the outputs produced:
hex2txt.exeoutputsRunning with param 0hex2txt.exe --param 3outputsRunning with param 3hex2txt.exe --totooutputserror: unrecognised option '--toto'
All this is expected, however: hex2txt.exe foo outputs Running with param 0
I would have expected to get an error as "foo" is not expected. What am I doing wrong here?