|
|
@ -19,40 +19,40 @@ ArgParser::~ArgParser() |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ArgParser::printOptionError(char name, Error error) |
|
|
|
void ArgParser::printError(char parameter, Error error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
char tmp[] { name, '\0' }; |
|
|
|
char tmp[] { parameter, '\0' }; |
|
|
|
printOptionError(tmp, error, false); |
|
|
|
printError(tmp, error, false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ArgParser::printOptionError(const char* name, Error error, bool longName) |
|
|
|
void ArgParser::printError(const char* parameter, Error error, bool longName) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!m_errorReporting) { |
|
|
|
if (!m_errorReporting) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (error == Error::InvalidOption) { |
|
|
|
if (error == Error::InvalidOption) { |
|
|
|
printf("%s: invalid option -- '%s'\n", m_name, name); |
|
|
|
printf("%s: invalid option -- '%s'\n", m_name, parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (error == Error::UnrecognizedOption) { |
|
|
|
else if (error == Error::UnrecognizedOption) { |
|
|
|
printf("%s: unrecognized option -- '%s'\n", m_name, name); |
|
|
|
printf("%s: unrecognized option -- '%s'\n", m_name, parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (error == Error::DoesntAllowArgument) { |
|
|
|
else if (error == Error::DoesntAllowArgument) { |
|
|
|
printf("%s: option '--%s' doesn't allow an argument\n", m_name, name); |
|
|
|
printf("%s: option '--%s' doesn't allow an argument\n", m_name, parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (error == Error::ExtraOperand) { |
|
|
|
else if (error == Error::ExtraOperand) { |
|
|
|
printf("%s: extra operand '%s'\n", m_name, name); |
|
|
|
printf("%s: extra operand '%s'\n", m_name, parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (error == Error::RequiresArgument) { |
|
|
|
else if (error == Error::RequiresArgument) { |
|
|
|
if (longName) { |
|
|
|
if (longName) { |
|
|
|
printf("%s: option '--%s' requires an argument", m_name, name); |
|
|
|
printf("%s: option '--%s' requires an argument", m_name, parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
printf("%s: option requires an argument -- '%s'\n", m_name, name); |
|
|
|
printf("%s: option requires an argument -- '%s'\n", m_name, parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (error == Error::InvalidArgumentType) { |
|
|
|
else if (error == Error::InvalidArgumentType) { |
|
|
|
printf("%s: invalid argument type '%s'\n", m_name, name); |
|
|
|
printf("%s: invalid argument type '%s'\n", m_name, parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO: Print command usage, if it's enabled.
|
|
|
|
// TODO: Print command usage, if it's enabled.
|
|
|
@ -78,7 +78,7 @@ bool ArgParser::parseShortOption(std::string_view option, std::string_view next) |
|
|
|
|
|
|
|
|
|
|
|
// Option does not exist
|
|
|
|
// Option does not exist
|
|
|
|
if (foundOption == m_options.cend()) { |
|
|
|
if (foundOption == m_options.cend()) { |
|
|
|
printOptionError(c, Error::InvalidOption); |
|
|
|
printError(c, Error::InvalidOption); |
|
|
|
|
|
|
|
|
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
if (m_exitOnFirstError) { |
|
|
|
if (m_exitOnFirstError) { |
|
|
@ -93,7 +93,7 @@ bool ArgParser::parseShortOption(std::string_view option, std::string_view next) |
|
|
|
value = option.substr(i + 1); |
|
|
|
value = option.substr(i + 1); |
|
|
|
if (value.empty() && next.empty()) { |
|
|
|
if (value.empty() && next.empty()) { |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
printOptionError(c, Error::RequiresArgument); |
|
|
|
printError(c, Error::RequiresArgument); |
|
|
|
|
|
|
|
|
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
if (m_exitOnFirstError) { |
|
|
|
if (m_exitOnFirstError) { |
|
|
@ -105,7 +105,7 @@ bool ArgParser::parseShortOption(std::string_view option, std::string_view next) |
|
|
|
} |
|
|
|
} |
|
|
|
else if (next[0] == '-') { |
|
|
|
else if (next[0] == '-') { |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
printOptionError(c, Error::RequiresArgument); |
|
|
|
printError(c, Error::RequiresArgument); |
|
|
|
|
|
|
|
|
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
if (m_exitOnFirstError) { |
|
|
|
if (m_exitOnFirstError) { |
|
|
@ -152,14 +152,14 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next) |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (foundOption == m_options.cend()) { |
|
|
|
if (foundOption == m_options.cend()) { |
|
|
|
printOptionError(name.data(), Error::UnrecognizedOption); |
|
|
|
printError(name.data(), Error::UnrecognizedOption); |
|
|
|
|
|
|
|
|
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
} |
|
|
|
} |
|
|
|
else if (argumentProvided) { |
|
|
|
else if (argumentProvided) { |
|
|
|
if (foundOption->requiresArgument == Required::No) { |
|
|
|
if (foundOption->requiresArgument == Required::No) { |
|
|
|
foundOption->error = Error::DoesntAllowArgument; |
|
|
|
foundOption->error = Error::DoesntAllowArgument; |
|
|
|
printOptionError(name.data(), Error::DoesntAllowArgument); |
|
|
|
printError(name.data(), Error::DoesntAllowArgument); |
|
|
|
|
|
|
|
|
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
} |
|
|
|
} |
|
|
@ -173,7 +173,7 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next) |
|
|
|
else if (!next.empty() && foundOption->requiresArgument == Required::Yes) { |
|
|
|
else if (!next.empty() && foundOption->requiresArgument == Required::Yes) { |
|
|
|
if (next[0] == '-') { |
|
|
|
if (next[0] == '-') { |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
printOptionError(name.data(), Error::RequiresArgument); |
|
|
|
printError(name.data(), Error::RequiresArgument); |
|
|
|
|
|
|
|
|
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
} |
|
|
|
} |
|
|
@ -184,7 +184,7 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next) |
|
|
|
} |
|
|
|
} |
|
|
|
else if (foundOption->requiresArgument == Required::Yes) { |
|
|
|
else if (foundOption->requiresArgument == Required::Yes) { |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
foundOption->error = Error::RequiresArgument; |
|
|
|
printOptionError(name.data(), Error::RequiresArgument); |
|
|
|
printError(name.data(), Error::RequiresArgument); |
|
|
|
|
|
|
|
|
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
} |
|
|
|
} |
|
|
@ -203,7 +203,7 @@ bool ArgParser::parseArgument(std::string_view argument) |
|
|
|
for (;;) { |
|
|
|
for (;;) { |
|
|
|
// Run out of argument handlers
|
|
|
|
// Run out of argument handlers
|
|
|
|
if (m_argumentIndex >= m_arguments.size()) { |
|
|
|
if (m_argumentIndex >= m_arguments.size()) { |
|
|
|
printOptionError(argument.data(), Error::ExtraOperand); |
|
|
|
printError(argument.data(), Error::ExtraOperand); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -221,7 +221,7 @@ bool ArgParser::parseArgument(std::string_view argument) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
printOptionError(argument.data(), Error::InvalidArgumentType); |
|
|
|
printError(argument.data(), Error::InvalidArgumentType); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|