Skip to content

Instantly share code, notes, and snippets.

@wangbj
Created September 12, 2019 13:33
Show Gist options
  • Save wangbj/5d884a515eeeda4091592695ff3e952c to your computer and use it in GitHub Desktop.
Save wangbj/5d884a515eeeda4091592695ff3e952c to your computer and use it in GitHub Desktop.
/* run: ./a.out --long-option-one=1 --long-option-two=test -- abc d e f "" */
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include "cxxopts.hpp"
int main(int argc, char* argv[])
{
cxxopts::Options options("MyProgram", "One line description of MyProgram");
options.add_options()
("long-option-one",
"accept a long option - integer",
cxxopts::value<int>())
("long-option-two",
"accept another long option - string",
cxxopts::value<std::string>())
("program",
"program to run",
cxxopts::value<std::string>())
("programArgs",
"program arguments",
cxxopts::value<std::vector<std::string>>());
try {
options.parse_positional({"program", "programArgs"});
auto results = options.parse(argc, argv);
if (results["program"].count()) {
std::cout << "PROGRAM: " << results["program"].as<std::string>() << std::endl;
}
if (results["programArgs"].count()) {
for (auto arg: results["programArgs"].as<std::vector<std::string>>()) {
printf("%s(%ld) ", arg.c_str(), arg.size());
}
printf("\n");
}
} catch (const cxxopts::OptionException& e) {
std::cout << "caught exception: " << e.what()
<< " when parsing command line" << std::endl;
std::cout << options.help() << std::endl;
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment