#include#include namespace po = boost::program_options; int main(int argc, char** argv) { try { po::options_description desc("Allowed options"); desc.add_options() ("help", "produce 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; } if (argc < 2) { std::cout << "No arguments provided." << std::endl; return 1; } } catch (std::exception& e) { std::cerr << "error: " << e.what() << std::endl; return 1; } return 0; }
#includeIn this example, the "SwitchArg" function is used to define two empty command line options. If either option is provided on the command line, the program outputs a message indicating that either "foo" or "bar" mode is enabled. Package/library: TCLAP (tclap/CmdLine.h)#include int main(int argc, char** argv) { try { TCLAP::CmdLine cmd("Command line options", ' ', "0.9"); TCLAP::SwitchArg fooArg("f","foo","Enable foo mode", false); cmd.add(fooArg); TCLAP::SwitchArg barArg("b","bar","Enable bar mode", false); cmd.add(barArg); cmd.parse(argc, argv); if (fooArg.getValue() || barArg.getValue()) { std::cout << "Enabled foo or bar mode." << std::endl; } else { std::cout << "No options provided." << std::endl; } } catch (TCLAP::ArgException& e) { std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; } return 0; }