cxxopts
cxxopts copied to clipboard
function parse() cause unexpected input value changed
First thanks for your great job.
I got unexpected value changed to input argc.
test code as below:
` #include "cxxopts.hpp"
#include
using namespace std;
void print_usage(char* n) { std::cout << "usage : " << std::endl; }
static void parse_options(int& argc, char**& argv) {
{
std::cout << "before parse, input argc : " << argc << ", first argument: " << argv[1] << "...\n";
}
cxxopts::Options opt_parser(argv[0]);
opt_parser.add_options()
("d,debug", "Enable debug messages from SteamFS", cxxopts::value
try {
auto options = opt_parser.parse(argc, argv);
if (options.count("help")) {
print_usage(argv[0]);
// Strip everything before the option list from the default help string.
auto help = opt_parser.help();
std::cout << std::endl << "options:"
<< help.substr(help.find("\n\n") + 1, string::npos);
exit(0);
}
{
std::cout << "after parse, input argc : " << argc << ", first argument: " << argv[1] << "...\n";
}
std::cout << "parsed result: d: " << options["debug"].as<string>() << ", s: "<< options["single"].as<int>() << std::endl;
// Open some files, validate some values, etc
} catch (cxxopts::option_not_exists_exception& exc) {
std::cout << argv[0] << ": " << exc.what() << std::endl;
print_usage(argv[0]);
exit(2);
}
}
int main(int argc, char* argv[]) { std::cout << "argc : " << argc << std::endl; for(int i=0; i < argc; i++) { std::cout << argv[i] << " "; } std::cout<<std::endl; parse_options(argc, argv); return 0; } `
when exec test:
~/w/s/h/c/test ❯❯❯ g++ -o test test.cpp -I ../include/
~/w/s/h/c/test ❯❯❯ ./test -d sxx -s 3
we can see the input argc changed from 5 to 1.
argc : 5
./test -d sxx -s 3
before parse, input argc : 5, first argument: -d...
after parse, input argc : 1, first argument: -d...
parsed result: d: sxx, s: 3
That's because cxxopts
is removing the arguments that it has recognised. I will be working on a side-effect free version of the parse
function at some point in the future.