int main(int argc, char* argv[]) { if (argc > 1) { cout << "The first argument is: " << argv[1] << endl; } else { cout << "No arguments were passed." << endl; } return 0; }
#includeThis example uses the Boost Program Options library to parse command-line options and arguments. It defines several options, such as --help, --input-file, and --output-file. The code also checks whether these options were specified and prints out their values.int main(int argc, char* argv[]) { namespace po = boost::program_options; po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") ("input-file", po::value (), "input file") ("output-file", po::value (), "output file"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { cout << desc << endl; return 0; } if (vm.count("input-file")) { cout << "Input file: " << vm["input-file"].as () << endl; } else { cout << "No input file specified." << endl; } if (vm.count("output-file")) { cout << "Output file: " << vm["output-file"].as () << endl; } else { cout << "No output file specified." << endl; } return 0; }