Include the actual option string ("--help"), not just the option name ("help") in ParsedOptions

This commit is contained in:
Ingo Ruhnke 2015-11-10 17:09:34 +01:00
parent 49cecac61e
commit 0e3435b2f3

View file

@ -79,13 +79,13 @@ ArgParser::parse_args(int argc, char** argv)
{
if (option->argument.empty())
{
parsed_options.push_back(ParsedOption(option->key, long_opt, ""));
parsed_options.push_back(ParsedOption(option->key, "--" + long_opt, ""));
}
else
{
if (pos != std::string::npos)
{
parsed_options.push_back(ParsedOption(option->key, long_opt, long_opt_arg));
parsed_options.push_back(ParsedOption(option->key, "--" + long_opt, long_opt_arg));
}
else
{
@ -95,7 +95,7 @@ ArgParser::parse_args(int argc, char** argv)
}
else
{
parsed_options.push_back(ParsedOption(option->key, long_opt, argv[i + 1]));
parsed_options.push_back(ParsedOption(option->key, "--" + long_opt, argv[i + 1]));
++i;
}
}
@ -124,25 +124,25 @@ ArgParser::parse_args(int argc, char** argv)
{
if (option->argument.empty())
{
parsed_options.push_back(ParsedOption(option->key, std::string(1, *p), ""));
parsed_options.push_back(ParsedOption(option->key, argv[i], ""));
}
else
{
if (i == argc - 1 || *(p+1) != '\0')
{
// No more arguments
throw std::runtime_error("option requires an argument -- " + std::string(1, *p));
throw std::runtime_error("option requires an argument -- " + std::string(argv[i]));
}
else
{
parsed_options.push_back(ParsedOption(option->key, std::string(1, *p), argv[i + 1]));
parsed_options.push_back(ParsedOption(option->key, argv[i], argv[i + 1]));
++i;
}
}
}
else
{
throw std::runtime_error("invalid option -- " + std::string(1, *p));
throw std::runtime_error("invalid option -- " + std::string(argv[i]));
}
++p;
}