#include#include int main(int argc, char* argv[]) { boost::program_options::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") // Option ("version", "print version number"); // Option boost::program_options::variables_map vm; boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); boost::program_options::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return 0; } else if (vm.count("version")) { std::cout << "Version 1.0" << std::endl; return 0; } std::cout << "No options were specified" << std::endl; return 0; }
#include#include int main(int argc, char* argv[]) { try { TCLAP::CmdLine cmd("Command line parser example", ' ', "1.0"); TCLAP::ValueArg sizeArg("s", "size", "Size of file", true, 0, "integer"); cmd.add(sizeArg); cmd.parse(argc, argv); int size = sizeArg.getValue(); std::cout << "File size: " << size << std::endl; } catch(TCLAP::ArgException &e) { std::cerr << "Error: " << e.error() << " for arg " << e.argId() << std::endl; } return 0; }
#includeIn this example, the Gflags library is used to define and parse command-line options. The Option get method is used to retrieve the value of an option defined using the DEFINE_* macros. The value can be accessed using the FLAGS_* variables.#include DEFINE_int32(size, 0, "Size of file"); int main(int argc, char* argv[]) { gflags::ParseCommandLineFlags(&argc, &argv, true); std::cout << "File size: " << FLAGS_size << std::endl; return 0; }