/** * \file example.cc * \author Sven Moritz Hallberg * \date 2002-02-12 * * \brief Usage example for prk/clopt. */ #include "clopt.hh" #include #include #include // copy int main(int argc, char* argv[]) { const char* usage_string = "Usage: example [options] [arguments] \n" "\n" "Parse the command line using prk::clparse.\n" "\n" "Options understood:\n" " -h Display this help.\n" " -a Examples of non-argument options.\n" " -b ...\n" " -c ...\n" " -n ARG Examples of options with arguments.\n" " -l ARG ...\n"; // Display usage and exit if no arguments were given. if(argc < 2) { std::cerr << usage_string; return 1; } // Store the clopts here. std::vector clopts; // Parse the command line. try { char **rest; // iterator pointing to first non-option rest = prk::clparse(argv+1, argv+argc, std::back_inserter(clopts), "nl"); std::cout << clopts.size() << " command line option" << (clopts.size()>1?"s":"") << " parsed." << std::endl; // Handle clopts. for(std::vector::const_iterator it = clopts.begin(); it != clopts.end(); ++it) { switch(it->name()) { case 'h': std::cout << usage_string; break; case 'a': case 'b': case 'c': std::cout << "Non-argument option -" << it->name() << " recognized by handler." << std::endl; break; case 'n': case 'l': std::cout << "Argument-expecting option -" << it->name() << " recognized by handler." << std::endl; std::cout << "Argument is: " << it->optarg() << std::endl; break; default: std::cerr << "Unrecognized option -" << it->name() << " encountered by handler." << std::endl; } } std::cout << std::endl; // Display remaining arguments. std::cout << "Remaining command line arguments:\n"; std::copy(rest, argv+argc, std::ostream_iterator(std::cout, "\n")); } catch(const prk::missing_optarg& e) { std::cerr << "error: " << e.what() << std::endl; return 1; } return 0; }